Skip to content

Commit

Permalink
Fixed private_message access hook and removed redundant classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mccrodp committed Jun 19, 2016
1 parent 0fa83da commit 19f5e53
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 310 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
uuid: 47962025-df21-493d-a921-33c199ffbb3c
uuid: 8b2dba0f-83fd-45ab-877a-6a6560bf1e00
langcode: en
status: true
dependencies:
config:
- field.storage.message.field_message_private_body
- message.type.private_message
module:
- text
id: message.private_message.field_message_private_body
field_name: field_message_private_body
entity_type: message
Expand All @@ -18,4 +16,4 @@ translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: text_long
field_type: string_long
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: 4cc25c0e-05f9-424e-a103-3b7e799355df
uuid: 172ed12e-fb77-47f7-9390-afaa2346caf5
langcode: en
status: true
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
uuid: 1ae7f899-eed6-442b-9018-249dca166c0a
uuid: bcfc43e3-2726-4b76-99b6-2602ad6a4610
langcode: en
status: true
dependencies:
config:
- field.storage.message.field_message_to_user
- field.storage.message.field_message_private_to_user
- message.type.private_message
id: message.private_message.field_message_to_user
field_name: field_message_to_user
id: message.private_message.field_message_private_to_user
field_name: field_message_private_to_user
entity_type: message
bundle: private_message
label: 'To User'
description: 'Private message to user reference.'
required: true
required: false
translatable: false
default_value: { }
default_value_callback: ''
Expand All @@ -23,6 +23,6 @@ settings:
type: _none
target_bundles: null
sort:
field: name
direction: ASC
field_type: entity_reference
field: _none
auto_create: false
field_type: entity_reference
2 changes: 1 addition & 1 deletion config/install/message.type.private_message.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ data:
override: true
enabled: false
quota: null
days: null
days: null
4 changes: 2 additions & 2 deletions config/install/user.user.field_message_private_usr_notify.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uuid: b85ace72-8888-4e6b-8900-a3172b02aab2
uuid: 39b154d5-bd3c-4b78-bc5d-12784718cb45
langcode: en
status: true
dependencies:
Expand All @@ -16,7 +16,7 @@ required: false
translatable: false
default_value:
-
value: 1
value: 0
default_value_callback: ''
settings:
on_label: 'On'
Expand Down
5 changes: 3 additions & 2 deletions message_private.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package: Message
type: module
configure: message_private.admin_settings
dependencies:
- views
- message
- message_ui
- views
- message_notify
- message_ui
8 changes: 4 additions & 4 deletions message_private.links.action.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Add a 'Create a new message for %user' action link.
# Only display the local task on other users profile, not current user's.
message_private.user.create_message:
route_name: message_ui.create_message_by_type
message_private.user.add:
route_name: message_ui.add
title: 'Create a new message for...'
appears_on:
- entity.user.canonical

