From 19f5e539ff52fe63ea83dcf28654c63e765c1cce Mon Sep 17 00:00:00 2001 From: mccrodp Date: Sun, 19 Jun 2016 21:39:30 +0100 Subject: [PATCH] Fixed private_message access hook and removed redundant classes. --- ...ate_message.field_message_private_body.yml | 6 +- ..._message.field_message_private_subject.yml | 2 +- ..._message.field_message_private_to_user.yml | 16 +-- .../install/message.type.private_message.yml | 2 +- ....user.field_message_private_usr_notify.yml | 4 +- message_private.info.yml | 5 +- message_private.links.action.yml | 8 +- message_private.module | 134 ++++-------------- message_private.permissions.yml | 8 +- message_private.services.yml | 5 - src/Controller/MessagePrivateController.php | 112 --------------- .../Derivative/MessagePrivateLocalTasks.php | 2 +- src/Routing/RouteSubscriber.php | 57 -------- templates/message--private_message.html.twig | 2 +- 14 files changed, 53 insertions(+), 310 deletions(-) delete mode 100644 message_private.services.yml delete mode 100644 src/Controller/MessagePrivateController.php delete mode 100644 src/Routing/RouteSubscriber.php diff --git a/config/install/message.private_message.field_message_private_body.yml b/config/install/message.private_message.field_message_private_body.yml index b125762..8ab9365 100644 --- a/config/install/message.private_message.field_message_private_body.yml +++ b/config/install/message.private_message.field_message_private_body.yml @@ -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 @@ -18,4 +16,4 @@ translatable: false default_value: { } default_value_callback: '' settings: { } -field_type: text_long \ No newline at end of file +field_type: string_long \ No newline at end of file diff --git a/config/install/message.private_message.field_message_private_subject.yml b/config/install/message.private_message.field_message_private_subject.yml index 528d37b..c012fb0 100644 --- a/config/install/message.private_message.field_message_private_subject.yml +++ b/config/install/message.private_message.field_message_private_subject.yml @@ -1,4 +1,4 @@ -uuid: 4cc25c0e-05f9-424e-a103-3b7e799355df +uuid: 172ed12e-fb77-47f7-9390-afaa2346caf5 langcode: en status: true dependencies: diff --git a/config/install/message.private_message.field_message_private_to_user.yml b/config/install/message.private_message.field_message_private_to_user.yml index 276e57e..603f959 100644 --- a/config/install/message.private_message.field_message_private_to_user.yml +++ b/config/install/message.private_message.field_message_private_to_user.yml @@ -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: '' @@ -23,6 +23,6 @@ settings: type: _none target_bundles: null sort: - field: name - direction: ASC -field_type: entity_reference \ No newline at end of file + field: _none + auto_create: false +field_type: entity_reference diff --git a/config/install/message.type.private_message.yml b/config/install/message.type.private_message.yml index fb6cda1..7b3f533 100644 --- a/config/install/message.type.private_message.yml +++ b/config/install/message.type.private_message.yml @@ -14,4 +14,4 @@ data: override: true enabled: false quota: null - days: null + days: null \ No newline at end of file diff --git a/config/install/user.user.field_message_private_usr_notify.yml b/config/install/user.user.field_message_private_usr_notify.yml index 08d4039..14e5581 100644 --- a/config/install/user.user.field_message_private_usr_notify.yml +++ b/config/install/user.user.field_message_private_usr_notify.yml @@ -1,4 +1,4 @@ -uuid: b85ace72-8888-4e6b-8900-a3172b02aab2 +uuid: 39b154d5-bd3c-4b78-bc5d-12784718cb45 langcode: en status: true dependencies: @@ -16,7 +16,7 @@ required: false translatable: false default_value: - - value: 1 + value: 0 default_value_callback: '' settings: on_label: 'On' diff --git a/message_private.info.yml b/message_private.info.yml index 67177a8..9752857 100644 --- a/message_private.info.yml +++ b/message_private.info.yml @@ -5,6 +5,7 @@ package: Message type: module configure: message_private.admin_settings dependencies: + - views - message - - message_ui - - views \ No newline at end of file + - message_notify + - message_ui \ No newline at end of file diff --git a/message_private.links.action.yml b/message_private.links.action.yml index a79ff92..4b5ff6e 100644 --- a/message_private.links.action.yml +++ b/message_private.links.action.yml @@ -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 \ No newline at end of file diff --git a/message_private.module b/message_private.module index e548d7e..6dd0a30 100755 --- a/message_private.module +++ b/message_private.module @@ -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(); + } } } @@ -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')); } } } diff --git a/message_private.permissions.yml b/message_private.permissions.yml index 32d4880..267fad0 100644 --- a/message_private.permissions.yml +++ b/message_private.permissions.yml @@ -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.' diff --git a/message_private.services.yml b/message_private.services.yml deleted file mode 100644 index 9a92801..0000000 --- a/message_private.services.yml +++ /dev/null @@ -1,5 +0,0 @@ -services: - message_private.route_subscriber: - class: Drupal\message_private\Routing\RouteSubscriber - tags: - - { name: event_subscriber } \ No newline at end of file diff --git a/src/Controller/MessagePrivateController.php b/src/Controller/MessagePrivateController.php deleted file mode 100644 index 126dc45..0000000 --- a/src/Controller/MessagePrivateController.php +++ /dev/null @@ -1,112 +0,0 @@ -entityManager = \Drupal::entityManager(); - } - - /** - * Display list of message types to create an instance for them. - * - */ - // @todo - remove note: message_private_create_new_message_instance_list in D7. - public function getAllowedInstanceList() { - // $access_controller = new MessageAccessControlHandler('message'); - // $allowed_types = $access_controller->userCreateMessageAccess(); - - // @todo - replace this line with access controlled type list: - $allowed_types = MessageType::loadMultiple(); - - if ($types = MessageType::loadMultiple()) { - foreach ($types as $type) { - if ($allowed_types || (is_array($allowed_types) && array_key_exists($type, $allowed_types))) { - return $allowed_types; - } - } - } - return FALSE; - } - - /** - * Generates output of all message type entities with permission to create. - * - * @return array - * An array as expected by drupal_render(). - */ - public function showTypes() { - // $account = $this->currentUser(); - - // @todo add access control for message_type, see message_ui_access_control. - - $items = array(); - // @todo : Use the following or MessageType's method? $this->entityManager()->getStorage('message_type')->loadMultiple() - // Only use node types the user has access to. - // @todo - override the path for private messages, or is there a better way? - foreach ($this->getAllowedInstanceList() as $type => $entity) { - if ($type != 'private_message') { - // @todo - get access control working below. - // \Doctrine\Common\Util\Debug::dump($this->entityManager()->getAccessControlHandler('message')->createAccess($type->id())); - // if ($this->entityManager()->getAccessControlHandler('message')->createAccess($type->id())) { - /* @var $entity MessageType */ - $url = Url::fromUri('internal:/admin/content/messages/create/' . str_replace('_', '-', $type)); - $items[] = array( - 'type' => $type, - 'name' => $entity->label(), - 'internal_link' => Link::fromTextAndUrl(ucfirst(str_replace('_', ' ', $type)), $url), - ); - //\Doctrine\Common\Util\Debug::dump($content); - // } - } - else { - // Create link to customised menu item for private_message create. - /* @var $entity MessageType */ - $url = Url::fromUri('internal:/message/create/' . str_replace('_', '-', $type)); - $items[] = array( - 'type' => $type, - 'name' => $entity->label(), - 'internal_link' => Link::fromTextAndUrl(ucfirst(str_replace('_', ' ', $type)), $url), - ); - } - } - - // Bypass the admin/content/messages/create listing if only one content type is available. - /* if (count($content) == 1) { - $type = array_shift($content); - return $this->redirect('message_ui.create_message_by_type', array('message_type' => $type->id())); - } */ - - if ($items) { - return array( - '#theme' => 'instance_item_list', - '#items' => $items, - '#type' => 'ul' - ); - } - else { - $url = Url::fromRoute('message.type_add'); - return t("There are no messages types. You can create a new message type here."); - } - } -} diff --git a/src/Plugin/Derivative/MessagePrivateLocalTasks.php b/src/Plugin/Derivative/MessagePrivateLocalTasks.php index a19135f..1335ab9 100644 --- a/src/Plugin/Derivative/MessagePrivateLocalTasks.php +++ b/src/Plugin/Derivative/MessagePrivateLocalTasks.php @@ -23,7 +23,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { $this->derivatives['message_private.messages'] = $base_plugin_definition; $this->derivatives['message_private.messages']['title'] = 'Messages'; // @todo: this should pass a user / uid argument also. - $this->derivatives['message_private.messages']['route_name'] = 'message_private.user.create_message'; + $this->derivatives['message_private.messages']['route_name'] = 'message_private.user.add'; return $this->derivatives; } } diff --git a/src/Routing/RouteSubscriber.php b/src/Routing/RouteSubscriber.php deleted file mode 100644 index 827b0b1..0000000 --- a/src/Routing/RouteSubscriber.php +++ /dev/null @@ -1,57 +0,0 @@ -get('message_ui.show_message'); - if ($route) { - $route->setRequirements(array( - '_message_private_view_access' => '{message}', - )); - } - // Message creation types list. - $route = $collection->get('message_ui.create_message'); - if ($route) { - // $route->setDefault('controller', '\Drupal\message_private\Controller\MessagePrivateController::showTypes'); - } - // Message create by private message type. - $route = $collection->get('message_ui.create_message_by_type'); - if ($route) { - $route->setRequirements(array( - '_message_private_add_access:' => 'message:private_message', - )); - } - // Modify Message edit form to have private message access permissions. - $route = $collection->get('message_ui.edit_message'); - if ($route) { - $route->setRequirements(array( - '_message_private_edit_access' => '{message}', - )); - } - // Message deletion. - $route = $collection->get('message_ui.delete_message'); - if ($route) { - $route->setRequirements(array( - '_message_private_delete_access' => '{message}', - )); - } - } -} diff --git a/templates/message--private_message.html.twig b/templates/message--private_message.html.twig index cc83f08..9d6de9b 100644 --- a/templates/message--private_message.html.twig +++ b/templates/message--private_message.html.twig @@ -34,7 +34,7 @@