ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilPRGPermissionsHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
31 public const ORGU_OPERATIONS = [
37 ];
38
39 public const ROLEPERM_VIEW = 'rp_visible';
40 public const ROLEPERM_READ = 'rp_read';
41 public const ROLEPERM_WRITE = 'rp_write';
42 //both org-unit and rbac permission read "manage_members";
43 //however, rbac-manage_members does include all of the orgu-permissions listed here.
44 public const ROLEPERM_MANAGE_MEMBERS = 'rp_manage_members';
45
46 private const ROLEMAPPINGS = [
47 'rp_visible' => 'visible',
48 'rp_read' => 'read',
49 'rp_write' => 'write',
50 'rp_manage_members' => 'manage_members'
51 ];
52
53 protected ilAccess $access;
57 protected int $prg_ref_id;
58 protected array $cache = [];
59
63 protected array $user_id_cache;
64
65 public function __construct(
70 int $prg_ref_id
71 ) {
72 $this->access = $access;
73 $this->orgu_settings = $orgu_settings;
74 $this->data_cache = $data_cache;
75 $this->orgu_access = $orgu_access;
76 $this->prg_ref_id = $prg_ref_id;
77 }
78
79 public function may(string $operation): bool
80 {
81 $this->throwForInvalidOperation($operation);
82 if (in_array($operation, self::ORGU_OPERATIONS)) {
83 return
84 $this->access->checkAccess( //RBAC overrides OrgUs
85 self::ROLEMAPPINGS[self::ROLEPERM_MANAGE_MEMBERS],
86 '',
87 $this->getProgrammeRefId()
88 )
89 || $this->access->checkPositionAccess($operation, $this->getProgrammeRefId());
90 }
91
92 return $this->access->checkAccess(self::ROLEMAPPINGS[$operation], '', $this->getProgrammeRefId());
93 }
94
98 public function mayAnyOf(array $operations): bool
99 {
100 foreach ($operations as $operation) {
101 if ($this->may($operation)) {
102 return true;
103 }
104 }
105 return false;
106 }
107
111 public function getUserIdsSusceptibleTo(string $operation): array
112 {
113 $this->throwForInvalidOperation($operation);
114
115 if ($this->may(self::ROLEPERM_MANAGE_MEMBERS)) { //RBAC overrides OrgUs
116 return $this->getAllAssignedUserIds();
117 }
118
119 if (in_array($operation, self::ORGU_OPERATIONS) && $this->may($operation)) {
120 return $this->getUserIdsInPrgAccessibleForOperation($operation);
121 }
122 return [];
123 }
124
128 public function filterUserIds(array $user_ids, string $operation): array
129 {
130 if ($this->may(self::ROLEPERM_MANAGE_MEMBERS)) { //RBAC overrides OrgUs
131 return $user_ids;
132 }
133
134 return $this->orgu_access->filterUserIdsByPositionOfCurrentUser(
135 $operation,
136 $this->getProgrammeRefId(),
137 $user_ids
138 );
139 }
140
141 protected function throwForInvalidOperation(string $operation): void
142 {
143 $valid = array_merge(
144 self::ORGU_OPERATIONS,
145 [
146 self::ROLEPERM_VIEW,
147 self::ROLEPERM_READ,
148 self::ROLEPERM_WRITE,
149 self::ROLEPERM_MANAGE_MEMBERS
150 ]
151 );
152
153 if (!in_array($operation, $valid)) {
154 throw new ilException('prg does not provide this permission: ' . $operation);
155 }
156 }
157
158 protected function getUserIdsInPrgAccessibleForOperation(string $orgu_operation): array
159 {
160 if (!isset($this->cache[$orgu_operation])) {
161 $user_ids = array_map(
162 'intval',
163 $this->orgu_access->filterUserIdsByPositionOfCurrentUser(
164 $orgu_operation,
165 $this->getProgrammeRefId(),
166 $this->getAllAssignedUserIds()
167 )
168 );
169 $this->cache[$orgu_operation] = array_unique($user_ids);
170 }
171 return $this->cache[$orgu_operation];
172 }
173
177 protected function getAllAssignedUserIds(): array
178 {
179 if (!isset($this->cache[self::ROLEPERM_MANAGE_MEMBERS])) {
181 $this->cache[self::ROLEPERM_MANAGE_MEMBERS] = array_unique($prg->getMembers());
182 }
183 return $this->cache[self::ROLEPERM_MANAGE_MEMBERS];
184 }
185
186 protected function getProgrammeRefId(): int
187 {
188 return $this->prg_ref_id;
189 }
190
191 public function isOrguAccessEnabledGlobally(): bool
192 {
193 $obj_id = $this->data_cache->lookupObjId($this->getProgrammeRefId());
194 $type_settings = $this->orgu_settings->getObjectPositionSettingsByType('prg');
195
196 return $type_settings->isActive() && $type_settings->isChangeableForObject();
197 }
198}
Class ilAccessHandler Checks access for ILIAS objects.
Base class for ILIAS Exception handling.
class ilObjectDataCache
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Both role and OrgU-based permissions are relevant in many places of the PRG.
ilOrgUnitPositionAccess $orgu_access
throwForInvalidOperation(string $operation)
getUserIdsInPrgAccessibleForOperation(string $orgu_operation)
getUserIdsSusceptibleTo(string $operation)
ilOrgUnitGlobalSettings $orgu_settings
__construct(ilAccess $access, ilOrgUnitGlobalSettings $orgu_settings, ilObjectDataCache $data_cache, ilOrgUnitPositionAccess $orgu_access, int $prg_ref_id)
filterUserIds(array $user_ids, string $operation)
$valid