18 declare(strict_types=1);
44 $this->db = $DIC->database();
45 $this->rbacreview = $DIC->rbac()->review();
46 $this->
logger = $DIC->logger()->ac();
49 public function setBlockedStatus(
int $a_role_id,
int $a_ref_id,
bool $a_blocked_status): void
52 $query =
'UPDATE rbac_fa set blocked = ' . $this->db->quote($a_blocked_status,
'integer') .
' ' .
53 'WHERE rol_id = ' . $this->db->quote($a_role_id,
'integer') .
' ' .
54 'AND parent = ' . $this->db->quote($a_ref_id,
'integer');
55 $this->db->manipulate(
$query);
64 foreach ($this->rbacreview->assignedRoles($a_usr_id) as $role_id) {
67 $query =
"DELETE FROM rbac_ua WHERE usr_id = " . $this->db->quote($a_usr_id,
'integer');
74 public function deleteRole(
int $a_rol_id,
int $a_ref_id): void
78 throw new DomainException(
'System administrator role is not deletable.');
82 $mapping->deleteRole($a_rol_id);
88 $query =
"DELETE FROM rbac_ua " .
89 "WHERE rol_id = " . $this->db->quote($a_rol_id,
'integer');
93 $query =
"DELETE FROM rbac_pa " .
94 "WHERE rol_id = " . $this->db->quote($a_rol_id,
'integer') .
" ";
106 $query =
'DELETE FROM rbac_templates ' .
107 'WHERE rol_id = ' . $this->db->quote($a_obj_id,
'integer');
110 $query =
'DELETE FROM rbac_fa ' .
111 'WHERE rol_id = ' . $this->db->quote($a_obj_id,
'integer');
123 $this->
logger->notice(
'System administrator role is not deletable.');
128 if ($a_ref_id != 0) {
129 $clause =
'AND parent = ' . $this->db->quote($a_ref_id,
'integer') .
' ';
132 $query =
'DELETE FROM rbac_fa ' .
133 'WHERE rol_id = ' . $this->db->quote($a_rol_id,
'integer') .
' ' .
137 $query =
'DELETE FROM rbac_templates ' .
138 'WHERE rol_id = ' . $this->db->quote($a_rol_id,
'integer') .
' ' .
147 array $a_limited_roles = []
151 $ilAtomQuery->addTableLock(
'rbac_ua');
152 $ilAtomQuery->addQueryCallable(
153 function (
ilDBInterface $ilDB) use (&$ret, $a_role_id, $a_usr_id, $a_limit, $a_limited_roles):
void {
155 $limit_query =
'SELECT COUNT(*) num FROM rbac_ua ' .
156 'WHERE ' . $ilDB->
in(
'rol_id', (array) $a_limited_roles,
false,
'integer');
159 if ($row->num >= $a_limit) {
164 $query =
"INSERT INTO rbac_ua (usr_id, rol_id) " .
166 $ilDB->
quote($a_usr_id,
'integer') .
"," . $ilDB->
quote($a_role_id,
'integer') .
177 $this->rbacreview->setAssignedCacheEntry($a_role_id, $a_usr_id,
true);
179 $mapping->assign($a_role_id, $a_usr_id);
187 public function assignUser(
int $a_rol_id,
int $a_usr_id): void
190 $alreadyAssigned = $this->rbacreview->isAssigned($a_usr_id, $a_rol_id);
193 if (!$alreadyAssigned) {
194 $query =
"INSERT INTO rbac_ua (usr_id, rol_id) " .
195 "VALUES (" . $this->db->quote($a_usr_id,
'integer') .
"," . $this->db->quote(
201 $this->rbacreview->setAssignedCacheEntry($a_rol_id, $a_usr_id,
true);
205 $mapping->assign($a_rol_id, $a_usr_id);
207 $ref_id = $this->rbacreview->getObjectReferenceOfRole($a_rol_id);
211 if (!$alreadyAssigned) {
213 $GLOBALS[
'DIC'][
'ilAppEventHandler']->raise(
214 'Services/AccessControl',
218 'usr_id' => $a_usr_id,
219 'role_id' => $a_rol_id,
231 $query =
"DELETE FROM rbac_ua " .
232 "WHERE usr_id = " . $this->db->quote($a_usr_id,
'integer') .
" " .
233 "AND rol_id = " . $this->db->quote($a_rol_id,
'integer') .
" ";
236 $this->rbacreview->setAssignedCacheEntry($a_rol_id, $a_usr_id,
false);
239 $mapping->deassign($a_rol_id, $a_usr_id);
242 $ref_id =
$GLOBALS[
'DIC'][
'rbacreview']->getObjectReferenceOfRole($a_rol_id);
247 $GLOBALS[
'DIC'][
'ilAppEventHandler']->raise(
'Services/AccessControl',
'deassignUser', array(
249 'usr_id' => $a_usr_id,
250 'role_id' => $a_rol_id,
268 foreach ($a_ops as
$key => $operation) {
269 $a_ops[
$key] = (
int) $operation;
272 $ops_ids = serialize($a_ops);
274 $query =
'DELETE FROM rbac_pa ' .
275 'WHERE rol_id = %s ' .
277 $res = $this->db->queryF(
279 array(
'integer',
'integer'),
280 array($a_rol_id, $a_ref_id)
287 $query =
"INSERT INTO rbac_pa (rol_id,ops_id,ref_id) " .
289 "(" . $this->db->quote($a_rol_id,
'integer') .
"," . $this->db->quote(
292 ) .
"," . $this->db->quote($a_ref_id,
'integer') .
")";
301 public function revokePermission(
int $a_ref_id,
int $a_rol_id = 0,
bool $a_keep_protected =
true): void
309 if ($a_keep_protected !=
true) {
311 $and1 =
" AND rol_id = " . $this->db->quote($a_rol_id,
'integer') .
" ";
316 $query =
"DELETE FROM rbac_pa " .
317 "WHERE ref_id = " . $this->db->quote($a_ref_id,
'integer') .
326 $roles_in_scope = $this->rbacreview->getParentRoleIds($a_ref_id);
331 foreach ($roles_in_scope as $role) {
332 if ($role[
'protected'] ==
true) {
336 $role_ids[] = $role[
'obj_id'];
340 if ($role_ids === []) {
344 $query =
'DELETE FROM rbac_pa ' .
345 'WHERE ' . $this->db->in(
'rol_id', $role_ids,
false,
'integer') .
' ' .
346 'AND ref_id = ' . $this->db->quote($a_ref_id,
'integer');
350 if ($roles_in_scope[$a_rol_id][
'protected'] ==
true) {
354 $query =
"DELETE FROM rbac_pa " .
355 "WHERE ref_id = " . $this->db->quote($a_ref_id,
'integer') .
" " .
356 "AND rol_id = " . $this->db->quote($a_rol_id,
'integer') .
" ";
366 $query =
'DELETE FROM rbac_pa ' .
368 '( ' .
$GLOBALS[
'DIC'][
'tree']->getSubTreeQuery($a_ref_id, array(
'child')) .
' ) ' .
369 'AND rol_id = ' . $this->db->quote($a_role_id,
'integer');
371 $this->db->manipulate(
$query);
379 $query =
'DELETE FROM rbac_templates ' .
380 'WHERE parent IN ( ' .
381 $GLOBALS[
'DIC'][
'tree']->getSubTreeQuery($a_ref_id, array(
'child')) .
' ) ' .
382 'AND rol_id = ' . $this->db->quote($a_rol_id,
'integer');
384 $this->db->manipulate(
$query);
386 $query =
'DELETE FROM rbac_fa ' .
387 'WHERE parent IN ( ' .
388 $GLOBALS[
'DIC'][
'tree']->getSubTreeQuery($a_ref_id, array(
'child')) .
' ) ' .
389 'AND rol_id = ' . $this->db->quote($a_rol_id,
'integer');
391 $this->db->manipulate(
$query);
404 $query =
"DELETE FROM rbac_pa " .
405 "WHERE " . $this->db->in(
'ref_id', $a_ref_ids,
false,
'integer') .
' ' .
406 "AND rol_id = " . $this->db->quote($a_rol_id,
'integer');
415 int $a_source_parent,
418 bool $a_consider_protected =
true 426 $a_consider_protected
428 $ops = $this->rbacreview->getRoleOperationsOnObject($a_source_id, $a_source_parent);
440 int $a_source_parent,
443 bool $a_consider_protected =
true 452 $query =
'SELECT * FROM rbac_templates ' .
453 'WHERE rol_id = ' . $this->db->quote($a_source_id,
'integer') .
' ' .
454 'AND parent = ' . $this->db->quote($a_source_parent,
'integer');
458 while ($row = $this->db->fetchObject(
$res)) {
459 $operations[$rownum][
'type'] = $row->type;
460 $operations[$rownum][
'ops_id'] = $row->ops_id;
465 $query =
'DELETE FROM rbac_templates WHERE rol_id = ' . $this->db->quote($a_dest_id,
'integer') .
' ' .
466 'AND parent = ' . $this->db->quote($a_dest_parent,
'integer');
469 foreach ($operations as $op) {
470 $query =
'INSERT INTO rbac_templates (rol_id,type,ops_id,parent) ' .
472 $this->db->quote($a_dest_id,
'integer') .
"," .
473 $this->db->quote($op[
'type'],
'text') .
"," .
474 $this->db->quote($op[
'ops_id'],
'integer') .
"," .
475 $this->db->quote($a_dest_parent,
'integer') .
")";
476 $this->db->manipulate(
$query);
480 if ($a_consider_protected ==
true) {
481 if ($this->rbacreview->isProtected($a_source_parent, $a_source_id)) {
493 int $a_source1_parent,
495 int $a_source2_parent,
504 $query =
"SELECT s1.type, s1.ops_id " .
505 "FROM rbac_templates s1, rbac_templates s2 " .
506 "WHERE s1.rol_id = " . $this->db->quote($a_source1_id,
'integer') .
" " .
507 "AND s1.parent = " . $this->db->quote($a_source1_parent,
'integer') .
" " .
508 "AND s2.rol_id = " . $this->db->quote($a_source2_id,
'integer') .
" " .
509 "AND s2.parent = " . $this->db->quote($a_source2_parent,
'integer') .
" " .
510 "AND s1.type = s2.type " .
511 "AND s1.ops_id = s2.ops_id";
517 $operations[$rowNum][
'type'] = $row->type;
518 $operations[$rowNum][
'ops_id'] = $row->ops_id;
524 $query =
'DELETE FROM rbac_templates WHERE rol_id = ' . $this->db->quote($a_dest_id,
'integer') .
' ' .
525 'AND parent = ' . $this->db->quote($a_dest_parent,
'integer');
528 $query =
'INSERT INTO rbac_templates (rol_id,type,ops_id,parent) ' .
530 $sta = $this->db->prepareManip(
$query, array(
'integer',
'text',
'integer',
'integer'));
531 foreach ($operations as $set) {
532 $this->db->execute($sta, array(
543 int $a_source1_parent,
545 int $a_source2_parent,
555 $s1_ops = $this->rbacreview->getAllOperationsOfRole($a_source1_id, $a_source1_parent);
556 $s2_ops = $this->rbacreview->getAllOperationsOfRole($a_source2_id, $a_source2_parent);
559 foreach ($s1_ops as
$type => $ops) {
560 foreach ($ops as $op) {
563 $query =
'INSERT INTO rbac_templates (rol_id,type,ops_id,parent) ' .
565 $this->db->quote($a_dest_id,
'integer') .
', ' .
566 $this->db->quote(
$type,
'text') .
', ' .
567 $this->db->quote($op,
'integer') .
', ' .
568 $this->db->quote($a_dest_parent,
'integer') .
' ' .
570 $this->db->manipulate(
$query);
575 foreach ($s2_ops as
$type => $ops) {
576 foreach ($ops as $op) {
577 if (!isset($s1_ops[
$type]) || !in_array($op, $s1_ops[$type])) {
578 $query =
'INSERT INTO rbac_templates (rol_id,type,ops_id,parent) ' .
580 $this->db->quote($a_dest_id,
'integer') .
', ' .
581 $this->db->quote($type,
'text') .
', ' .
582 $this->db->quote($op,
'integer') .
', ' .
583 $this->db->quote($a_dest_parent,
'integer') .
' ' .
585 $this->db->manipulate(
$query);
596 int $a_source_parent,
604 $s1_ops = $this->rbacreview->getAllOperationsOfRole($a_source_id, $a_source_parent);
605 $d_ops = $this->rbacreview->getAllOperationsOfRole($a_dest_id, $a_dest_parent);
607 foreach ($s1_ops as
$type => $ops) {
608 foreach ($ops as $op) {
609 if (isset($d_ops[
$type]) && in_array($op, $d_ops[$type])) {
610 $query =
'DELETE FROM rbac_templates ' .
611 'WHERE rol_id = ' . $this->db->quote($a_dest_id,
'integer') .
' ' .
612 'AND type = ' . $this->db->quote($type,
'text') .
' ' .
613 'AND ops_id = ' . $this->db->quote($op,
'integer') .
' ' .
614 'AND parent = ' . $this->db->quote($a_dest_parent,
'integer');
615 $this->db->manipulate(
$query);
629 ?
string $a_type = null
636 if ($a_type !== null) {
637 $and_type =
" AND type=" . $this->db->quote($a_type,
'text') .
" ";
639 $query =
'DELETE FROM rbac_templates ' .
640 'WHERE rol_id = ' . $this->db->quote($a_rol_id,
'integer') .
' ' .
641 'AND parent = ' . $this->db->quote($a_ref_id,
'integer') .
' ' .
650 public function setRolePermission(
int $a_rol_id,
string $a_type, array $a_ops,
int $a_ref_id): void
653 $this->
logger->logStack();
656 foreach ($a_ops as $op) {
660 'rol_id' => [
'integer', $a_rol_id],
661 'type' => [
'text', $a_type],
662 'ops_id' => [
'integer', $op],
663 'parent' => [
'integer', $a_ref_id]
680 string $a_assign =
"y" 687 if ($a_assign !=
"y") {
692 $query =
'SELECT rol_id FROM rbac_fa ' .
693 'WHERE rol_id = ' . $this->db->quote($a_rol_id,
'integer') .
' ' .
694 'AND parent = ' . $this->db->quote($a_parent,
'integer');
696 if (
$res->numRows()) {
700 'INSERT INTO rbac_fa (rol_id, parent, assign, protected) ' .
701 'VALUES (%s,%s,%s,%s)',
702 $this->db->quote($a_rol_id,
'integer'),
703 $this->db->quote($a_parent,
'integer'),
704 $this->db->quote($a_assign,
'text'),
705 $this->db->quote(
'n',
'text')
716 $query =
"INSERT INTO rbac_ta (typ_id, ops_id) " .
717 "VALUES(" . $this->db->quote($a_type_id,
'integer') .
"," . $this->db->quote($a_ops_id,
'integer') .
")";
727 $query =
"DELETE FROM rbac_ta " .
728 "WHERE typ_id = " . $this->db->quote($a_type_id,
'integer') .
" " .
729 "AND ops_id = " . $this->db->quote($a_ops_id,
'integer');
736 public function setProtected(
int $a_ref_id,
int $a_role_id,
string $a_value): void
740 $query =
'UPDATE rbac_fa ' .
741 'SET protected = ' . $this->db->quote($a_value,
'text') .
' ' .
742 'WHERE rol_id = ' . $this->db->quote($a_role_id,
'integer');
754 foreach ($this->rbacreview->getRolesOfRoleFolder($a_source_id,
false) as $role_data) {
756 if (substr($title, 0, 3) ==
'il_') {
759 $real_local[] = $role_data;
761 if ($real_local === []) {
765 foreach ($real_local as $role) {
770 $roleObj->setTitle($orig->getTitle());
771 $roleObj->setDescription($orig->getDescription());
772 $roleObj->setImportId($orig->getImportId());
785 int $a_template_parent
787 if ($this->rbacreview->isProtected($a_role_parent, $a_role_id)) {
789 $new_ops = $this->rbacreview->getOperationsOfRole(
803 if (!$a_template_id) {
825 $new_ops = $this->rbacreview->getOperationsOfRole(
857 $action->setRefId($a_ref_id);
872 $tree = $DIC[
'tree'];
874 $new_parent = $tree->getParentId($ref_id);
875 $old_context_roles = $this->rbacreview->getParentRoleIds($old_parent,
false);
876 $new_context_roles = $this->rbacreview->getParentRoleIds($new_parent,
false);
883 $tree->useCache(
false);
885 $for_addition = $for_deletion = [];
886 foreach ($new_context_roles as $new_role_id => $new_role) {
887 if (!isset($old_context_roles[$new_role_id])) {
888 $for_addition[] = $new_role_id;
889 } elseif ($new_role[
'parent'] != $old_context_roles[$new_role_id][
'parent']) {
891 $for_deletion[] = $new_role_id;
892 $for_addition[] = $new_role_id;
895 foreach ($old_context_roles as $old_role_id => $old_role) {
896 if (!isset($new_context_roles[$old_role_id])) {
897 $for_deletion[] = $old_role_id;
900 if ($for_deletion === [] && $for_addition === []) {
906 if ($rbac_log_active) {
907 $role_ids = array_unique(array_merge(array_keys($for_deletion), array_keys($for_addition)));
910 foreach ($tree->getSubTree($tree->getNodeData($ref_id),
true) as $node_data) {
911 $node_id = (
int) $node_data[
'child'];
912 if ($rbac_log_active) {
919 if (!($node_data[
'type'] ??
false)) {
926 foreach ($for_deletion as $role_id) {
930 foreach ($for_addition as $role_id) {
931 $role_parent_id = $this->rbacreview->getParentOfRole($role_id, $ref_id);
932 switch ($node_data[
'type']) {
959 $this->rbacreview->getOperationsOfRole($role_id, $node_data[
'type'], $role_parent_id),
967 if ($rbac_log_active) {
removeUser(int $a_usr_id)
deletes a user from rbac_ua all user <-> role relations are deleted
static gatherFaPa(int $a_ref_id, array $a_role_ids, bool $a_add_action=false)
grantPermission(int $a_rol_id, array $a_ops, int $a_ref_id)
Grants a permission to an object and a specific role.
adjustMovedObjectPermissions(int $ref_id, int $old_parent)
Adjust permissions of moved objects.
setRolePermission(int $a_rol_id, string $a_type, array $a_ops, int $a_ref_id)
Inserts template permissions in rbac_templates for an specific object type.
deleteRolePermission(int $a_rol_id, int $a_ref_id, ?string $a_type=null)
Deletes all entries of a template.
deleteSubtreeTemplates(int $a_ref_id, int $a_rol_id)
Delete all template permissions of subtree nodes.
static getLogger(string $a_component_id)
Get component logger.
deassignOperationFromObject(int $a_type_id, int $a_ops_id)
Deassign an existing operation from an object Update of rbac_ta.
applyMovedObjectDidacticTemplates(int $a_ref_id, int $a_old_parent)
Apply didactic templates after object movement.
static lookupCourseNonMemberTemplatesId()
copyRoleTemplatePermissions(int $a_source_id, int $a_source_parent, int $a_dest_parent, int $a_dest_id, bool $a_consider_protected=true)
Copies template permissions of one role to another.
revokePermissionList(array $a_ref_ids, int $a_rol_id)
Revokes permissions of a LIST of objects of ONE role.
assignUser(int $a_rol_id, int $a_usr_id)
Assigns an user to a role.
quote($value, string $type)
revokePermission(int $a_ref_id, int $a_rol_id=0, bool $a_keep_protected=true)
Revokes permissions of an object of one role.
revokeSubtreePermissions(int $a_ref_id, int $a_role_id)
Revoke subtree permissions.
static lookupGroupStatusTemplateId(int $a_obj_id)
static diffFaPa(array $a_old, array $a_new)
copyRolePermissions(int $a_source_id, int $a_source_parent, int $a_dest_parent, int $a_dest_id, bool $a_consider_protected=true)
Copies template permissions and permission of one role to another.
static _lookupObjId(int $ref_id)
static _getInstance()
Get singleton instance of this class.
static lookupTemplateId(int $a_ref_id)
static add(int $a_action, int $a_ref_id, array $a_diff, bool $a_source_ref_id=false)
static _lookupTitle(int $obj_id)
deleteTemplate(int $a_obj_id)
Deletes a template from role folder and deletes all entries in rbac_templates, rbac_fa.
initIntersectionPermissions(int $a_ref_id, int $a_role_id, int $a_role_parent, int $a_template_id, int $a_template_parent)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
copyLocalRoles(int $a_source_id, int $a_target_id)
Copy local roles This method creates a copy of all local role.
query(string $query)
Run a (read-only) Query on the database.
copyRolePermissionIntersection(int $a_source1_id, int $a_source1_parent, int $a_source2_id, int $a_source2_parent, int $a_dest_parent, int $a_dest_id)
Copies the intersection of the template permissions of two roles to a third role. ...
__construct()
Constructor public.
setProtected(int $a_ref_id, int $a_role_id, string $a_value)
Set protected.
assignRoleToFolder(int $a_rol_id, int $a_parent, string $a_assign="y")
Assigns a role to a role folder A role folder is an object to store roles.
in(string $field, array $values, bool $negate=false, string $type="")
deleteLocalRole(int $a_rol_id, int $a_ref_id=0)
Deletes a local role and entries in rbac_fa and rbac_templates.
deleteRole(int $a_rol_id, int $a_ref_id)
Deletes a role and deletes entries in rbac_pa, rbac_templates, rbac_ua, rbac_fa.
static getActionsByTemplateId(int $a_tpl_id)
Get actions of one template.
represents a creation of local roles action
assignUserLimited(int $a_role_id, int $a_usr_id, int $a_limit, array $a_limited_roles=[])
copyRolePermissionSubtract(int $a_source_id, int $a_source_parent, int $a_dest_id, int $a_dest_parent)
Subtract role permissions.
Class ilRbacAdmin Core functions for role based access control.
assignOperationToObject(int $a_type_id, int $a_ops_id)
Assign an existing operation to an object Update of rbac_ta.
manipulate(string $query)
Run a (write) Query on the database.
static _lookupType(int $id, bool $reference=false)
setBlockedStatus(int $a_role_id, int $a_ref_id, bool $a_blocked_status)
copyRolePermissionUnion(int $a_source1_id, int $a_source1_parent, int $a_source2_id, int $a_source2_parent, int $a_dest_id, int $a_dest_parent)
deassignUser(int $a_rol_id, int $a_usr_id)
Deassigns a user from a role.