ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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('write', '', $this->getRefId())) {
88  return true;
89  }
90 
91  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
92  return false;
93  }
94 
95  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_SCORE_PARTICIPANTS, $this->getRefId())) {
96  return true;
97  }
98 
99  return false;
100  }
101 
105  public function checkManageParticipantsAccess(): bool
106  {
107  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
108  return true;
109  }
110 
111  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
112  return false;
113  }
114 
115  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_MANAGE_PARTICIPANTS, $this->getRefId())) {
116  return true;
117  }
118 
119  return false;
120  }
121 
122  public function checkParticipantsResultsAccess(): bool
123  {
124  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
125  return true;
126  }
127 
128  if ($this->getAccess()->checkAccess('tst_results', '', $this->getRefId())) {
129  return true;
130  }
131 
132  if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
133  return false;
134  }
135 
136  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_MANAGE_PARTICIPANTS, $this->getRefId())) {
137  return true;
138  }
139 
140  if ($this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_ACCESS_RESULTS, $this->getRefId())) {
141  return true;
142  }
143 
144  return false;
145  }
146 
148  {
149  if ($this->getAccess()->checkAccess('write', '', $this->getRefId())) {
150  return true;
151  }
152 
153  if ($this->getAccess()->checkRbacOrPositionPermissionAccess(
154  'read_learning_progress',
156  $this->getRefId()
157  )) {
158  return true;
159  }
160 
161  return false;
162  }
163 
164  protected function checkAccessForActiveId(Closure $access_filter, int $active_id, int $test_id): bool
165  {
166  $participantData = new ilTestParticipantData($this->db, $this->lng);
167  $participantData->setActiveIdsFilter([$active_id]);
168  $participantData->setParticipantAccessFilter($access_filter);
169  $participantData->load($test_id);
170 
171  return in_array($active_id, $participantData->getActiveIds());
172  }
173 
174  public function checkResultsAccessForActiveId(int $active_id, int $test_id): bool
175  {
176  $access_filter = $this->participant_access_filter->getAccessResultsUserFilter($this->getRefId());
177  return $this->checkAccessForActiveId($access_filter, $active_id, $test_id);
178  }
179 
180  public function checkScoreParticipantsAccessForActiveId(int $active_id, int $test_id): bool
181  {
182  $access_filter = $this->participant_access_filter->getScoreParticipantsUserFilter($this->getRefId());
183  return $this->checkAccessForActiveId($access_filter, $active_id, $test_id);
184  }
185 
186  public function isParticipantAllowed(int $obj_id, int $user_id): ParticipantAccess
187  {
188  try {
189  $access_settings = $this->main_settings_repository->getForObjFi($obj_id)
190  ->getAccessSettings();
191  } catch (\Exception $e) {
192  return ParticipantAccess::BROKEN_TEST;
193  }
194 
195  $participant = $this->participant_repository->getParticipantByUserId(
198  ),
199  $user_id
200  );
201 
202  if ($access_settings->getFixedParticipants()
203  && ($participant === null || !$participant->isInvitedParticipant())) {
204  return ParticipantAccess::NOT_INVITED;
205  }
206 
207  $ip = $_SERVER['REMOTE_ADDR'];
208 
209  $allowed_individual = $this->isParticipantExplicitelyAllowedByIndividualIPRange($participant, $ip);
210  if ($allowed_individual === false) {
211  return ParticipantAccess::INDIVIDUAL_CLIENT_IP_MISMATCH;
212  }
213 
214 
215  if ($allowed_individual === true
216  || !$access_settings->isIpRangeEnabled()) {
217  return ParticipantAccess::ALLOWED;
218  }
219 
220  if (!$this->isIpAllowedToAccessTest($ip, $access_settings)) {
221  return ParticipantAccess::TEST_LEVEL_CLIENT_IP_MISMATCH;
222  }
223 
224  return ParticipantAccess::ALLOWED;
225  }
226 
228  ?Participant $participant,
229  string $ip
230  ): ?bool {
231  $range_start = $participant?->getClientIpFrom();
232  $range_end = $participant?->getClientIpTo();
233 
234  if ($range_start === null && $range_end === null) {
235  return null;
236  }
237 
238  if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
239  return $this->isIpv4Between($ip, $range_start, $range_end);
240  }
241 
242  if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
243  return $this->isIpv6Between($ip, $range_start, $range_end);
244  }
245 
246  return false;
247  }
248 
249  private function isIpAllowedToAccessTest(
250  string $ip,
251  SettingsAccess $access_settings
252  ): bool {
253  if (!$access_settings->isIpRangeEnabled()) {
254  return true;
255  }
256 
257  $range_start = $access_settings->getIpRangeFrom();
258  $range_end = $access_settings->getIpRangeTo();
259 
260  if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
261  return $this->isIpv4Between($ip, $range_start, $range_end);
262  }
263 
264  if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
265  return $this->isIpv6Between($ip, $range_start, $range_end);
266  }
267 
268  return false;
269  }
270 
271  private function isIpTypeOf(int $ip_type_flag, string $ip, string $range_start, string $range_end): bool
272  {
273  return filter_var($ip, FILTER_VALIDATE_IP, $ip_type_flag) !== false
274  && filter_var($range_start, FILTER_VALIDATE_IP, $ip_type_flag) !== false
275  && filter_var($range_end, FILTER_VALIDATE_IP, $ip_type_flag) !== false;
276  }
277 
278  private function isIpv4Between(string $ip, string $range_start, string $range_end): bool
279  {
280  return ip2long($range_start) <= ip2long($ip)
281  && ip2long($ip) <= ip2long($range_end);
282  }
283 
284  private function isIpv6Between(string $ip, string $range_start, string $range_end): bool
285  {
286  return bin2hex(inet_pton($range_start)) <= bin2hex(inet_pton($ip))
287  && bin2hex(inet_pton($ip)) <= bin2hex(inet_pton($range_end));
288  }
289 }
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)
checkOtherParticipantsLearningProgressAccess()
$ref_id
Definition: ltiauth.php:66
checkAccessForActiveId(Closure $access_filter, int $active_id, int $test_id)
isParticipantExplicitelyAllowedByIndividualIPRange(?Participant $participant, string $ip)
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
global $DIC
Definition: shib_login.php:25
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