chg: [inbox:collectNotifications] Collect notifications for the logged in user

pull/93/head
Sami Mokaddem 2022-01-25 11:32:09 +01:00
parent 42de70e87d
commit 74df550419
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 19 additions and 4 deletions

View File

@ -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;
}

View File

@ -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;
}
}