Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
# Lifter007: TODO
/**
* UserManagement.class.php
*
* Management for the Stud.IP global users
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* @author Stefan Suchi <suchi@data-quest>
* @author Suchi & Berg GmbH <info@data-quest.de>
* @copyright 2009 Stud.IP
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL Licence 2
* @category Stud.IP
*/
// Imports
require_once 'lib/statusgruppe.inc.php'; // remove user from statusgroups
require_once 'lib/messaging.inc.php'; // remove messages send or recieved by user
require_once 'lib/object.inc.php';
/**
* UserManagement.class.php
*
* Management for the Stud.IP global users
*
*/
class UserManagement
{
private $user;
private $validator;
private $user_data;
public $msg;
private static $pwd_hasher;
public static function getPwdHasher()
{
if (self::$pwd_hasher === null) {
self::$pwd_hasher = new PasswordHash(8, Config::get()->PHPASS_USE_PORTABLE_HASH);
}
return self::$pwd_hasher;
}
/**
* Constructor
*
* Pass nothing to create a new user, or the user_id from an existing user to change or delete
* @param string $user_id the user which should be retrieved
*/
public function __construct($user_id = false)
{
$this->validator = new email_validation_class();
$this->validator->timeout = 10; // How long do we wait for response of mailservers?
$this->getFromDatabase($user_id);
}
public function __get($attr)
{
if ($attr === 'user_data') {
return $this->user_data;
}
}
public function __set($attr, $value)
{
if ($attr === 'user_data') {
if (!is_array($value)) {
throw new InvalidArgumentException('user_data only accepts array');
}
return $this->user_data->setData($value, true);
}
}
/**
* load user data from database into internal array
*
* @param string $user_id the user which should be retrieved
*/
public function getFromDatabase($user_id)
{
$this->user = User::toObject($user_id);
if (!$this->user) {
$this->user = new User();
}
$this->user_data = new UserDataAdapter($this->user);
}
/**
* store user data from internal array into database
*
* @access private
* @return bool all data stored?
*/
private function storeToDatabase()
{
if ($this->user->isNew()) {
if ($this->user->store()) {
StudipLog::log('USER_CREATE', $this->user->id, null, implode(';', $this->user->toArray('username vorname nachname perms email')));
return true;
} else {
return false;
}
}
$nperms = [
'user' => 0,
'autor' => 1,
'tutor' => 2,
'dozent' => 3
];
if ($this->user->isDirty('perms')) {
if ($this->user->perms === 'dozent' && in_array($this->user->getPristineValue('perms'), ['user','autor','tutor'])) {
$this->logInstUserDel($this->user->id, "inst_perms = 'user'");
$this->user->institute_memberships->unsetBy('inst_perms', 'user');
// make user visible globally if dozent may not be invisible (StEP 00158)
if (Config::get()->DOZENT_ALWAYS_VISIBLE && $this->user->visible !== 'never') {
$this->user->visible = 'yes';
}
if ($nperms[$this->user->perms] < $nperms[$this->user->getPristineValue('perms')]) {
$old_status = [];
foreach ($nperms as $status => $n) {
if ($n > $nperms[$this->user->perms] && $n <= $nperms[$this->user->getPristineValue('perms')]) {
$old_status[] = $status;
}
}
$new_status = $this->user->perms;
CourseMember::findEachBySQL(
function (CourseMember $cm) use ($new_status) {
$cm->status = $new_status;
$cm->store();
},
'INNER JOIN seminare ON (seminare.Seminar_id = seminar_user.Seminar_id)
WHERE seminar_user.user_id = ?
AND seminar_user.status IN (?)
AND seminare.status NOT IN (?)
',
[
$this->user->id,
$old_status,
studygroup_sem_types()
]
);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
}
}
}
foreach (words('username vorname nachname perms email title_front title_rear password') as $field) {
// logging
if ($this->user->isFieldDirty($field)) {
$old_value = $this->user->getPristineValue($field);
$value = $this->user->getValue($field);
switch ($field) {
case 'username':
StudipLog::log('USER_CHANGE_USERNAME', $this->user->id, null, "{$old_value} -> {$value}");
break;
case 'vorname':
StudipLog::log('USER_CHANGE_NAME', $this->user->id, null, "Vorname: {$old_value} -> {$value}");
break;
case 'nachname':
StudipLog::log('USER_CHANGE_NAME', $this->user->id, null, "Nachname: {$old_value} -> {$value}");
break;
case 'perms':
StudipLog::log('USER_CHANGE_PERMS', $this->user->id, null, "{$old_value} -> {$value}");
break;
case 'email':
StudipLog::log('USER_CHANGE_EMAIL', $this->user->id, null, "{$old_value} -> {$value}");
break;
case 'title_front':
StudipLog::log('USER_CHANGE_TITLE', $this->user->id, null, "title_front: {$old_value} -> {$value}");
break;
case 'title_rear':
StudipLog::log('USER_CHANGE_TITLE', $this->user->id, null, "title_rear: {$old_value} -> {$value}");
case 'password':
StudipLog::log('USER_CHANGE_PASSWORD', $this->user->id, null, "password: {$old_value} -> {$value}");
break;
}
}
}
$changed = $this->user->store();
return (bool) $changed;
}
/**
* generate a secure password of $length characters [a-z0-9]
*
* @param integer $length number of characters
* @return string password
*/
public function generate_password($length)
{
$pass = "";
mt_srand((double) microtime() * 1000000);
for ($i = 1; $i <= $length; $i++) {
$temp = mt_rand() % 36;
if ($temp < 10) {
$temp += 48; // 0 = chr(48), 9 = chr(57)
} else {
$temp += 87; // a = chr(97), z = chr(122)
}
$pass .= chr($temp);
}
return $pass;
}
/**
* Check if Email-Adress is valid and reachable
*
* @param string Email-Adress to check
* @return bool Email-Adress valid and reachable?
*/
private function checkMail($Email)
{
// Adress correct?
if (!$this->validator->ValidateEmailAddress($Email)) {
$this->msg .= 'error§' . _('E-Mail-Adresse syntaktisch falsch!') . '§';
return false;
}
// E-Mail reachable?
if (!$this->validator->ValidateEmailHost($Email)) {
// Mailserver nicht erreichbar, ablehnen
$this->msg .= 'error§' . _('Mailserver ist nicht erreichbar!') . '§';
return false;
}
if (!$this->validator->ValidateEmailBox($Email)) {
// Nutzer unbekannt, ablehnen
$this->msg .= 'error§' . sprintf(_('E-Mail an <em>%s</em> ist nicht zustellbar!'), $Email) . '§';
return false;
}
return true;
}
/**
* Create a new studip user with the given parameters
*
* @param array structure: array('string table_name.field_name'=>'string value')
* @return bool Creation successful?
*/
public function createNewUser($newuser)
{
global $perm;
// Do we have permission to do so?
if (!$perm->have_perm('admin')) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung Accounts anzulegen.') . '§';
return false;
}
if (!$perm->is_fak_admin() && $newuser['auth_user_md5.perms'] === 'admin') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung, <em>Admin-Accounts</em> anzulegen.') . '§';
return false;
}
if (!$perm->have_perm('root') && $newuser['auth_user_md5.perms'] === 'root') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung, <em>Root-Accounts</em> anzulegen.') . '§';
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
return false;
}
// Do we have all necessary data?
if (empty($newuser['auth_user_md5.username']) || empty($newuser['auth_user_md5.perms']) || empty($newuser['auth_user_md5.Email'])) {
$this->msg .= 'error§' . _('Bitte geben Sie <em>Username</em>, <em>Status</em> und <em>E-Mail</em> an!') . '§';
return false;
}
// Is the username correct?
if (!$this->validator->ValidateUsername($newuser['auth_user_md5.username'])) {
$this->msg .= 'error§' . _('Der gewählte Benutzername ist zu kurz oder enthält unzulässige Zeichen!') . '§';
return false;
}
// Can we reach the email?
if (!$this->checkMail($newuser['auth_user_md5.Email'])) {
return false;
}
if (!$newuser['auth_user_md5.auth_plugin']) {
$newuser['auth_user_md5.auth_plugin'] = 'standard';
}
// Store new values in internal array
$this->getFromDatabase(null);
$this->user_data->setData($newuser);
if ($this->user_data['auth_user_md5.auth_plugin'] === 'standard') {
$password = $this->generate_password(8);
$this->user_data['auth_user_md5.password'] = self::getPwdHasher()->HashPassword($password);
}
// Does the user already exist?
// NOTE: This should be a transaction, but it is not...
$temp = User::findByUsername($newuser['auth_user_md5.username']);
if ($temp) {
$this->msg .= 'error§' . sprintf(_('BenutzerIn <em>%s</em> ist schon vorhanden!'), $newuser['auth_user_md5.username']) . '§';
return false;
}
if (!$this->storeToDatabase()) {
$this->msg .= 'error§' . sprintf(_('BenutzerIn "%s" konnte nicht angelegt werden.'), $newuser['auth_user_md5.username']) . '§';
return false;
}
$this->msg .= 'msg§' . sprintf(_('BenutzerIn "%s" angelegt.'), $newuser['auth_user_md5.username']) . '§';
// Automated entering new users, based on their status (perms)
$result = AutoInsert::instance()->saveUser($this->user_data['auth_user_md5.user_id'], $this->user_data['auth_user_md5.perms']);
foreach ($result['added'] as $item) {
$this->msg .= 'msg§' . sprintf(_('Das automatische Eintragen in die Veranstaltung <em>%s</em> wurde durchgeführt.'), $item) . '§';
}
foreach ($result['removed'] as $item) {
$this->msg .= 'msg§' . sprintf(_('Das automatische Austragen aus der Veranstaltung <em>%s</em> wurde durchgeführt.'), $item) . '§';
}
// include language-specific subject and mailbody
$user_language = $this->user_data['user_info.preferred_language'] ?: Config::get()->DEFAULT_LANGUAGE;
// send mail with password generation link
self::sendPasswordMail($this->user, true);
$this->msg .= 'msg§' . _('Es wurde eine Mail mit Anweisungen zum Setzen des Passworts durch die/den Nutzer/in verschickt.') . '§';
// add default visibility settings
Visibility::createDefaultCategories($this->user_data['auth_user_md5.user_id']);
return true;
}
/**
* Create a new preliminary studip user with the given parameters
*
* @param array structure: array('string table_name.field_name'=>'string value')
* @return bool Creation successful?
*/
public function createPreliminaryUser($newuser)
{
global $perm;
$this->getFromDatabase(null);
$this->user_data->setData($newuser);
// Do we have permission to do so?
if (!$perm->have_perm('admin')) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung Accounts anzulegen.') . '§';
return false;
}
if (in_array($this->user->perms, words('root admin'))) {
$this->msg .= 'error§' . _('Es können keine vorläufigen Administrationsaccounts angelegt werden.') . '§';
return false;
}
if (!$this->user->id) {
$this->user->setId($this->user->getNewId());
}
if (!$this->user->username) {
$this->user->username = $this->user->id;
}
$this->user->auth_plugin = null;
$this->user->visible = 'never';
// Do we have all necessary data?
if (empty ($this->user->perms) || empty ($this->user->vorname) || empty ($this->user->nachname)) {
$this->msg .= 'error§' . _('Bitte geben Sie <em>Status</em>, <em>Vorname</em> und <em>Nachname</em> an!') . '§';
return false;
}
// Is the username correct?
if (!$this->validator->ValidateUsername($this->user->username)) {
$this->msg .= 'error§' . _('Der gewählte Benutzername ist zu kurz oder enthält unzulässige Zeichen!') . '§';
return false;
}
// Does the user already exist?
// NOTE: This should be a transaction, but it is not...
$temp = User::findByUsername($this->user->username);
if ($temp) {
$this->msg .= 'error§' . sprintf(_('BenutzerIn <em>%s</em> ist schon vorhanden!'), $this->user->username) . '§';
return false;
}
if (!$this->storeToDatabase()) {
$this->msg .= 'error§' . sprintf(_('BenutzerIn "%s" konnte nicht angelegt werden.'), $this->user->username) . '§';
return false;
}
$this->msg .= 'msg§' . sprintf(_('BenutzerIn "%s" (vorläufig) angelegt.'), $this->user->username) . '§';
// add default visibility settings
Visibility::createDefaultCategories($this->user->id);
return true;
}
/**
* Change an existing studip user according to the given parameters
*
* @param array structure: array('string table_name.field_name'=>'string value')
* @return bool Change successful?
*/
public function changeUser($newuser)
{
global $perm;
// Do we have permission to do so?
if (!$perm->have_perm('admin')) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung Accounts zu verändern.') . '§';
return false;
}
if (!$perm->is_fak_admin() && $newuser['auth_user_md5.perms'] === 'admin') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung, <em>Admin-Accounts</em> anzulegen.') . '§';
return false;
}
if (!$perm->have_perm('root') && $newuser['auth_user_md5.perms'] === 'root') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung, <em>Root-Accounts</em> anzulegen.') . '§';
return false;
}
if (!$perm->have_perm('root')) {
if (!$perm->is_fak_admin() && $this->user_data['auth_user_md5.perms'] === 'admin') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung <em>Admin-Accounts</em> zu verändern.') . '§';
return false;
}
if ($this->user_data['auth_user_md5.perms'] === 'root') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung <em>Root-Accounts</em> zu verändern.') . '§';
return false;
}
if ($perm->is_fak_admin() && $this->user_data['auth_user_md5.perms'] === 'admin') {
if (!$this->adminOK()) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung diesen Admin-Account zu verändern.') . '§';
return false;
}
}
}
// active dozent? (ignore the studygroup guys)
$status = studygroup_sem_types();
if (empty($status)) {
$count = 0;
} else {
$query = "SELECT COUNT(*)
FROM seminar_user AS su
LEFT JOIN seminare AS s USING (Seminar_id)
WHERE su.user_id = ?
AND s.status NOT IN (?)
AND su.status = 'dozent'
AND (SELECT COUNT(*) FROM seminar_user su2 WHERE Seminar_id = su.Seminar_id AND su2.status = 'dozent') = 1
GROUP BY user_id";
$statement = DBManager::get()->prepare($query);
$statement->execute([
$this->user_data['auth_user_md5.user_id'],
$status,
]);
$count = $statement->fetchColumn();
}
if ($count && isset($newuser['auth_user_md5.perms']) && $newuser['auth_user_md5.perms'] !== 'dozent') {
$this->msg .= 'error§' . sprintf(_('Der Benutzer <em>%s</em> ist alleiniger Lehrperson in %s aktiven Veranstaltungen und kann daher nicht in einen anderen Status versetzt werden!'), $this->user_data['auth_user_md5.username'], $count) . '§';
return false;
}
// active admin?
if ($this->user_data['auth_user_md5.perms'] === 'admin' && $newuser['auth_user_md5.perms'] !== 'admin') {
// count number of institutes where the user is admin
$query = "SELECT COUNT(*)
FROM user_inst
WHERE user_id = ? AND inst_perms = 'admin'
GROUP BY Institut_id";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
// if there are institutes with admin-perms, add error-message and deny change
if ($count = $statement->fetchColumn()) {
$this->msg .= 'error§'. sprintf(_('Der Benutzer <em>%s</em> ist Admin in %s Einrichtungen und kann daher nicht in einen anderen Status versetzt werden!'), $this->user_data['auth_user_md5.username'], $count) . '§';
return false;
}
}
// Is the username correct?
if (isset($newuser['auth_user_md5.username'])) {
if ($this->user_data['auth_user_md5.username'] != $newuser['auth_user_md5.username']) {
if (!$this->validator->ValidateUsername($newuser['auth_user_md5.username'])) {
$this->msg .= 'error§' . _('Der gewählte Benutzername ist zu kurz oder enthält unzulässige Zeichen!') . '§';
return false;
}
$check_uname = StudipAuthAbstract::CheckUsername($newuser['auth_user_md5.username']);
if ($check_uname['found']) {
$this->msg .= 'error§' . _('Der Benutzername wird bereits von einem anderen Benutzer verwendet. Bitte wählen Sie einen anderen Benutzernamen!') . '§';
return false;
} else {
//$this->msg .= "info§" . $check_uname['error'] ."§";
}
} else
unset($newuser['auth_user_md5.username']);
}
// Can we reach the email?
if (isset($newuser['auth_user_md5.Email'])) {
if (!$this->checkMail($newuser['auth_user_md5.Email'])) {
return false;
}
}
// Store changed values in internal array if allowed
$old_perms = $this->user_data['auth_user_md5.perms'];
$auth_plugin = $this->user_data['auth_user_md5.auth_plugin'];
foreach ($newuser as $key => $value) {
if (!StudipAuthAbstract::CheckField($key, $auth_plugin)) {
$this->user_data[$key] = $value;
} else if ($this->user_data[$key] !== $value) {
$this->msg .= 'error§' . sprintf(_('Das Feld <em>%s</em> können Sie nicht ändern!'), $key) . '§';
return false;
}
}
if (!$this->storeToDatabase()) {
$this->msg .= 'info§' . _('Es wurden keine Veränderungen der Grunddaten vorgenommen.') . '§';
return true;
}
$this->msg .= 'msg§' . sprintf(_('Benutzer "%s" verändert.'), $this->user_data['auth_user_md5.username']) . '§';
if ($auth_plugin !== null) {
// Automated entering new users, based on their status (perms)
$result = AutoInsert::instance()->saveUser($this->user_data['auth_user_md5.user_id'], $newuser['auth_user_md5.perms']);
foreach ($result['added'] as $item) {
$this->msg .= 'msg§' . sprintf(_('Das automatische Eintragen in die Veranstaltung <em>%s</em> wurde durchgeführt.'), $item) . '§';
}
foreach ($result['removed'] as $item) {
$this->msg .= 'msg§' . sprintf(_('Das automatische Austragen aus der Veranstaltung <em>%s</em> wurde durchgeführt.'), $item) . '§';
}
// include language-specific subject and mailbody
$user_language = getUserLanguagePath($this->user_data['auth_user_md5.user_id']);
$Zeit = strftime('%x, %X');
// TODO: This should be refactored so that the included file returns an array
include "locale/{$user_language}/LC_MAILS/change_mail.inc.php"; // Defines $subject and $mailbody
StudipMail::sendMessage($this->user_data['auth_user_md5.Email'], $subject ?? '', $mailbody ?? '');
}
// Upgrade to admin or root?
if (in_array($newuser['auth_user_md5.perms'], ['admin', 'root'])) {
$this->re_sort_position_in_seminar_user();
// delete all seminar entries
$course_member = SimpleCollection::createFromArray(
CourseMember::findByUser($this->user_data['auth_user_md5.user_id'])
);
$seminar_ids = $course_member->pluck('seminar_id');
$count = 0;
foreach($course_member as $member) {
$member->delete();
$count++;
}
if ($count) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus Veranstaltungen gelöscht.'), $count) . '§';
array_map('AdmissionApplication::addMembers', $seminar_ids);
}
// delete all entries from waiting lists
$admission_members = SimpleCollection::createFromArray(
AdmissionApplication::findByUser($this->user_data['auth_user_md5.user_id'])
);
$seminar_ids = $admission_members->pluck('seminar_id');
$count = 0;
foreach ($admission_members as $admission_member) {
$admission_member->delete();
$count++;
}
if ($count) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus Wartelisten gelöscht.'), $count) . '§';
array_map('AdmissionApplication::addMembers', $seminar_ids);
}
// delete 'Studiengaenge'
if ($count = UserStudyCourse::deleteBySQL('user_id = ?', [$this->user_data['auth_user_md5.user_id']])) {
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
$this->msg .= 'info§' . sprintf(_('%s Zuordnungen zu Studiengängen gelöscht.'), $count) . '§';
}
// delete all private appointments of this user
if ($count = delete_range_of_dates($this->user_data['auth_user_md5.user_id'], false)) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus den Terminen gelöscht.'), $count) . '§';
}
}
if ($newuser['auth_user_md5.perms'] === 'admin') {
$this->logInstUserDel($this->user_data['auth_user_md5.user_id'], "inst_perms != 'admin'");
$query = "DELETE FROM user_inst WHERE user_id = ? AND inst_perms != 'admin'";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus MitarbeiterInnenlisten gelöscht.'), $count) . '§';
}
}
if ($newuser['auth_user_md5.perms'] === 'root') {
$this->logInstUserDel($this->user_data['auth_user_md5.user_id']);
$query = "DELETE FROM user_inst WHERE user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus MitarbeiterInnenlisten gelöscht.'), $count) . '§';
}
}
return true;
}
private function logInstUserDel($user_id, $condition = null)
{
$query = "SELECT Institut_id FROM user_inst WHERE user_id = ?";
if (isset($condition)) {
$query .= ' AND ' . $condition;
}
$statement = DBManager::get()->prepare($query);
$statement->execute([$user_id]);
while ($institute_id = $statement->fetchColumn()) {
StudipLog::log('INST_USER_DEL', $institute_id, $user_id);
}
}
/**
* Mail a password generation link to the user
*
* @return bool Password change successful?
*/
public function setPassword()
{
global $perm;
// Do we have permission to do so?
if (!$perm->have_perm('admin')) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung Accounts zu verändern.') . '§';
return false;
}
if (!$perm->have_perm('root')) {
if ($this->user_data['auth_user_md5.perms'] === "root") {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung <em>Root-Accounts</em> zu verändern.') . '§';
return false;
}
if ($perm->is_fak_admin() && $this->user_data['auth_user_md5.perms'] === 'admin') {
if (!$this->adminOK()) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung diesen Admin-Account zu verändern.') . '§';
return false;
}
}
}
// Can we reach the email?
if (!$this->checkMail($this->user_data['auth_user_md5.Email'])) {
return false;
}
self::sendPasswordMail($this->user);
return true;
}
/**
* Send a mail to the user denoted by the passed user-object with a link
* to reset the password. For admin, root and non-standard-auth a notification
* is sent instead.
*
* @param User $user
*
* @return void
*/
public static function sendPasswordMail($user, $new = false)
{
setTempLanguage($user->user_id);
// always generate a token, so root, admin and all other users profit from the abuse protection

Jan-Hendrik Willms
committed
if ($new) {
$expiration_in_hours = 24;
$spoken_expiration = _('24 Stunden');
} else {
$expiration_in_hours = 7 * 24;
$spoken_expiration = _('eine Woche');
}
$token = Token::create($expiration_in_hours * 60 * 60, $user->id, true);
// new users alawys receive a link to generate a password
if ($new) {
$subject = sprintf(
_("[Stud.IP - %s] Es wurde ein Zugang für sie erstellt - Setzen sie ein Passwort"),
Config::get()->UNI_NAME_CLEAN
);
$mailbody = sprintf(
_("Dies ist eine Bestätigungsmail des Stud.IP-Systems\n"

Jan-Hendrik Willms
committed
."(Studienbegleitender Internetsupport von Präsenzlehre)\n- %1\$s -\n\n"
."Es wurde für sie ein Zugang zum System erstellt, Ihr Nutzername lautet:\n\n"

Jan-Hendrik Willms
committed
."%2\$s\n\n"
."Um den Zugang nutzen zu können, müssen sie ein Passwort setzen.\n"
."Öffnen Sie dafür bitte folgenden Link\n\n"

Jan-Hendrik Willms
committed
."%3\$s\n\n"

Jan-Hendrik Willms
committed
."Der Link ist %4\$s (bis %5\$s) gültig.\n\n"
."Wahrscheinlich unterstützt Ihr E-Mail-Programm ein einfaches Anklicken des Links.\n"
."Ansonsten müssen Sie Ihren Browser öffnen und den Link komplett in die Zeile\n"
."\"Location\" oder \"URL\" kopieren.\n\n"
),
Config::get()->UNI_NAME_CLEAN,
$user->username,

Jan-Hendrik Willms
committed
$GLOBALS['ABSOLUTE_URI_STUDIP'] . 'dispatch.php/new_password/set/'. $token->token .'?cancel_login=1',
$spoken_expiration,
strftime('%x %X', $token->expiration)
);
} else
// only users with auth-type standard cann reset their password
if ($user->auth_plugin !== 'standard') {
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
// inform user, that their password cannot be reset via mail
$subject = sprintf(
_("[Stud.IP - %s] Passwortänderung angefordert"),
Config::get()->UNI_NAME_CLEAN
);
$mailbody = sprintf(
_("Dies ist eine Informationsmail des Stud.IP-Systems\n"
."(Studienbegleitender Internetsupport von Präsenzlehre)\n- %s -\n\n"
. "Sie haben einen Link angefordert\n"
. "um das Passwort zurückzusetzen.\n"
. "Dies ist aber für den mit dieser Mail \n"
. "verknüpften Account so nicht möglich.\n\n"
. "Wenden sie sich bitte stattdessen an\n%s"
),
Config::get()->UNI_NAME_CLEAN,
$GLOBALS['UNI_CONTACT']
);
} else {
$subject = sprintf(
_("[Stud.IP - %s] Neues Passwort setzen"),
Config::get()->UNI_NAME_CLEAN
);
$mailbody = sprintf(
_("Dies ist eine Bestätigungsmail des Stud.IP-Systems\n"

Jan-Hendrik Willms
committed
."(Studienbegleitender Internetsupport von Präsenzlehre)\n- %1\$s -\n\n"
."Sie haben um die Zurücksetzung des Passwortes zu Ihrem Benutzernamen %5\$s gebeten.\n\n"
."Diese E-Mail wurde Ihnen zugesandt um sicherzustellen,\n"
."dass die angegebene E-Mail-Adresse tatsächlich Ihnen gehört.\n\n"
."Wenn Sie um die Zurücksetzung Ihres Passwortes gebeten haben,\n"
."dann öffnen Sie bitte folgenden Link\n\n"

Jan-Hendrik Willms
committed
."%2\$s\n\n"
."in Ihrem Browser. Auf der Seite können Sie ein neues Passwort setzen.\n\n"

Jan-Hendrik Willms
committed
."Der Link ist %3\$s (bis %4\$s) gültig.\n\n"
."Wahrscheinlich unterstützt Ihr E-Mail-Programm ein einfaches Anklicken des Links.\n"
."Ansonsten müssen Sie Ihren Browser öffnen und den Link komplett in die Zeile\n"
."\"Location\" oder \"URL\" kopieren.\n\n"
."Falls Sie nicht diese Mail nicht angefordert haben\n"
."oder überhaupt nicht wissen, wovon hier die Rede ist,\n"
."dann hat jemand Ihre E-Mail-Adresse fälschlicherweise verwendet!\n"
."Ignorieren Sie in diesem Fall diese E-Mail. Es werden dann keine\n"
."Änderungen an Ihren Zugangsdaten vorgenommen.\n\n"
),
Config::get()->UNI_NAME_CLEAN,

Jan-Hendrik Willms
committed
$GLOBALS['ABSOLUTE_URI_STUDIP'] . 'dispatch.php/new_password/set/'. $token->token .'?cancel_login=1',
$spoken_expiration,
strftime('%x %X', $token->expiration),
$user->username
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
);
}
StudipMail::sendMessage($user->email, $subject, $mailbody);
restoreLanguage();
}
/**
* Delete an existing user from the database and tidy up
*
* @param bool delete all documents in course context belonging to the user
* @param bool delete all course content belonging to the user
* @param bool delete all personal documents belonging to the user
* @param bool delete all personal content belonging to the user
* @param bool delete all names identifying the user
* @param bool delete all memberships of the user
* @return bool Removal successful?
*/
public function deleteUser($delete_documents = true, $delete_content_from_course = true, $delete_personal_documents = true, $delete_personal_content = true, $delete_names = true, $delete_memberships = true)
{
global $perm;
// Do we have permission to do so?
if (!$perm->have_perm('admin')) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung Accounts zu löschen.') . '§';
return FALSE;
}
if (!$perm->have_perm('root')) {
if ($this->user_data['auth_user_md5.perms'] === 'root') {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung <em>Root-Accounts</em> zu löschen.') . '§';
return false;
}
if ($this->user_data['auth_user_md5.perms'] === 'admin' && !$this->adminOK()) {
$this->msg .= 'error§' . _('Sie haben keine Berechtigung diesen Admin-Account zu löschen.') . '§';
return false;
}
}
// active dozent?
$query = "SELECT COUNT(*)
FROM (
SELECT 1
FROM `seminar_user` AS `su1`
-- JOIN seminar_user to check for other teachers
INNER JOIN `seminar_user` AS `su2`
ON (`su1`.`seminar_id` = `su2`.`seminar_id` AND `su2`.`status` = 'dozent')
-- JOIN seminare to check the status for studygroup mode
INNER JOIN `seminare`
ON (`su1`.`seminar_id` = `seminare`.`seminar_id`)
WHERE `su1`.`user_id` = :user_id
AND `su1`.`status` = 'dozent'
AND `seminare`.`status` NOT IN (
-- Select all status ids for studygroups
SELECT `id`
FROM `sem_classes`
WHERE `studygroup_mode` = 1
)
GROUP BY `su1`.`seminar_id`
HAVING COUNT(*) = 1
ORDER BY NULL
) AS `sub`";
$statement = DBManager::get()->prepare($query);
$statement->bindValue(':user_id', $this->user_data['auth_user_md5.user_id']);
$statement->execute();
$active_count = $statement->fetchColumn() ?: 0;
if ($active_count && $delete_memberships) {
$this->msg .= 'error§' . sprintf(_('<em>%s</em> ist Lehrkraft in %s aktiven Veranstaltungen und kann daher nicht gelöscht werden.'), $this->user_data['auth_user_md5.username'], $active_count) . '§';
return false;
//founder of studygroup?
} elseif (Config::get()->STUDYGROUPS_ENABLE) {
$status = studygroup_sem_types();
if (empty($status)) {
$group_ids = [];
} else {
$query = "SELECT Seminar_id
FROM seminare AS s
LEFT JOIN seminar_user AS su USING (Seminar_id)
WHERE su.status = 'dozent' AND su.user_id = ? AND s.status IN (?)";
$statement = DBManager::get()->prepare($query);
$statement->execute([
$this->user_data['auth_user_md5.user_id'],
$status,
]);
$group_ids = $statement->fetchAll(PDO::FETCH_COLUMN);
}
foreach ($group_ids as $group_id) {
$sem = Seminar::GetInstance($group_id);
if (StudygroupModel::countMembers($group_id) > 1) {
// check whether there are tutors or even autors that can be promoted
$tutors = $sem->getMembers('tutor');
$autors = $sem->getMembers('autor');
if (count($tutors) > 0) {
$new_founder = current($tutors);
StudygroupModel::promote_user($new_founder['username'], $sem->getId(), 'dozent');
continue;
}
// if not promote an autor
elseif (count($autors) > 0) {
$new_founder = current($autors);
StudygroupModel::promote_user($new_founder['username'], $sem->getId(), 'dozent');
continue;
}
// since no suitable successor was found, we are allowed to remove the studygroup
} else {
$sem->delete();
}
unset($sem);
}
}
// store user preferred language for sending mail
$user_language = getUserLanguagePath($this->user_data['auth_user_md5.user_id']);
// Load privacy plugins to ensure all event handlers can react to the
// UserDataDidRemove event
PluginEngine::getPlugins('PrivacyPlugin');
// delete user from instituts
$this->logInstUserDel($this->user_data['auth_user_md5.user_id']);
if ($delete_memberships) {
$query = "DELETE FROM user_inst WHERE user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus MitarbeiterInnenlisten gelöscht.'), $count) . '§';
}
// delete user from Statusgruppen
if ($count = StatusgruppeUser::deleteBySQL('user_id = ?', [$this->user_data['auth_user_md5.user_id']])) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus Funktionen / Gruppen gelöscht.'), $count) . '§';
}
// delete user from archiv
$query = "DELETE FROM archiv_user WHERE user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus den Zugriffsberechtigungen für das Archiv gelöscht.'), $count) . '§';
}
// delete 'Studiengaenge'
$query = "DELETE FROM user_studiengang WHERE user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Zuordnungen zu Studiengängen gelöscht.'), $count) . '§';
}
$this->re_sort_position_in_seminar_user();
// delete user from seminars (postings will be preserved)
$query = "DELETE FROM seminar_user WHERE user_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%s Einträge aus Veranstaltungen gelöscht.'), $count) . '§';
}
$query = "DELETE FROM `termin_related_persons` WHERE `user_id` = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
if ($count = $statement->rowCount()) {
$this->msg .= 'info§' . sprintf(_('%u Terminzuordnungen gelöscht.'), $count) . '§';
}
// delete visibility settings
Visibility::removeUserPrivacySettings($this->user_data['auth_user_md5.user_id']);
// delete deputy entries if necessary
$query = "DELETE FROM deputies WHERE ? IN (user_id, range_id)";
$statement = DBManager::get()->prepare($query);
$statement->execute([$this->user_data['auth_user_md5.user_id']]);
$deputyEntries = $statement->rowCount();
if ($deputyEntries) {
$this->msg .= 'info§' . sprintf(_('%s Einträge in den Vertretungseinstellungen gelöscht.'), $deputyEntries) . '§';
}
// delete all remaining user data
$queries = [
"DELETE FROM user_userdomains WHERE user_id = ?",
];
foreach ($queries as $query) {
DBManager::get()->execute($query, [$this->user_data['auth_user_md5.user_id']]);
}
NotificationCenter::postNotification('UserDataDidRemove', $this->user_data['auth_user_md5.user_id'], 'memberships');
}
// delete documents of this user
if ($delete_documents) {
$db_filecount = FileRef::deleteBySQL('user_id = ?', [$this->user_data['auth_user_md5.user_id']]);
if ($db_filecount > 0) {
$this->msg .= 'info§' . sprintf(_('%s Dateien aus Veranstaltungen und Einrichtungen gelöscht.'), $db_filecount) . '§';
}
NotificationCenter::postNotification('UserDataDidRemove', $this->user_data['auth_user_md5.user_id'], 'course_documents');
}
// delete all remaining user data in course context if option selected
if ($delete_content_from_course) {
$queries = [
"DELETE FROM questionnaires WHERE user_id = ?",
"DELETE FROM questionnaire_answers WHERE user_id = ?",
"DELETE FROM questionnaire_assignments WHERE user_id = ?",
"DELETE FROM questionnaire_anonymous_answers WHERE user_id = ?",
"DELETE FROM etask_assignment_attempts WHERE user_id = ?",
"DELETE FROM etask_responses WHERE user_id = ?",
"DELETE FROM etask_tasks WHERE user_id = ?",
"DELETE FROM etask_tests WHERE user_id = ?",
];
foreach ($queries as $query) {
DBManager::get()->execute($query, [$this->user_data['auth_user_md5.user_id']]);
}
NotificationCenter::postNotification('UserDataDidRemove', $this->user_data['auth_user_md5.user_id'], 'course_contents');
}
if ($delete_personal_documents) {
$user_folder = Folder::findTopFolder($this->user->id);
if ($user_folder) {
$this->msg .= 'info§' . _('Persönlicher Dateibereich gelöscht.') . '§';
$user_folder->delete();