ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilTestAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28
38{
40 protected ilDBInterface $db;
41 protected ilLanguage $lng;
43
46
47 public function __construct(
48 protected int $ref_id
49 ) {
51 global $DIC;
52 $this->db = $DIC['ilDB'];
53 $this->lng = $DIC['lng'];
54 $this->participant_access_filter = new ilTestParticipantAccessFilterFactory($DIC['ilAccess']);
55 $this->participant_repository = TestDIC::dic()['participant.repository'];
56 $this->access = $DIC->access();
57 $this->main_settings_repository = TestDIC::dic()['settings.main.repository'];
58 }
59
60 public function getAccess(): ilAccessHandler
61 {
62 return $this->access;
63 }
64
66 {
67 $this->access = $access;
68 }
69
70 public function getRefId(): int
71 {
72 return $this->ref_id;
73 }
74
78 public function checkCorrectionsAccess(): bool
79 {
80 return $this->getAccess()->checkAccess('write', '', $this->getRefId());
81 }
82
86 public function checkScoreParticipantsAccess(): bool
87 {
88 if (!$this->getAccess()->checkAccess('read', '', $this->getRefId())) {
89 return false;
90 }
91 return
92 $this->getAccess()->checkAccess('write', '', $this->getRefId())
93 || $this->getAccess()->checkPositionAccess(ilOrgUnitOperation::OP_SCORE_PARTICIPANTS, $this->getRefId())
94 ;
95 }
96
97 public function checkScoreParticipantsAccessAnon(): bool
98 {
99 return $this->getAccess()->checkAccess('score_anon', '', $this->getRefId());
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 (SettingsNotFoundException $e) {
192 return ParticipantAccess::MISSING_SETTINGS;
193 } catch (\Exception $e) {
194 return ParticipantAccess::BROKEN_TEST;
195 }
196
197 $participant = $this->participant_repository->getParticipantByUserId(
200 ),
201 $user_id
202 );
203
204 if ($access_settings->getFixedParticipants()
205 && ($participant === null || !$participant->isInvitedParticipant())) {
206 return ParticipantAccess::NOT_INVITED;
207 }
208
209 $ip = $_SERVER['REMOTE_ADDR'];
210
211 $allowed_individual = $this->isParticipantExplicitelyAllowedByIndividualIPRange($participant, $ip);
212 if ($allowed_individual === false) {
213 return ParticipantAccess::INDIVIDUAL_CLIENT_IP_MISMATCH;
214 }
215
216
217 if ($allowed_individual === true
218 || !$access_settings->isIpRangeEnabled()) {
219 return ParticipantAccess::ALLOWED;
220 }
221
222 if (!$this->isIpAllowedToAccessTest($ip, $access_settings)) {
223 return ParticipantAccess::TEST_LEVEL_CLIENT_IP_MISMATCH;
224 }
225
226 return ParticipantAccess::ALLOWED;
227 }
228
230 ?Participant $participant,
231 string $ip
232 ): ?bool {
233 $range_start = $participant?->getClientIpFrom();
234 $range_end = $participant?->getClientIpTo();
235
236 if ($range_start === null && $range_end === null) {
237 return null;
238 }
239
240 if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
241 return $this->isIpv4Between($ip, $range_start, $range_end);
242 }
243
244 if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
245 return $this->isIpv6Between($ip, $range_start, $range_end);
246 }
247
248 return false;
249 }
250
251 private function isIpAllowedToAccessTest(
252 string $ip,
253 SettingsAccess $access_settings
254 ): bool {
255 if (!$access_settings->isIpRangeEnabled()) {
256 return true;
257 }
258
259 $range_start = $access_settings->getIpRangeFrom();
260 $range_end = $access_settings->getIpRangeTo();
261
262 if ($this->isIpTypeOf(FILTER_FLAG_IPV4, $ip, $range_start, $range_end)) {
263 return $this->isIpv4Between($ip, $range_start, $range_end);
264 }
265
266 if ($this->isIpTypeOf(FILTER_FLAG_IPV6, $ip, $range_start, $range_end)) {
267 return $this->isIpv6Between($ip, $range_start, $range_end);
268 }
269
270 return false;
271 }
272
273 private function isIpTypeOf(int $ip_type_flag, string $ip, string $range_start, string $range_end): bool
274 {
275 return filter_var($ip, FILTER_VALIDATE_IP, $ip_type_flag) !== false
276 && filter_var($range_start, FILTER_VALIDATE_IP, $ip_type_flag) !== false
277 && filter_var($range_end, FILTER_VALIDATE_IP, $ip_type_flag) !== false;
278 }
279
280 private function isIpv4Between(string $ip, string $range_start, string $range_end): bool
281 {
282 return ip2long($range_start) <= ip2long($ip)
283 && ip2long($ip) <= ip2long($range_end);
284 }
285
286 private function isIpv6Between(string $ip, string $range_start, string $range_end): bool
287 {
288 return bin2hex(inet_pton($range_start)) <= bin2hex(inet_pton($ip))
289 && bin2hex(inet_pton($ip)) <= bin2hex(inet_pton($range_end));
290 }
291}
@depracated This is only a temporary exception to identify missing migrations and will be removed in ...
language handling
static _getTestIDFromObjectID($object_id)
Returns the ILIAS test id for a given object id.
static _lookupObjId(int $ref_id)
isIpv6Between(string $ip, string $range_start, string $range_end)
MainSettingsDatabaseRepository $main_settings_repository
checkResultsAccessForActiveId(int $active_id, int $test_id)
isParticipantExplicitelyAllowedByIndividualIPRange(?Participant $participant, string $ip)
isIpTypeOf(int $ip_type_flag, string $ip, string $range_start, string $range_end)
checkOtherParticipantsLearningProgressAccess()
checkAccessForActiveId(Closure $access_filter, int $active_id, int $test_id)
isIpv4Between(string $ip, string $range_start, string $range_end)
ParticipantRepository $participant_repository
checkScoreParticipantsAccessForActiveId(int $active_id, int $test_id)
ilDBInterface $db
isIpAllowedToAccessTest(string $ip, SettingsAccess $access_settings)
ilAccessHandler $access
isParticipantAllowed(int $obj_id, int $user_id)
setAccess(ilAccessHandler $access)
ilTestParticipantAccessFilterFactory $participant_access_filter
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26