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