Skip to content

Commit 042460f

Browse files
committed
Base presenter and permissions for notifications
1 parent 4f6a664 commit 042460f

File tree

7 files changed

+156
-1
lines changed

7 files changed

+156
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\V1Module\Presenters;
4+
5+
use App\Exceptions\ForbiddenRequestException;
6+
use App\Model\Entity\Notification;
7+
use App\Model\Repository\Notifications;
8+
use App\Security\ACL\INotificationPermissions;
9+
10+
class NotificationsPresenter extends BasePresenter {
11+
12+
/**
13+
* @var INotificationPermissions
14+
* @inject
15+
*/
16+
public $notificationAcl;
17+
18+
/**
19+
* @var Notifications
20+
* @inject
21+
*/
22+
public $notifications;
23+
24+
25+
public function checkDefault() {
26+
if (!$this->notificationAcl->canViewCurrent()) {
27+
throw new ForbiddenRequestException();
28+
}
29+
}
30+
31+
/**
32+
* Get all notifications which are currently active.
33+
* @GET
34+
*/
35+
public function actionDefault() {
36+
$notifications = $this->notifications->findAllCurrent();
37+
$notifications = array_filter($notifications,
38+
function (Notification $notification) {
39+
return $this->notificationAcl->canViewDetail($notification);
40+
});
41+
42+
$this->sendSuccessResponse($notifications);
43+
}
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Security\ACL;
4+
5+
use App\Model\Entity\Notification;
6+
7+
interface INotificationPermissions {
8+
function canViewAll(): bool;
9+
function canViewCurrent(): bool;
10+
function canViewDetail(Notification $notification);
11+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Security\Policies;
4+
5+
use App\Model\Entity\Notification;
6+
use App\Security\Authorizator;
7+
use App\Security\Identity;
8+
9+
class NotificationPermissionPolicy implements IPermissionPolicy {
10+
11+
/** @var Authorizator */
12+
private $authorizator;
13+
14+
public function getAssociatedClass() {
15+
return Notification::class;
16+
}
17+
18+
public function __construct(Authorizator $authorizator) {
19+
$this->authorizator = $authorizator;
20+
}
21+
22+
23+
public function hasRole(Identity $identity, Notification $notification) {
24+
$user = $identity->getUserData();
25+
if (!$user) {
26+
return false;
27+
}
28+
29+
// TODO
30+
}
31+
32+
public function isGlobal(Identity $identity, Notification $notification) {
33+
return $notification->getGroups()->isEmpty();
34+
}
35+
36+
public function isGroupsMember(Identity $identity, Notification $notification) {
37+
$user = $identity->getUserData();
38+
if (!$user) {
39+
return false;
40+
}
41+
42+
foreach ($notification->getGroups() as $group) {
43+
$isMember = $group->isMemberOfSubgroup($user);
44+
if ($isMember) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
}

app/config/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ acl:
196196
email: App\Security\ACL\IEmailPermissions
197197
shadowAssignment: App\Security\ACL\IShadowAssignmentPermissions
198198
shadowAssignmentPoints: App\Security\ACL\IShadowAssignmentPointsPermissions
199+
notification: App\Security\ACL\INotificationPermissions
199200
policies:
200201
group: App\Security\Policies\GroupPermissionPolicy
201202
instance: App\Security\Policies\InstancePermissionPolicy
@@ -213,6 +214,7 @@ acl:
213214
sisBoundGroup: App\Security\Policies\SisBoundGroupPermissionPolicy
214215
shadowAssignment: App\Security\Policies\ShadowAssignmentPermissionPolicy
215216
shadowAssignmentPoints: App\Security\Policies\ShadowAssignmentPointsPermissionPolicy
217+
notification: App\Security\Policies\NotificationPermissionPolicy
216218

217219
extensions:
218220
console: Kdyby\Console\DI\ConsoleExtension
@@ -366,6 +368,7 @@ services:
366368
- App\Model\Repository\SisValidTerms
367369
- App\Model\Repository\ShadowAssignments
368370
- App\Model\Repository\ShadowAssignmentPointsRepository
371+
- App\Model\Repository\Notifications
369372

370373
# views factories
371374
- App\Model\View\ExerciseViewFactory

app/config/permissions.neon

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ roles:
1313
- name: empowered-supervisor
1414
parents: supervisor
1515

16-
1716
- name: superadmin
17+
1818
permissions:
1919
- allow: true
2020
role: superadmin
@@ -767,3 +767,25 @@ permissions:
767767
- remove
768768
conditions:
769769
- assignmentPoints.isSupervisor
770+
771+
############################
772+
# Notification permissions #
773+
############################
774+
775+
- allow: true
776+
role: scope-read-all
777+
resource: notification
778+
actions:
779+
- viewAll
780+
- viewCurrent
781+
- viewDetail
782+
783+
- allow: true
784+
resource: notification
785+
actions:
786+
- viewDetail
787+
conditions:
788+
- notification.hasRole
789+
- or:
790+
- notification.isGlobal
791+
- notification.isGroupsMember

app/model/entity/Group.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,26 @@ public function isMemberOf(User $user) {
304304
return $this->getActiveMembers(GroupMembership::TYPE_ALL)->contains($user);
305305
}
306306

307+
/**
308+
* Is member of this group or any subgroup.
309+
* @note Is member or supervisor or admin, whole package of members.
310+
* @param User $user
311+
* @return bool
312+
*/
313+
public function isMemberOfSubgroup(User $user) {
314+
if ($this->isAdminOf($user) || $this->isMemberOf($user)) {
315+
return true;
316+
}
317+
318+
foreach ($this->childGroups as $childGroup) {
319+
if ($childGroup->isMemberOfSubgroup($user)) {
320+
return true;
321+
}
322+
}
323+
324+
return false;
325+
}
326+
307327
/**
308328
* @ORM\ManyToMany(targetEntity="User")
309329
*/

app/model/repository/Notifications.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ class Notifications extends BaseSoftDeleteRepository {
1111
public function __construct(EntityManager $em) {
1212
parent::__construct($em, Notification::class);
1313
}
14+
15+
public function findAllCurrent(): array {
16+
// TODO
17+
}
1418
}

0 commit comments

Comments
 (0)