ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCronDeleteNeverLoggedInUserAccounts.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 {
10 
12  private $roleIdWhiteliste = '';
13 
15  private $thresholdInDays = self::DEFAULT_CREATION_THRESHOLD;
16 
18  protected $lng;
19 
21  protected $settings;
22 
24  protected $rbacreview;
25 
27  protected $objectDataCache;
28 
30  protected $request;
31 
35  public function __construct()
36  {
37  global $DIC;
38 
39  if ($DIC) {
40  if (isset($DIC['ilSetting'])) {
41  $this->settings = $DIC->settings();
42 
43  $this->roleIdWhiteliste = (string) $this->settings->get(
44  'cron_users_without_login_delete_incl_roles',
45  ''
46  );
47 
48  $this->thresholdInDays = (int) $this->settings->get(
49  'cron_users_without_login_delete_threshold',
50  self::DEFAULT_CREATION_THRESHOLD
51  );
52  }
53 
54  if (isset($DIC['lng'])) {
55  $this->lng = $DIC->language();
56  $this->lng->loadLanguageModule('usr');
57  }
58 
59  if (isset($DIC['rbacreview'])) {
60  $this->rbacreview = $DIC->rbac()->review();
61  }
62 
63  if (isset($DIC['ilObjDataCache'])) {
64  $this->objectDataCache = $DIC['ilObjDataCache'];
65  }
66 
67  if (isset($DIC['http'])) {
68  $this->request = $DIC->http()->request();
69  }
70  }
71  }
72 
76  public function getId()
77  {
78  return 'user_never_logged_in';
79  }
80 
84  public function getTitle()
85  {
86  global $DIC;
87 
88  return $DIC->language()->txt('user_never_logged_in');
89  }
90 
94  public function getDescription()
95  {
96  global $DIC;
97 
98  return $DIC->language()->txt('user_never_logged_in_info');
99  }
100 
104  public function getDefaultScheduleType()
105  {
106  return self::SCHEDULE_TYPE_DAILY;
107  }
108 
112  public function getDefaultScheduleValue()
113  {
114  return 1;
115  }
116 
120  public function hasAutoActivation()
121  {
122  return false;
123  }
124 
128  public function hasFlexibleSchedule()
129  {
130  return true;
131  }
132 
136  public function hasCustomSettings()
137  {
138  return true;
139  }
140 
144  public function run()
145  {
146  global $DIC;
147 
148  $result = new \ilCronJobResult();
149 
151  $message = 'No user deleted';
152 
154  $this->thresholdInDays ?: self::DEFAULT_CREATION_THRESHOLD
155  );
156 
157  $roleIdWhitelist = array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste)));
158 
159  $counter = 0;
160  foreach ($userIds as $userId) {
161  if ($userId == ANONYMOUS_USER_ID || $userId == SYSTEM_USER_ID) {
162  continue;
163  }
164 
165  $user = ilObjectFactory::getInstanceByObjId($userId, false);
166  if (!$user || !($user instanceof \ilObjUser)) {
167  continue;
168  }
169 
170  $ignoreUser = true;
171 
172  if (count($roleIdWhitelist) > 0) {
173  $assignedRoleIds = array_filter(array_map('intval', (array) $this->rbacreview->assignedRoles($userId)));
174 
175  $respectedRolesToInclude = array_intersect($assignedRoleIds, $roleIdWhitelist);
176  if (count($respectedRolesToInclude) > 0) {
177  $ignoreUser = false;
178  }
179  }
180 
181  if ($ignoreUser) {
182  continue;
183  }
184 
185  $DIC->logger()->usr()->info(sprintf(
186  "Deleting user account with id %s (login: %s)",
187  $user->getId(),
188  $user->getLogin()
189  ));
190  $user->delete();
191 
192  $counter++;
193  }
194 
195  if ($counter) {
196  $status = \ilCronJobResult::STATUS_OK;
197  $message = sprintf('%s user(s) deleted', $counter);
198  }
199 
200  $result->setStatus($status);
201  $result->setMessage($message);
202 
203  return $result;
204  }
205 
209  public function addCustomSettingsToForm(\ilPropertyFormGUI $a_form)
210  {
211  $roleWhiteList = new ilMultiSelectInputGUI(
212  $this->lng->txt('cron_users_without_login_del_role_whitelist'),
213  'role_whitelist'
214  );
215  $roleWhiteList->setInfo($this->lng->txt('cron_users_without_login_del_role_whitelist_info'));
216  $roles = array();
217  foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
218  if ($role_id != ANONYMOUS_ROLE_ID) {
219  $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
220  }
221  }
222  $roleWhiteList->setOptions($roles);
223  $roleWhiteList->setValue(array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste))));
224  $roleWhiteList->setWidth(300);
225  $a_form->addItem($roleWhiteList);
226 
227  $threshold = new ilNumberInputGUI(
228  $this->lng->txt('cron_users_without_login_del_create_date_thr'),
229  'threshold'
230  );
231  $threshold->setInfo($this->lng->txt('cron_users_without_login_del_create_date_thr_info'));
232  $threshold->setValue($this->thresholdInDays);
233  $threshold->setSuffix($this->lng->txt('days'));
234  $threshold->setSize(4);
235  $threshold->setMaxLength(4);
236  $threshold->setRequired(true);
237  $a_form->addItem($threshold);
238  }
239 
243  public function saveCustomSettings(\ilPropertyFormGUI $a_form)
244  {
245  $valid = true;
246 
247  $roleIdWhitelist = $this->request->getParsedBody()['role_whitelist'] ?? [];
248  $this->roleIdWhiteliste = implode(',', array_map('intval', (is_array($roleIdWhitelist) ? $roleIdWhitelist : [])));
249 
250  $this->thresholdInDays = $this->request->getParsedBody()['threshold'] ?? '';
251 
252  if (!is_numeric($this->thresholdInDays) || $this->hasDecimals($this->thresholdInDays)) {
253  $valid = false;
254  $a_form->getItemByPostVar('threshold')->setAlert($this->lng->txt('user_never_logged_in_info_threshold_err_num'));
255  }
256 
257  if ($valid) {
258  $this->settings->set(
259  'cron_users_without_login_delete_incl_roles',
260  (string) $this->roleIdWhiteliste
261  );
262  $this->settings->set(
263  'cron_users_without_login_delete_threshold',
264  (int) $this->thresholdInDays
265  );
266  return true;
267  } else {
268  \ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
269  return false;
270  }
271  }
272 
277  protected function hasDecimals($number) : bool
278  {
279  if (strpos($number, ',') !== false || strpos($number, '.') !== false) {
280  return true;
281  }
282 
283  return false;
284  }
285 }
settings()
Definition: settings.php:2
getItemByPostVar($a_post_var)
Get Item by POST variable.
$result
This class represents a property form user interface.
Cron job application base class.
$valid
addItem($a_item)
Add Item (Property, SectionHeader).
setInfo($a_info)
Set Information Text.
This class represents a multi selection list property in a property form.
This class represents a number property in a property form.
__construct()
ilCronDeleteNeverLoggedInUserAccounts constructor.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$DIC
Definition: xapitoken.php:46
$message
Definition: xapiexit.php:14
static getUserIdsNeverLoggedIn(int $thresholdInDays)
Get ids of all users that have never logged in.