ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRegistrationCode.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 protected const DB_TABLE = 'reg_registration_codes';
29 public const CODE_LENGTH = 10;
30
31 public static function create(
32 int $role,
33 int $stamp,
34 array $local_roles,
35 ?string $limit,
36 ?string $limit_date,
37 bool $reg_type,
38 bool $ext_type
39 ): int {
40 global $DIC;
41
42 $ilDB = $DIC->database();
43 $id = $ilDB->nextId(self::DB_TABLE);
44
45 // create unique code
46 $found = true;
47 $code = '';
48 while ($found) {
50 $chk = $ilDB->queryF(
51 "SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s",
52 ["text"],
53 [$code]
54 );
55 $found = (bool) $ilDB->numRows($chk);
56 }
57
58 $data = [
59 'code_id' => ['integer', $id],
60 'code' => ['text', $code],
61 'generated_on' => ['integer', $stamp],
62 'role' => ['integer', $role],
63 'role_local' => ['text', implode(";", $local_roles)],
64 'alimit' => ['text', $limit],
65 'alimitdt' => ['text', $limit_date],
66 'reg_enabled' => ['integer', $reg_type],
67 'ext_enabled' => ['integer', $ext_type]
68 ];
69
70 $ilDB->insert(self::DB_TABLE, $data);
71 return $id;
72 }
73
74 protected static function generateRandomCode(): string
75 {
76 // missing : 01iloO
77 $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
78
79 $code = "";
80 $max = strlen($map) - 1;
81 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
82 $code .= $map[random_int(0, $max)];
83 }
84 return $code;
85 }
86
87 public static function isUnusedCode(string $code): bool
88 {
89 global $DIC;
90
91 $ilDB = $DIC['ilDB'];
92
93 $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
94 $set = $ilDB->fetchAssoc($set);
95 return $set && !$set["used"];
96 }
97
98 public static function isValidRegistrationCode(string $a_code): bool
99 {
100 global $DIC;
101
102 $ilDB = $DIC->database();
103
104 $query = 'SELECT alimit, alimitdt FROM reg_registration_codes ' .
105 'WHERE used = ' . $ilDB->quote(0, 'integer') . ' ' .
106 'AND reg_enabled = ' . $ilDB->quote(1, 'integer') . ' ' .
107 'AND code = ' . $ilDB->quote($a_code, 'text');
108 $res = $ilDB->query($query);
109 if ($ilDB->numRows($res) !== 1) {
110 return false;
111 }
112
113 $is_valid = true;
114
115 $row = $ilDB->fetchAssoc($res);
116 if ($row['alimit'] === 'absolute') {
117 $clock_factory = (new \ILIAS\Data\Factory())->clock();
118 $right_interval = new DateTimeImmutable(
119 $row['alimitdt'],
120 $clock_factory->system()->now()->getTimezone()
121 );
122
123 $is_valid = $right_interval >= $clock_factory->system()->now();
124 }
125
126 return $is_valid;
127 }
128
129 public static function getCodeValidUntil(string $code): string
130 {
131 $code_data = self::getCodeData($code);
132
133 if ($code_data["alimit"]) {
134 switch ($code_data["alimit"]) {
135 case "absolute":
136 return $code_data['alimitdt'];
137 }
138 }
139 return "0";
140 }
141
142 public static function useCode(string $code): bool
143 {
144 global $DIC;
145
146 $ilDB = $DIC->database();
147 return (bool) $ilDB->update(
148 self::DB_TABLE,
149 ["used" => ["timestamp", time()]],
150 ["code" => ["text", $code]]
151 );
152 }
153
154 public static function getCodeRole(string $code): int
155 {
156 global $DIC;
157
158 $ilDB = $DIC->database();
159 $set = $ilDB->query("SELECT role FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
160 $row = $ilDB->fetchAssoc($set);
161 if (isset($row["role"])) {
162 return (int) $row["role"];
163 }
164 return 0;
165 }
166
167 public static function getCodeData(string $code): array
168 {
169 global $DIC;
170
171 $ilDB = $DIC->database();
172 $set = $ilDB->query("SELECT role, role_local, alimit, alimitdt, reg_enabled, ext_enabled" .
173 " FROM " . self::DB_TABLE .
174 " WHERE code = " . $ilDB->quote($code, "text"));
175 return $ilDB->fetchAssoc($set);
176 }
177
178 public static function applyRoleAssignments(
179 ilObjUser $user,
180 string $code
181 ): bool {
182 $recommended_content_manager = new ilRecommendedContentManager();
183
184 $grole = self::getCodeRole($code);
185 if ($grole) {
186 $GLOBALS['DIC']['rbacadmin']->assignUser($grole, $user->getId());
187 }
188 $code_data = self::getCodeData($code);
189 if ($code_data["role_local"]) {
190 $code_local_roles = explode(";", $code_data["role_local"]);
191 foreach ($code_local_roles as $role_id) {
192 $GLOBALS['DIC']['rbacadmin']->assignUser($role_id, $user->getId());
193
194 // patch to remove for 45 due to mantis 21953
195 $role_obj = $GLOBALS['DIC']['rbacreview']->getObjectOfRole($role_id);
196 switch (ilObject::_lookupType($role_obj)) {
197 case 'crs':
198 case 'grp':
199 $role_refs = ilObject::_getAllReferences($role_obj);
200 $role_ref = end($role_refs);
201 // deactivated for now, see discussion at
202 // https://docu.ilias.de/goto_docu_wiki_wpage_5620_1357.html
203 //$recommended_content_manager->addObjectRecommendation($user->getId(), $role_ref);
204 break;
205 }
206 }
207 }
208 return true;
209 }
210
211 public static function applyAccessLimits(
212 ilObjUser $user,
213 string $code
214 ): void {
215 $code_data = self::getCodeData($code);
216
217 if ($code_data["alimit"]) {
218 switch ($code_data["alimit"]) {
219 case "absolute":
220 $end = new ilDateTime($code_data['alimitdt'], IL_CAL_DATE);
221 //$user->setTimeLimitFrom(time());
222 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
223 $user->setTimeLimitUnlimited(false);
224 break;
225
226 case "relative":
227
228 $rel = unserialize($code_data["alimitdt"], ["allowed_classes" => false]);
229
230 $end = new ilDateTime(time(), IL_CAL_UNIX);
231
232 if ($rel['y'] > 0) {
233 $end->increment(IL_CAL_YEAR, (int) $rel['y']);
234 }
235
236 if ($rel['m'] > 0) {
237 $end->increment(IL_CAL_MONTH, (int) $rel['m']);
238 }
239
240 if ($rel['d'] > 0) {
241 $end->increment(IL_CAL_DAY, (int) $rel['d']);
242 }
243
244 //$user->setTimeLimitFrom(time());
245 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
246 $user->setTimeLimitUnlimited(false);
247 break;
248
249 case 'unlimited':
250 $user->setTimeLimitUnlimited(true);
251 break;
252 }
253 } else {
254 $user->setTimeLimitUnlimited(true);
255 }
256 }
257}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_YEAR
const IL_CAL_MONTH
const IL_CAL_DAY
@classDescription Date and time handling
User class.
setTimeLimitUntil(?int $a_until)
setTimeLimitUnlimited(bool $unlimited)
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilRegistrationCode.
static getCodeValidUntil(string $code)
static create(int $role, int $stamp, array $local_roles, ?string $limit, ?string $limit_date, bool $reg_type, bool $ext_type)
static useCode(string $code)
static isUnusedCode(string $code)
static getCodeRole(string $code)
static applyAccessLimits(ilObjUser $user, string $code)
static getCodeData(string $code)
static isValidRegistrationCode(string $a_code)
static applyRoleAssignments(ilObjUser $user, string $code)
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54