ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTestAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
37 {
39  protected ilDBInterface $db;
40  protected ilLanguage $lng;
42 
45 
46  public function __construct(
47  protected int $ref_id
48  ) {
50  global $DIC;
51  $this->db = $DIC['ilDB'];
52  $this->lng = $DIC['lng'];
53  $this->participant_access_filter = new ilTestParticipantAccessFilterFactory($DIC['ilAccess']);
54  $this->participant_repository = TestDIC::dic()['participant.repository'];
55  $this->access = $DIC->access();
56  $this->main_settings_repository = TestDIC::dic()['settings.main.repository'];
57  }
58 
59  public function getAccess(): ilAccessHandler
60  {
61  return $this->access;
62  }
63 
64  public function setAccess(ilAccessHandler $access)
65  {
66  $this->access = $access;
67  }
68 
69  public function getRefId(): int
70  {
71  return $this->ref_id;
72  }
73 
77  public function checkCorrectionsAccess(): bool
78  {
79  return $this->getAccess()->checkAccess('write', '', $this->getRefId());
80  }
81 
85  public function checkScoreParticipantsAccess(): bool
86  {
87  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
88  return false;
89  }
90  return
91  $this->getAccess()->checkAccess('write', '', $this->getRefId())
92  || $this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_SCORE_PARTICIPANTS, $this->getRefId())
93  ;
94  }
95 
96  public function checkScoreParticipantsAccessAnon(): bool
97  {
98  return $this->getAccess()->checkAccess('score_anon', '', $this->getRefId());
99  }
100 
104  public function checkManageParticipantsAccess(): bool
105  {
106  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
107  return true;
108  }
109 
110  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
111  return false;
112  }
113 
114  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_MANAGE_PARTICIPANTS, $this->getRefId())) {
115  return true;
116  }
117 
118  return false;
119  }
120 
121  public function checkParticipantsResultsAccess(): bool
122  {
123  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
124  return true;
125  }
126 
127  if ($this->getAccess()->checkAccess('tst_results', '', $this->getRefId())) {
128  return true;
129  }
130 
131  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
132  return false;
133  }
134 
135  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_MANAGE_PARTICIPANTS, $this->getRefId())) {
136  return true;
137  }
138 
139  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_ACCESS_RESULTS, $this->getRefId())) {
140  return true;
141  }
142 
143  return false;
144  }
145 
147  {
148  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
149  return true;
150  }
151 
152  if ($this->getAccess()->checkRbacOrPositionPermissionAccess(
153  'read_learning_progress',
155  $this->getRefId()
156  )) {
157  return true;
158  }
159 
160  return false;
161  }
162 
163  protected function checkAccessForActiveId(Closure $access_filter, int $active_id, int $test_id): bool
164  {
165  $participantData = new ilTestParticipantData($this->db, $this->lng);
166  $participantData->setActiveIdsFilter([$active_id]);
167  $participantData->setParticipantAccessFilter($access_filter);
168  $participantData->load($test_id);
169 
170  return in_array($active_id, $participantData->getActiveIds());
171  }
172 
173  public function checkResultsAccessForActiveId(int $active_id, int $test_id): bool
174  {
175  $access_filter = $this->participant_access_filter->getAccessResultsUserFilter($this->getRefId());
176  return $this->checkAccessForActiveId($access_filter, $active_id, $test_id);
177  }
178 
179  public function checkScoreParticipantsAccessForActiveId(int $active_id, int $test_id): bool
180  {
181  $access_filter = $this->participant_access_filter->getScoreParticipantsUserFilter($this->getRefId());
182  return $this->checkAccessForActiveId($access_filter, $active_id, $test_id);
183  }
184 
185  public function isParticipantAllowed(int $obj_id, int $user_id): ParticipantAccess
186  {
187  try {
188  $access_settings = $this->main_settings_repository->getForObjFi($obj_id)
189  ->getAccessSettings();
190  } catch (\Exception $e) {
191  return ParticipantAccess::BROKEN_TEST;
192  }
193 
194  $participant = $this->participant_repository->getParticipantByUserId(
197  ),
198  $user_id
199  );
200 
201  if ($access_settings->getFixedParticipants()
202  && ($participant === null || !$participant->isInvitedParticipant())) {
203  return ParticipantAccess::NOT_INVITED;
204  }
205 
206  $ip = $_SERVER['REMOTE_ADDR'];
207 
208  $allowed_individual = $this->isParticipantExplicitelyAllowedByIndividualIPRange($participant, $ip);
209  if ($allowed_individual === false) {
210  return ParticipantAccess::INDIVIDUAL_CLIENT_IP_MISMATCH;
211  }
212 
213 
214  if ($allowed_individual === true
215  || !$access_settings->isIpRangeEnabled()) {
216  return ParticipantAccess::ALLOWED;
217  }
218 
219  if (!$this->isIpAllowedToAccessTest($ip, $access_settings)) {
220  return ParticipantAccess::TEST_LEVEL_CLIENT_IP_MISMATCH;
221  }
222 
223  return ParticipantAccess::ALLOWED;
224  }
225 
227  ?Participant $participant,
228  string $ip
229  ): ?bool {
230  $range_start = $participant?->getClientIpFrom();
231  $range_end = $participant?->getClientIpTo();
232 
233  if ($range_start === null && $range_end === null) {
234  return null;
235  }
236 
237  if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
238  return $this->isIpv4Between($ip, $range_start, $range_end);
239  }
240 
241  if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
242  return $this->isIpv6Between($ip, $range_start, $range_end);
243  }
244 
245  return false;
246  }
247 
248  private function isIpAllowedToAccessTest(
249  string $ip,
250  SettingsAccess $access_settings
251  ): bool {
252  if (!$access_settings->isIpRangeEnabled()) {
253  return true;
254  }
255 
256  $range_start = $access_settings->getIpRangeFrom();
257  $range_end = $access_settings->getIpRangeTo();
258 
259  if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
260  return $this->isIpv4Between($ip, $range_start, $range_end);
261  }
262 
263  if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
264  return $this->isIpv6Between($ip, $range_start, $range_end);
265  }
266 
267  return false;
268  }
269 
270  private function isIpTypeOf(int $ip_type_flag, string $ip, string $range_start, string $range_end): bool
271  {
272  return filter_var($ip, FILTER_VALIDATE_IP, $ip_type_flag) !== false
273  && filter_var($range_start, FILTER_VALIDATE_IP, $ip_type_flag) !== false
274  && filter_var($range_end, FILTER_VALIDATE_IP, $ip_type_flag) !== false;
275  }
276 
277  private function isIpv4Between(string $ip, string $range_start, string $range_end): bool
278  {
279  return ip2long($range_start) <= ip2long($ip)
280  && ip2long($ip) <= ip2long($range_end);
281  }
282 
283  private function isIpv6Between(string $ip, string $range_start, string $range_end): bool
284  {
285  return bin2hex(inet_pton($range_start)) <= bin2hex(inet_pton($ip))
286  && bin2hex(inet_pton($ip)) <= bin2hex(inet_pton($range_end));
287  }
288 }
isIpTypeOf(int $ip_type_flag, string $ip, string $range_start, string $range_end)
ilAccessHandler $access
ParticipantRepository $participant_repository
setAccess(ilAccessHandler $access)
MainSettingsDatabaseRepository $main_settings_repository
static _lookupObjId(int $ref_id)
checkResultsAccessForActiveId(int $active_id, int $test_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
checkOtherParticipantsLearningProgressAccess()
$ref_id
Definition: ltiauth.php:65
checkAccessForActiveId(Closure $access_filter, int $active_id, int $test_id)
isParticipantExplicitelyAllowedByIndividualIPRange(?Participant $participant, string $ip)
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26
ilDBInterface $db
isIpv4Between(string $ip, string $range_start, string $range_end)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getTestIDFromObjectID($object_id)
Returns the ILIAS test id for a given object id.
checkScoreParticipantsAccessForActiveId(int $active_id, int $test_id)
isIpAllowedToAccessTest(string $ip, SettingsAccess $access_settings)
__construct(Container $dic, ilPlugin $plugin)
isIpv6Between(string $ip, string $range_start, string $range_end)
isParticipantAllowed(int $obj_id, int $user_id)
ilTestParticipantAccessFilterFactory $participant_access_filter