# Add a 'Create a new message' action link above message_private view.
# Get from user to Add a 'Create a new message for %user' action link
message_private.messages.create_message:
route_name: message_ui.create_message_by_type
message_private.messages.add:
route_name: message_ui.add
title: 'Create a new message for...'
appears_on:
- message_private.messages
134 changes: 26 additions & 108 deletions message_private.module
Original file line number Diff line number Diff line change
Expand Up @@ -72,117 +72,33 @@ function message_private_help($route_name, RouteMatchInterface $arg) {
function message_private_message_access(EntityInterface $message, $operation, AccountInterface $account) {
// Customise access check only for private messages.
if ($message->bundle() == 'private_message') {
// Perform operation specific access control.
switch ($operation) {
case 'view':
/*
$access_control_handler = $this->entityManager->getAccessControlHandler('message');
// If checking whether a node of a particular type may be created.
if ($account->hasPermission('administer message private')
|| $account->hasPermission('bypass private message access control')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($message_type) {
return $access_control_handler->createAccess($message_type->id(), $account, [], TRUE);
}
// If checking whether a message of any type may be created.
foreach ($this->entityManager->getStorage('message_type')->loadMultiple() as $message_type) {
if (($access = $access_control_handler->createAccess($message_type->id(), $account, [], TRUE)) && $access->isAllowed()) {
return $access;
}
}
*/
break;
case 'add':
/*
if ($account->hasPermission('administer message private')
|| $account->hasPermission('bypass private message access control')) {
// If checking whether a node of a particular type may be created.
if ($account->hasPermission('administer message private')
|| $account->hasPermission('bypass private message access control')) {
return AccessResult::allowed()->cachePerPermissions();
}
// Verify that the user can apply the op.
if ($account->hasPermission($operation . ' any message instance')
|| $account->hasPermission($operation . ' a private_message message instance', $account)
) {
if ($operation != 'create') {
// Check if the user is message author.
/* @var Drupal\message\Entity\message $message */
if ($message->getOwnerId() == $account->id()) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($message_type) {
return $access_control_handler->createAccess($message_type->id(), $account, [], TRUE);
}
// If checking whether a message of any type may be created.
foreach ($this->entityManager->getStorage('message_type')->loadMultiple() as $message_type) {
if (($access = $access_control_handler->createAccess($message_type->id(), $account, [], TRUE)) && $access->isAllowed()) {
return $access;
}
}
*/
break;
case 'edit':
/*
$access_control_handler = $this->entityManager->getAccessControlHandler('message');
// If checking whether a node of a particular type may be created.
if ($account->hasPermission('administer message private')
|| $account->hasPermission('bypass private message access control')) {
return AccessResult::allowed()->cachePerPermissions();
}

// @todo: go through the below, previously message_private_access_control().

// Get the message type from the function argument or from the message object.
$type = $message->bundle();

// If this is not a private message then use the message callback provided
// by message_ui module.
if ($type != 'private_message') {
// No opinion.
return AccessResult::neutral();
}
else {
if ($account->hasPermission('bypass private message access control')) {
return TRUE;
}

$operation = 'edit';

// Verify that the user can apply the op.
if ($account->hasPermission($operation . ' any message instance')
|| $account->hasPermission($operation . ' a ' . $type . ' message instance')
) {
if ($type == 'private_message' && $operation != 'create') {
// Check if the user is message author.
// @var $message \Drupal\message\Entity\Message
if ($message->getAuthorId() == $account->id()) {
return TRUE;
}
$users = $message->get('field_message_user_ref');
if ($users && is_array($users)) {
foreach ($users as $user_ref) {
if ($user_ref['target_id'] == $account->id()) {
return TRUE;
}
}
}
}
else {
return TRUE;
$users = $message->get('field_message_private_to_user')->getValue();
if ($users && is_array($users)) {
foreach ($users as $user_ref) {
if ($user_ref['target_id'] == $account->id()) {
return AccessResult::allowed()->cachePerPermissions();
}
}
}
return FALSE;
*/
break;
case 'delete':
/*
$access_control_handler = $this->entityManager->getAccessControlHandler('message');
// If checking whether a node of a particular type may be created.
if ($account->hasPermission('administer message private')
|| $account->hasPermission('bypass private message access control')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($message_type) {
return $access_control_handler->createAccess($message_type->id(), $account, [], TRUE);
}
// If checking whether a message of any type may be created.
foreach ($this->entityManager->getStorage('message_type')->loadMultiple() as $message_type) {
if (($access = $access_control_handler->createAccess($message_type->id(), $account, [], TRUE)) && $access->isAllowed()) {
return $access;
}
}
*/
break;
}
else {
return AccessResult::allowed()->cachePerPermissions();
}
}
}

Expand Down Expand Up @@ -317,8 +233,10 @@ function message_private_message_insert(\Drupal\message\MessageInterface $messag
}
}
if (!empty($mail)) {
// @todo: wait for D8 version of message_notify or use alternative?
message_notify_send_message($message, array('mail' => implode(',', $mail)));
/* @var \Drupal\message_notify\MessageNotifier $message_notifier */
$message_notifier = $this->container->get('message_notify.sender');
// @todo - figure out how to pass the email addresses to the notify plugin / hook.
$message_notifier->send($message, array('mail' => implode(',', $mail), 'email'));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions message_private.permissions.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'bypass private message access control':
title: 'Bypass message access control'
bypass private message access control:
title: 'Bypass message private access control'
description: 'Grant to the user the permission to apply CRUD option on any private messages.'
'restrict access': true
'administer message private':
restrict access: true
administer message private:
title: 'Administer message private'
description: 'Access the message private administration pages.'
5 changes: 0 additions & 5 deletions message_private.services.yml

This file was deleted.

Loading

0 comments on commit 19f5e53

Please sign in to comment.