-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNotificationPermissionPolicy.php
More file actions
86 lines (69 loc) · 2.09 KB
/
NotificationPermissionPolicy.php
File metadata and controls
86 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace App\Security\Policies;
use App\Model\Entity\Group;
use App\Model\Entity\GroupMembership;
use App\Model\Entity\Notification;
use App\Security\Identity;
use App\Security\Roles;
class NotificationPermissionPolicy extends BasePermissionPolicy implements IPermissionPolicy
{
/** @var Roles */
private $roles;
public function getAssociatedClass()
{
return Notification::class;
}
public function __construct(Roles $roles)
{
$this->roles = $roles;
}
public function hasRole(Identity $identity, Notification $notification)
{
$user = $identity->getUserData();
if (!$user) {
return false;
}
return $this->roles->isInRole($user->getRole(), $notification->getRole());
}
public function isGlobal(Identity $identity, Notification $notification)
{
return $notification->getGroups()->isEmpty();
}
public function isAuthor(Identity $identity, Notification $notification)
{
$user = $identity->getUserData();
if (!$user) {
return false;
}
return $user === $notification->getAuthor();
}
public function isAncestorGroupAdmin(Identity $identity, Notification $notification)
{
$user = $identity->getUserData();
if ($user === null || $notification->getGroups()->isEmpty()) {
return false;
}
/** @var Group $group */
foreach ($notification->getGroups() as $group) {
if ($this->checkMinimalMembership($user, $group, GroupMembership::TYPE_ADMIN)) {
return true;
}
}
return false;
}
public function isGroupsMember(Identity $identity, Notification $notification)
{
$user = $identity->getUserData();
if (!$user) {
return false;
}
/** @var Group $group */
foreach ($notification->getGroups() as $group) {
$isMember = $group->isMemberOfSubgroup($user);
if ($isMember) {
return true;
}
}
return false;
}
}