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