19 declare(strict_types=1);
64 $this->
user = $DIC->user();
65 $this->db = $DIC->database();
66 $this->review = $DIC->rbac()->review();
68 $this->tree = $DIC->repositoryTree();
69 $this->
http = $DIC->http();
75 if (self::$instance ===
null) {
76 self::$instance =
new self();
78 return self::$instance;
86 self::$user_role_cache = [];
88 self::$_checkAccessOfUserCache = [];
108 public function checkAccess(
string $a_operations,
int $a_ref_id,
string $a_type =
""): bool
113 public function checkAccessOfUser(
int $a_user_id,
string $a_operations,
int $a_ref_id,
string $a_type =
""): bool
116 $cacheKey = $a_user_id .
':' . $a_operations .
':' . $a_ref_id .
':' . $a_type;
119 if (!is_array(self::$_checkAccessOfUserCache)) {
120 self::$_checkAccessOfUserCache = [];
123 if (array_key_exists($cacheKey, self::$_checkAccessOfUserCache)) {
124 return self::$_checkAccessOfUserCache[$cacheKey];
134 if (count(self::$_checkAccessOfUserCache) < self::MAX_CACHE_ENTRIES) {
135 self::$_checkAccessOfUserCache[$cacheKey] =
true;
147 if (count(self::$_checkAccessOfUserCache) < self::MAX_CACHE_ENTRIES) {
148 self::$_checkAccessOfUserCache[$cacheKey] =
true;
154 $paCacheKey = $a_user_id .
':' . $a_ref_id;
155 if (!is_array(self::$_paCache)) {
156 self::$_paCache = [];
159 if (array_key_exists($paCacheKey, self::$_paCache)) {
161 $ops = self::$_paCache[$paCacheKey];
164 $q =
"SELECT * FROM rbac_pa " .
165 "WHERE ref_id = " . $this->db->quote($a_ref_id,
'integer');
167 $r = $this->db->query(
$q);
171 if ($row->ops_id ===
':') {
174 if (in_array((
int) $row->rol_id, $roles)) {
175 $ops = array_merge($ops, unserialize(stripslashes($row->ops_id)));
179 if (count(self::$_paCache) < self::MAX_CACHE_ENTRIES) {
180 self::$_paCache[$paCacheKey] = $ops;
184 $operations = explode(
",", $a_operations);
185 foreach ($operations as $operation) {
186 if ($operation ==
"create") {
187 if (empty($a_type)) {
188 throw new DomainException(
189 'checkAccess(): ' .
"Expect a type definition for checking a 'create' permission" 196 if (!in_array($ops_id, (array) $ops)) {
197 if (count(self::$_checkAccessOfUserCache) < self::MAX_CACHE_ENTRIES) {
198 self::$_checkAccessOfUserCache[$cacheKey] =
false;
206 if (count(self::$_checkAccessOfUserCache) < self::MAX_CACHE_ENTRIES) {
208 self::$_checkAccessOfUserCache[$cacheKey] =
true;
217 foreach ($a_ref_ids as
$ref_id) {
218 if (!isset(self::$_paCache[$a_user_id .
":" . $ref_id])) {
225 if ($ref_ids !== []) {
227 $q =
"SELECT * FROM rbac_pa " .
228 "WHERE " . $this->db->in(
"ref_id", $ref_ids,
false,
"integer");
230 $r = $this->db->query(
$q);
233 if($row->ops_id ===
':') {
236 if (in_array($row->rol_id, $roles[(
int) $row->ref_id])) {
237 $ops[(
int) $row->ref_id] = array_merge(
238 $ops[(
int) $row->ref_id],
239 unserialize($row->ops_id)
243 foreach ($a_ref_ids as $ref_id) {
245 if (!isset(self::$_paCache[$a_user_id .
":" . $ref_id])) {
255 public function checkPermission(
int $a_ref_id,
int $a_rol_id,
string $a_operation): bool
258 $query_rbac_operations =
'SELECT ops_id FROM rbac_operations ' .
259 'WHERE operation = ' . $this->db->quote($a_operation,
'text');
260 $res_rbac_operations = $this->db->query($query_rbac_operations);
262 while ($row = $this->db->fetchObject($res_rbac_operations)) {
263 $ops_id = (
int) $row->ops_id;
266 $query_rbac_pa =
"SELECT * FROM rbac_pa " .
267 "WHERE rol_id = " . $this->db->quote($a_rol_id,
'integer') .
" " .
268 "AND ref_id = " . $this->db->quote($a_ref_id,
'integer') .
" ";
269 $res_rbac_pa = $this->db->query($query_rbac_pa);
271 while ($row = $this->db->fetchObject($res_rbac_pa)) {
272 if ($row->ops_id ===
':') {
275 $ops = array_merge($ops, unserialize($row->ops_id));
277 return in_array($ops_id, $ops);
283 if (($this->mem_view[
'active'] ??
null) and $a_user_id == $this->
user->getId()) {
284 if (in_array($a_ref_id, $this->mem_view[
'items'])) {
285 return $a_operations;
289 if ($a_user_id != $this->
objectDataCache->lookupOwner($this->objectDataCache->lookupObjId($a_ref_id))) {
290 return $a_operations;
294 foreach (explode(
",", $a_operations) as $operation) {
295 if ($operation !=
'cat_administrate_users' &&
296 $operation !=
'edit_permission' &&
297 $operation !=
'edit_learning_progress' &&
298 $operation !=
'read_learning_progress' &&
299 !preg_match(
'/^create/', $operation) &&
300 $operation !=
'read_outcomes' 304 if (!strlen($new_ops)) {
305 $new_ops = $operation;
307 $new_ops .= (
',' . $operation);
320 if (isset($this->mem_view[
'active']) && $this->mem_view[
'active'] && $a_usr_id == $this->
user->getId()) {
322 if (in_array($a_ref_id, $this->mem_view[
'items']) && $this->mem_view[
'role']) {
324 return [$this->mem_view[
'role']];
328 if (isset(self::$user_role_cache[$a_usr_id]) and is_array(self::$user_role_cache)) {
329 return self::$user_role_cache[$a_usr_id];
331 return self::$user_role_cache[$a_usr_id] = $this->review->assignedRoles($a_usr_id);
337 $member_view_activation =
null;
338 if ($this->
http->wrapper()->query()->has(
'mv')) {
339 $member_view_activation = $this->
http->wrapper()->query()->retrieve(
345 if ($this->
http->wrapper()->query()->has(
'ref_id')) {
346 $ref_id = $this->
http->wrapper()->query()->retrieve(
351 if ($member_view_activation ===
true) {
353 $settings->toggleActivation(
$ref_id,
true);
357 if ($member_view_activation ===
false) {
358 $settings->toggleActivation(
$ref_id,
false);
360 if (!$settings->isActive()) {
361 $this->mem_view[
'active'] =
false;
362 $this->mem_view[
'items'] = [];
363 $this->mem_view[
'role'] = 0;
365 $this->mem_view[
'active'] =
true;
366 $this->mem_view[
'items'] = $this->tree->getSubTreeIds($settings->getContainer());
367 $this->mem_view[
'items'] = array_merge($this->mem_view[
'items'], [$settings->getContainer()]);
374 if (!in_array($a_role_id, self::$user_role_cache[$a_usr_id])) {
375 self::$user_role_cache[$a_usr_id][] = $a_role_id;
381 $paCacheKey = $a_usr_id .
':' . $a_ref_id;
382 unset(self::$_paCache[$paCacheKey]);
preloadRbacPaCache(array $a_ref_ids, int $a_user_id)
filterOwnerPermissions(int $a_user_id, string $a_operations, int $a_ref_id)
static getDefaultMemberRole(int $a_ref_id)
static resetCaches()
Reset internal caches.
checkAccessOfUser(int $a_user_id, string $a_operations, int $a_ref_id, string $a_type="")
checkPermission(int $a_ref_id, int $a_rol_id, string $a_operation)
check if a specific role has the permission '$a_operation' of an object
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
checkAccess(string $a_operations, int $a_ref_id, string $a_type="")
checkAccess represents the main method of the RBAC-system in ILIAS3 developers want to use With this ...
static http()
Fetches the global http state from ILIAS.
addTemporaryRole(int $a_usr_id, int $a_role_id)
ilObjectDataCache $objectDataCache
static ilRbacSystem $instance
static _getOperationIdByName(string $a_operation)
get operation id by name of operation
static array $_checkAccessOfUserCache
resetPACache(int $a_usr_id, int $a_ref_id)
static array $user_role_cache
__construct()
Constructor.
fetchAssignedRoles(int $a_usr_id, int $a_ref_id)
Fetch assigned roles This method caches the assigned roles per user.