ILIAS  release_8 Revision v8.23
class.ilDclTableHelper.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  protected int $obj_id = 0;
22  protected int $ref_id = 0;
24  protected ilObjUser $user;
26 
30  public function __construct(
31  int $obj_id,
32  int $ref_id,
33  ilRbacReview $rbac_review,
34  ilObjUser $user,
35  ilDBInterface $database
36  ) {
37  $this->obj_id = $obj_id;
38  $this->ref_id = $ref_id;
39  $this->rbac_review = $rbac_review;
40  $this->user = $user;
41  $this->database = $database;
42  }
43 
45  {
46  $visible_tables_for_data_collection = $this->getAllVisibleTablesForDataColleciton();
47  $standard_views_for_data_collection = $this->getStandardViewsByVisibleTables($visible_tables_for_data_collection);
48 
49  $roles_ids = $this->getRolesIdsByViews($standard_views_for_data_collection);
50 
51  $roles_with_read_acces_ids = $this->getRolesIdsWithReadAccessOnDataCollection();
52 
53  //check if there are roles with rbac read right on the datacollection but without read right on any table view
54  $roles_with_no_read_right_on_any_standard_view = array_diff($roles_with_read_acces_ids, $roles_ids);
55 
56  $roles_data = $this->rbac_review->getRolesForIDs($roles_with_no_read_right_on_any_standard_view, true);
57  $role_titles = [];
58  if (!empty($roles_data)) {
59  foreach ($roles_data as $role_data) {
60  $role_titles[] = $role_data['title'];
61  }
62  }
63 
64  return $role_titles;
65  }
66 
67  protected function getRolesIdsWithReadAccessOnDataCollection(): array
68  {
69  $rbac_roles = $this->rbac_review->getParentRoleIds($this->ref_id);
70  $roles_with_read_acces_ids = [];
71  //get all roles with read access on data collection
72  foreach ($rbac_roles as $role) {
73  $operations = $this->rbac_review->getActiveOperationsOfRole($this->ref_id, $role['rol_id']);
74  //3 corresponds to the read rbac right
75  if (!empty($operations) && in_array(3, $operations)) {
76  $roles_with_read_acces_ids[] = $role['rol_id'];
77  }
78  }
79 
80  return $roles_with_read_acces_ids;
81  }
82 
83  protected function getRolesIdsByViews(array $views_for_data_collection): array
84  {
85  $roles_ids = [];
90  foreach ($views_for_data_collection as $key => $view_for_data_collection_array_of_objects) {
91  foreach ($view_for_data_collection_array_of_objects as $view_for_data_collection_object) {
92  $ilDclTableView = ilDclTableView::find($view_for_data_collection_object->getId());
93  $roles_of_view = $ilDclTableView->getRoles();
94  $roles_ids = array_merge($roles_ids, $roles_of_view);
95  }
96  }
97 
98  return $roles_ids;
99  }
100 
101  protected function getStandardViewsByVisibleTables(array $visible_tables_for_data_collection): array
102  {
103  $standard_views_for_data_collection = [];
104  foreach ($visible_tables_for_data_collection as $visible_table) {
105  $standard_views_for_data_collection[] = ilDclTableView::where(
106  [
107  'table_id' => $visible_table['id'],
108  ]
109  )->get();
110  }
111 
112  return $standard_views_for_data_collection;
113  }
114 
115  protected function getAllVisibleTablesForDataColleciton(): array
116  {
117  $visible_tables_for_data_collection = [];
118  $res = $this->database->queryF(
119  "SELECT * FROM il_dcl_table WHERE obj_id = %s AND is_visible = 1",
120  array('integer'),
121  array($this->obj_id)
122  );
123  while ($rec = $this->database->fetchAssoc($res)) {
124  $visible_tables_for_data_collection[] = $rec;
125  }
126 
127  return $visible_tables_for_data_collection;
128  }
129 
130  protected function hasUserReadAccessOnAnyVisibleTableView(): bool
131  {
132  // admin user has always access to the views of a data collection
133  if ($this->user->getId() == 6) {
134  return true;
135  }
136 
137  $visible_tables_for_data_collection = $this->getAllVisibleTablesForDataColleciton();
138  $standard_views_for_data_collection = $this->getStandardViewsByVisibleTables($visible_tables_for_data_collection);
139 
140  $roles_ids = $this->getRolesIdsByViews($standard_views_for_data_collection);
141 
142  $user_ids_with_read_right_on_any_standard_view = [];
143  foreach ($roles_ids as $role_id) {
144  $assigned_users = $this->rbac_review->assignedUsers($role_id);
145  if (!empty($assigned_users)) {
146  $user_ids_with_read_right_on_any_standard_view[] = array_merge(
147  $user_ids_with_read_right_on_any_standard_view,
148  $assigned_users
149  );
150  }
151  }
152 
153  //check if current user id is in the array of user ids with read right on standard view
154  if ($this->in_array_r($this->user->getId(), $user_ids_with_read_right_on_any_standard_view)) {
155  return true;
156  } else {
157  return false;
158  }
159  }
160 
161  protected function in_array_r(string $needle, array $haystack, bool $strict = false): bool
162  {
163  foreach ($haystack as $item) {
164  if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r(
165  $needle,
166  $item,
167  $strict
168  ))) {
169  return true;
170  }
171  }
172 
173  return false;
174  }
175 }
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
in_array_r(string $needle, array $haystack, bool $strict=false)
static where($where, $operator=null)
getStandardViewsByVisibleTables(array $visible_tables_for_data_collection)
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(int $obj_id, int $ref_id, ilRbacReview $rbac_review, ilObjUser $user, ilDBInterface $database)
ilDclTableHelper constructor.