From 74df5504198f5768fd68ff29766179095563b240 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 25 Jan 2022 11:32:09 +0100 Subject: [PATCH] chg: [inbox:collectNotifications] Collect notifications for the logged in user --- .../Component/NotificationComponent.php | 3 ++- src/Model/Table/InboxTable.php | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Controller/Component/NotificationComponent.php b/src/Controller/Component/NotificationComponent.php index 1349548..33c460b 100644 --- a/src/Controller/Component/NotificationComponent.php +++ b/src/Controller/Component/NotificationComponent.php @@ -16,6 +16,7 @@ class NotificationComponent extends Component public function initialize(array $config): void { $this->request = $config['request']; + $this->Controller = $this->getController(); } public function getNotifications(): array @@ -40,7 +41,7 @@ class NotificationComponent extends Component { $notifications = []; if (method_exists($table, 'collectNotifications')) { - $notifications = $table->collectNotifications(); + $notifications = $table->collectNotifications($this->Controller->ACL->getUser()); } return $notifications; } diff --git a/src/Model/Table/InboxTable.php b/src/Model/Table/InboxTable.php index 4e48c95..9a56b80 100644 --- a/src/Model/Table/InboxTable.php +++ b/src/Model/Table/InboxTable.php @@ -92,11 +92,10 @@ class InboxTable extends AppTable return $savedEntry; } - public function collectNotifications(): array + public function collectNotifications(\App\Model\Entity\User $user): array { $allNotifications = []; - $query = $this->find(); - $inboxNotifications = $query->all()->toArray(); + $inboxNotifications = $this->getNotificationsForUser($user); foreach ($inboxNotifications as $notification) { $title = __('New message'); $details = $notification->title; @@ -116,4 +115,19 @@ class InboxTable extends AppTable } return $allNotifications; } + + public function getNotificationsForUser(\App\Model\Entity\User $user): array + { + $query = $this->find(); + $conditions = []; + if ($user['role']['perm_admin']) { + // Admin will not see notifications if it doesn't belong to them. They can see process the message from the inbox + $conditions['Inbox.user_id IS'] = null; + } else { + $conditions['Inbox.user_id'] = $user->id; + } + $query->where($conditions); + $notifications = $query->all()->toArray(); + return $notifications; + } }