ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCronDeleteNeverLoggedInUserAccounts.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
27{
28 private const DEFAULT_CREATION_THRESHOLD = 365;
29
30 private string $roleIdWhiteliste = '';
32 private Language $lng;
36 private \ILIAS\HTTP\GlobalHttpState $http;
37 private \ILIAS\Refinery\Factory $refinery;
38 private \ilGlobalTemplateInterface $main_tpl;
39
40 public function __construct()
41 {
42 global $DIC;
43 $this->main_tpl = $DIC->ui()->mainTemplate();
44
45 if ($DIC) {
46 if (isset($DIC['ilSetting'])) {
47 $this->settings = $DIC->settings();
48
49 $this->roleIdWhiteliste = (string) $this->settings->get(
50 'cron_users_without_login_delete_incl_roles',
51 ''
52 );
53
54 $this->thresholdInDays = (int) $this->settings->get(
55 'cron_users_without_login_delete_threshold',
57 );
58 }
59
60 if (isset($DIC['lng'])) {
61 $this->lng = $DIC->language();
62 $this->lng->loadLanguageModule('usr');
63 }
64
65 if (isset($DIC['rbacreview'])) {
66 $this->rbacreview = $DIC->rbac()->review();
67 }
68
69 if (isset($DIC['ilObjDataCache'])) {
70 $this->objectDataCache = $DIC['ilObjDataCache'];
71 }
72
73 if (isset($DIC['http'])) {
74 $this->http = $DIC->http();
75 }
76
77 if (isset($DIC['refinery'])) {
78 $this->refinery = $DIC->refinery();
79 }
80 }
81 }
82
83 public function getId(): string
84 {
85 return 'user_never_logged_in';
86 }
87
88 public function getTitle(): string
89 {
90 global $DIC;
91
92 return $DIC->language()->txt('user_never_logged_in');
93 }
94
95 public function getDescription(): string
96 {
97 global $DIC;
98
99 return $DIC->language()->txt('user_never_logged_in_info');
100 }
101
103 {
104 return JobScheduleType::DAILY;
105 }
106
107 public function getDefaultScheduleValue(): int
108 {
109 return 1;
110 }
111
112 public function hasAutoActivation(): bool
113 {
114 return false;
115 }
116
117 public function hasFlexibleSchedule(): bool
118 {
119 return true;
120 }
121
122 public function hasCustomSettings(): bool
123 {
124 return true;
125 }
126
127 public function run(): JobResult
128 {
129 global $DIC;
130
131 $result = new JobResult();
132
133 $status = JobResult::STATUS_NO_ACTION;
134 $message = 'No user deleted';
135
137 $this->thresholdInDays ?: self::DEFAULT_CREATION_THRESHOLD
138 );
139
140 $roleIdWhitelist = array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste)));
141
142 $counter = 0;
143 foreach ($userIds as $userId) {
144 if ($userId === ANONYMOUS_USER_ID || $userId === SYSTEM_USER_ID) {
145 continue;
146 }
147
148 $user = ilObjectFactory::getInstanceByObjId($userId, false);
149 if (!($user instanceof ilObjUser)) {
150 continue;
151 }
152
153 $ignoreUser = true;
154
155 if (count($roleIdWhitelist) > 0) {
156 $assignedRoleIds = array_filter(array_map('intval', $this->rbacreview->assignedRoles($userId)));
157
158 $respectedRolesToInclude = array_intersect($assignedRoleIds, $roleIdWhitelist);
159 if (count($respectedRolesToInclude) > 0) {
160 $ignoreUser = false;
161 }
162 }
163
164 if ($ignoreUser) {
165 continue;
166 }
167
168 $DIC->logger()->user()->info(sprintf(
169 "Deleting user account with id %s (login: %s)",
170 $user->getId(),
171 $user->getLogin()
172 ));
173 $user->delete();
174
175 $counter++;
176 }
177
178 if ($counter) {
179 $status = JobResult::STATUS_OK;
180 $message = sprintf('%s user(s) deleted', $counter);
181 }
182
183 $result->setStatus($status);
184 $result->setMessage($message);
185
186 return $result;
187 }
188
189 public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
190 {
191 $roleWhiteList = new ilMultiSelectInputGUI(
192 $this->lng->txt('cron_users_without_login_del_role_whitelist'),
193 'role_whitelist'
194 );
195 $roleWhiteList->setInfo($this->lng->txt('cron_users_without_login_del_role_whitelist_info'));
196 $roles = [];
197 foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
198 if ($role_id !== ANONYMOUS_ROLE_ID) {
199 $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
200 }
201 }
202 $roleWhiteList->setOptions($roles);
203 $roleWhiteList->setValue(array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste))));
204 $roleWhiteList->setWidth(300);
205 $a_form->addItem($roleWhiteList);
206
207 $threshold = new ilNumberInputGUI(
208 $this->lng->txt('cron_users_without_login_del_create_date_thr'),
209 'threshold'
210 );
211 $threshold->allowDecimals(false);
212 $threshold->setInfo($this->lng->txt('cron_users_without_login_del_create_date_thr_info'));
213 $threshold->setValue((string) $this->thresholdInDays);
214 $threshold->setSuffix($this->lng->txt('days'));
215 $threshold->setSize(4);
216 $threshold->setMaxLength(4);
217 $threshold->setRequired(true);
218 $a_form->addItem($threshold);
219 }
220
221 public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
222 {
223 $valid = true;
224
225 $this->roleIdWhiteliste = implode(',', $this->http->wrapper()->post()->retrieve(
226 'role_whitelist',
227 $this->refinery->byTrying([
228 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
229 $this->refinery->always([])
230 ])
231 ));
232
233 try {
234 $this->thresholdInDays = $this->http->wrapper()->post()->retrieve(
235 'threshold',
236 $this->refinery->kindlyTo()->int()
237 );
239 $valid = false;
240 $a_form->getItemByPostVar('threshold')->setAlert($this->lng->txt('user_never_logged_in_info_threshold_err_num'));
241 }
242
243 if ($valid) {
244 $this->settings->set(
245 'cron_users_without_login_delete_incl_roles',
246 $this->roleIdWhiteliste
247 );
248 $this->settings->set(
249 'cron_users_without_login_delete_threshold',
250 (string) $this->thresholdInDays
251 );
252 return true;
253 }
254
255 $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
256 return false;
257 }
258}
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
This class represents a multi selection list property in a property form.
This class represents a number property in a property form.
User class.
static getUserIdsNeverLoggedIn(int $thresholdInDays)
class ilObjectDataCache
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
This class represents a property form user interface.
getItemByPostVar(string $a_post_var)
class ilRbacReview Contains Review functions of core Rbac.
ILIAS Setting Class.
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
const ANONYMOUS_USER_ID
Definition: constants.php:27
$valid
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$counter
$message
Definition: xapiexit.php:31