aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/MvvPerm.php
blob: 93f9f4404201e1c9e0e10cda3cd7ce265accc0d2 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
262
263
264
265
266
267
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
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
<?php
/**
 * MvvPerm.php
 *
 *
 * 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      Peter Thienel <thienel@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.5
 */

class MvvPerm {

    /**
     * Permission to read the value of the designated field.
     */
    const PERM_READ = 1;

    /**
     * Permission to read and write the value of the designated field.
     */
    const PERM_WRITE = 3;

    /**
     * Permission to read, write/create and delete the value
     * of the designated field.
     */
    const PERM_CREATE = 7;

    /**
    * Permission to read, write/create and delete the value
    * of the designated field. Possibly the admin have permission to
    * particular functions.
    */
    const PERM_ADMIN = 15;

    private static $privileges;

    private static $roles;

    private static $user_role_institutes;

    /**
     * The actual mvv object.
     *
     * @var object
     */
    private $mvv_object;

    /**
     * to distinguish config files between different languages
     *
     * @var string
     */
    private $variant;

    /**
     * Creates a new perm object for given mvv object.
     *
     * @param ModuleManagementModel $mvv_object
     */
    public function __construct(ModuleManagementModel $mvv_object)
    {
        $this->mvv_object = $mvv_object;
    }

    /**
     * Returns a new perm object for given mvv object.
     *
     * @param ModuleManagementModel|string $mvv_object Class name or instance of
     * mvv object.
     * @return MvvPerm A new perm object.
     * @throws InvalidArgumentException
     */
    public static function get($mvv_object)
    {
        static $perm_objects;

        if (is_object($mvv_object)) {
            $index = get_class($mvv_object) . $mvv_object->id;
        } else {
            $index = $mvv_object;
        }
        if (empty($perm_objects[$index])) {
            if (!is_object($mvv_object)) {
                $mvv_class_name = (string) $mvv_object;
                $mvv_object = new $mvv_class_name;
            }

            if (!$mvv_object instanceof ModuleManagementModel) {
                throw new InvalidArgumentException('Wrong object type. Only MVV '
                        . 'objects of type ModuleManagementModel are allowed.');
            }

            $perm_objects[$index] = new MvvPerm($mvv_object);
            $perm_objects[$index]->setVariant($mvv_object->getVariant());
        }
        return $perm_objects[$index];
    }

    /**
     * Intercepts static calls to retrieve permission from configuration.
     *
     * Allowed calls are:
     *
     * MvvPerm::getFieldPermFIELD($mvv_object, $user_id, $institut_id)
     *
     * @see MvvPerm:getFieldPerm()
     * FIELD is the name of the table field.
     * $mvv_object: Class name or instance of mvv object.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * Last two arguments are optional.
     *
     * MvvPerm::havePermPERM($mvv_object, $status, $user_id, $institut_id)
     *
     * @see MvvPerm::havePerm()
     * PERM is the constant defining the permission (MvvPerm::PERM_ADMIN,
     * MvvPerm::PERM_CREATE, MvvPerm::PERM_READ, MvvPerm::PERM_WRITE)
     * $mvv_object: Class name or instance of mvv object.
     * $status: The status of mvv object defined in config. Status 'default' as
     * default.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * Last three arguments are optional
     *
     * MvvPerm::haveFieldPermFIELD($mvv_object, $perm, $user_id, $institut_id)
     *
     * @see MvvPerm::haveFieldPerm()
     * FIELD is the name of the table field.
     * $mvv_object: Class name or instance of mvv object.
     * $perm: The constant defining the permission (MvvPerm::PERM_ADMIN,
     * MvvPerm::PERM_CREATE, MvvPerm::PERM_READ, MvvPerm::PERM_WRITE).
     * Default is MvvPerm::PERM_WRITE.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * Last three arguments are optional.
     *
     * @param type $name
     * @param type $arguments
     * @return type
     * @throws InvalidArgumentException If called with unknown permission.
     * @throws BadMethodCallException If called with unknown method to
     * retrieve permission.
     */
    public static function __callStatic($name, $arguments)
    {
        $name = mb_strtolower($name);

        if (mb_strpos($name, 'haveperm') === 0) {
            $perm = 'PERM_' . mb_strtoupper(mb_substr($name, 8));
            if (defined('self::' . $perm)) {
                return self::get($arguments[0])->havePerm(
                    constant('self::' . $perm),
                    ...array_slice($arguments, 1)
                );
            } else {
                throw new InvalidArgumentException('Undefined Permission.');
            }
        }

        if (mb_strpos($name, 'getfieldperm') === 0) {
            $field = mb_strtolower(mb_substr($name, 12));
            return self::get($arguments[0])->getFieldPerm(
                $field,
                ...array_slice($arguments, 1)
            );
        }

        if (mb_strpos($name, 'havefieldperm') === 0) {
            $field = mb_strtolower(mb_substr($name, 13));
            return self::get($arguments[0])->haveFieldPerm(
                $field,
                ...array_slice($arguments, 1)
            );
        }

        throw new BadMethodCallException('Method '
                . get_called_class() . "::$name not found");
    }

    /**
     * Intercepts instance methods to retrieve permission from configuration.
     *
     * Allowed calls are:
     *
     * havePermPERM($status, $user_id, $institut_id)
     *
     * @see MvvPerm::havePerm()
     * PERM is the constant defining the permission (MvvPerm::PERM_ADMIN,
     * MvvPerm::PERM_CREATE, MvvPerm::PERM_READ, MvvPerm::PERM_WRITE)
     * $status: The status of mvv object defined in config. Status 'default' as
     * default.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * All three arguments are optional
     *
     * getFieldPermFIELD($user_id, $institut_id)
     *
     * @see MvvPerm::getFieldPerm()
     * FIELD is the name of the table field.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * All two arguments are optional.
     *
     *
     * haveFieldPermFIELD($perm, $user_id, $institut_id)
     *
     * @see MvvPerm::haveFieldPerm()
     * FIELD is the name of the table field.
     * $perm: The constant defining the permission (MvvPerm::PERM_ADMIN,
     * MvvPerm::PERM_CREATE, MvvPerm::PERM_READ, MvvPerm::PERM_WRITE).
     * Default is MvvPerm::PERM_WRITE.
     * $user_id: The id of an user. Id of current user as default.
     * $institut_id: The id of an institute or an array of institute ids the
     * user has a mvv related role.
     * All three arguments are optional.
     *
     *
     * @param string $name
     * @param array $arguments
     * @return mixed
     * @throws InvalidArgumentException If called with unknown permission.
     * @throws BadMethodCallException If called with unknown method to
     * reitrieve permission.
     */
    public function __call($name, $arguments)
    {
        $name = mb_strtolower($name);

        if (mb_strpos($name, 'haveperm') === 0) {
            $perm = 'PERM_' . mb_strtoupper(mb_substr($name, 8));
            if (defined('self::' . $perm)) {
                return $this->havePerm(
                    constant('self::' . $perm),
                    ...$arguments
                );
            } else {
                throw new InvalidArgumentException('Undefined Permission.');
            }
        }

        if (mb_strpos($name, 'getfieldperm') === 0) {
            $field = mb_strtolower(mb_substr($name, 12));
            return $this->getFieldPerm($field,
                    $arguments[0], $arguments[1]);
        }

        if (mb_strpos($name, 'havefieldperm') === 0) {
            $field = mb_strtolower(mb_substr($name, 13));
            return $this->haveFieldPerm($field, ...$arguments);
        }

        throw new BadMethodCallException('Method '
                . get_called_class() . "::$name not found");
    }

    /**
     * Sets the variant of an mvv object. The variant means that a different
     * configuration file is used to retrieve the permissions. It is used to
     * determines permissions for different languages of a descriptor.
     *
     * @param string $variant The suffix (part after las underscore) of the
     * file name of a config file.
     * @return $this Returns this instance for method chaining.
     */
    public function setVariant($variant)
    {
        $this->variant = trim($variant);
        return $this;
    }

    /**
     * Retrieves the permission for the table or object.
     * Returns the (max) perm the given or current user has globally.
     * If the institute is set, checks for the perm in the context of this institute.
     *
     * @param object|string $object An mvv object or its class name.
     * @param string $status Constrain the permission to this status of the object.
     * @param string $user_id The ID of the user. If not set the ID of the
     * current user.
     * @param string|array $institut_id The ID(s) of institute(s)
     * if the user role is defined for institutes.
     * @param string $variant The optional variant of the object table (e.g. to
     * support different languages)
     * @return int The perm defined in class MVVPlugin.
     * @throws InvalidArgumentException If the object is not of type
     * ApplicationSimpleOrMap.
     */
    public final static function getPerm($object, $status = null,
            $user_id = null, $institut_id = null, $variant = null)
    {
        if (is_object($object)) {
            $mvv_object = $object;
        } else {
            $mvv_object = new $object;
        }
        if (!$mvv_object instanceof ModuleManagementModel) {
            throw new InvalidArgumentException('Wrong object type. Only MVV '
                        . 'objects of type ModuleManagementModel are allowed.');
        }
        $user_id = is_null($user_id) ? $GLOBALS['user']->id : $user_id;
        if ($GLOBALS['perm']->get_perm($user_id) == 'root' ) {
            return self::PERM_ADMIN;
        }
        if (is_null($institut_id)) {
            $inst_ids = [];
            foreach ($mvv_object->getResponsibleInstitutes() as $inst) {
                if ($inst) {
                    $inst_ids[] = $inst->getId();
                    // the user have permission if he is assigned to the faculty
                    $inst_ids[] = $inst->fakultaets_id;
                }
            }
        } else {
            $inst_ids = [];
            foreach (Institute::findMany((array) $institut_id) as $inst) {
                $inst_ids[] = $inst->getId();
                // the user have permission if he is assigned to the faculty
                $inst_ids[] = $inst->fakultaets_id;
            }
        }
        $table_meta = $mvv_object->getTableMetadata();
        $institut_ids = array_unique($inst_ids);
        $status = $status ?: $mvv_object->getStatus();
        $roles = self::getRoles($user_id);
        $variant = $variant ?: $mvv_object->getVariant();
        $mvv_table = $table_meta['table'] . ($variant ? '_' . $variant : '');
        self::getPrivileges($mvv_table);
        $perm = 0;
        foreach ($roles as $role) {
            if ($role->rolename === 'MVVAdmin') {
                return self::PERM_ADMIN;
            }
            if (count($institut_ids)) {
                $institutes_assigned_role = RolePersistence::getAssignedRoleInstitutes($user_id, $role->roleid);
                // count($institutes_assigned_role) === 0 means global role.
                if (count($institutes_assigned_role) === 0
                        || count(array_intersect($institut_ids, $institutes_assigned_role))) {
                    if (!$status) {
                        $priv = intval(self::$privileges[$mvv_table]['default_table'][$role->rolename]);
                    } else {
                        $priv = intval(self::$privileges[$mvv_table]['table'][$status][$role->rolename]);
                    }
                } else {
                    $priv = 0;
                }
            } else {
                if (!$status) {
                    $priv = intval(self::$privileges[$mvv_table]['default_table'][$role->rolename]);
                } else {
                    $priv = intval(self::$privileges[$mvv_table]['table'][$status][$role->rolename]);
                }
            }

            if ($priv > $perm) {
                $perm = $priv;
            }
        }
        return $perm;
    }

    /**
     * Accepts the id of the user, an institute id and the status of this object
     * as optional arguments. Returns whether the user has the given permission
     * to this object.
     *
     * The status of this object is defined in the configuration file
     *
     * @param int $perm The permission to check against.
     * @param string $status The status of the object defined in config.
     * @param string $user_id The id of the user.
     * @param string|array $institut_id The id of an institute or an array of
     * institute ids the user has a mvv related role.
     * @return bool
     */
    public final function havePerm($perm, $status = null,
            $user_id = null, $institut_id = null)
    {
        $user_perm = self::getPerm($this->mvv_object, $status, $user_id,
                $institut_id, $this->variant);
        return ($user_perm >= $perm);
    }

    /**
     * Returns whether the given user has at least the given permission to this
     * object with the actual status. The user_id is optional. Default is the
     * id of the current user.
     *
     * @param int $perm The permission to check against.
     * @param type $user_id The id of an user.
     * @return bool True if the permission is granted.
     */
    public function haveObjectPerm($perm, $user_id = null)
    {
        $institute_ids = SimpleORMapCollection::createFromArray(
                $this->mvv_object->getResponsibleInstitutes())->pluck('id');
        return $this->havePerm($perm, $this->mvv_object->getStatus(), $user_id,
                $institute_ids);
    }

    /**
     * Returns whether the actual user has at least the given permission to
     * the datafield entry.
     *
     * @param $datafield_id The id of a datafield.
     * @param int $perm
     * @return bool True if permission is granted.
     */
    public function haveDfEntryPerm($datafield_id, $perm)
    {
        $field = 'datafields';
        $field_perm = $this->getFieldPerm([$field, $datafield_id]);
        return $field_perm >= $perm;
    }

    /**
     * Retrieves the permission for the table or object.
     * Returns the (max) perm the given or current user have globally.
     * If the institute is set, checks for the perm in the context of this institute.
     *
     * @param string|array Name of field or an array with name of field and
     * related entry (usefull in the case of relations to datafields).
     * @param string $user_id Optional. The ID of the user. If not set the ID of
     * the current user.
     * @param string|array $institut_id Optional. The id of an institute or an
     * array of institute ids the user has a mvv related role.
     * @return int The perm defined in class MVVPlugin.
     */
    public final function getFieldPerm($field, $user_id = null, $institut_id = null)
    {
        $user_id = is_null($user_id) ? $GLOBALS['user']->id : $user_id;
        if ($GLOBALS['perm']->get_perm($user_id) == 'root' ) {
            return self::PERM_CREATE;
        }
        $roles = self::getRoles($user_id);
        $table_meta = $this->mvv_object->getTableMetadata();
        $mvv_table = $table_meta['table']
                . ($this->variant ? '_' . $this->variant : '');
        if (is_null($institut_id)) {
            $institut_ids = array_map(function ($ins) {
                return $ins ? $ins->getId() : null;
            }, $this->mvv_object->getResponsibleInstitutes());
        } else {
            $institut_ids = is_array($institut_id) ? $institut_id : [$institut_id];
        }
        $status = $this->mvv_object->getStatus();
        self::getPrivileges($mvv_table);

        $perm = 0;
        foreach ($roles as $role) {
            if ($role->rolename === 'MVVAdmin') {
                return self::PERM_CREATE;
            }
            if (count($institut_ids)) {
                $institutes_assigned_role = RolePersistence::getAssignedRoleInstitutes($user_id, $role->roleid);
                // count($institutes_assigned_role) === 0 means global role.
                if (count($institutes_assigned_role) === 0
                        || count(array_intersect($institut_ids, $institutes_assigned_role))) {
                    $priv = is_array($field)
                            ? self::$privileges[$mvv_table]['fields'][$field[0]][$field[1]][$status][$role->rolename]
                            : self::$privileges[$mvv_table]['fields'][$field][$status][$role->rolename];
                    if (is_null($priv)) {
                        $priv = is_array($field)
                                ? self::$privileges[$mvv_table]['fields'][$field[0]]['default'][$status][$role->rolename]
                                : self::$privileges[$mvv_table]['fields'][$field]['default'][$role->rolename];
                    }
                    if (is_null($priv)) {
                        $priv = self::$privileges[$mvv_table]['default_fields'][$role->rolename];
                    }
                } else {
                    $priv = 0;
                }
            } else {
                $priv = is_array($field)
                        ? self::$privileges[$mvv_table]['fields'][$field[0]][$field[1]][$status][$role->rolename]
                        : self::$privileges[$mvv_table]['fields'][$field][$status][$role->rolename];
                if (is_null($priv)) {
                    $priv = is_array($field)
                            ? self::$privileges[$mvv_table]['fields'][$field[0]]['default'][$status][$role->rolename]
                            : self::$privileges[$mvv_table]['fields'][$field]['default'][$role->rolename];
                }
                if (is_null($priv)) {
                    $priv = self::$privileges[$mvv_table]['default_fields'][$role->rolename];
                }
            }
            $priv = intval($priv);
            if ($priv > $perm) {
                $perm = $priv;
            }
        }
        return $perm;
    }

    /**
     * Returns true if the user have at least the given permission status.
     *
     * @param string $field The name of the table field.
     * @param int $perm The optional (default PERM_WRITE) permission status.
     * @param string $user_id Optional. The ID of the user. If not set the ID of
     * the current user.
     * @param string|array $institut_id Optional. The id of an institute or an array of
     * institute ids the user has a mvv related role.
     * @return bool True if the permission is at least the given permission.
     */
    public final function haveFieldPerm($field, $perm = null, $user_id = null, $institut_id = null)
    {
        $perm = intval($perm) ?: MvvPerm::PERM_WRITE;
        $user_perm = $this->getFieldPerm($field, $user_id, $institut_id);
        return ($user_perm >= $perm);
    }

    /**
     * Returns 'readonly' if the given user has no access to the given field.
     *
     * @param string $field The name of the table field.
     * @param int $perm Optional. The permission. Defaults to MvvPerm:PERM_WRITE.
     * @param string $user_id Optional. The ID of the user. If not set the ID of
     * the current user.
     * @param string|array $institut_id The id of an institute or an array of
     * institute ids the user has a mvv related role.
     * @return string 'readonly' if the permission is not granted.
     */
    public function disable($field, $perm = null, $user_id = null, $institut_id = null)
    {
        $perm = intval($perm) ?: MvvPerm::PERM_WRITE;
        return $this->haveFieldPerm($field, $perm, $user_id, $institut_id)
                ? '' : ' readonly ';
    }

    /**
     *
     *
     * @param string $user_id
     * @return
     */
    public static function getRoles($user_id)
    {
        if (!isset(self::$roles[$user_id])) {
            self::$roles[$user_id] = array_filter(
                RolePersistence::getAssignedRoles($user_id),
                function ($role) {
                    return stripos($role->rolename, 'mvv') === 0;
                }
            );
        }
        return self::$roles[$user_id];
    }

    /**
     * Retrieves all privileges from config files.
     */
    private static function getPrivileges($mvv_table)
    {
        if (self::$privileges === null) {
            $cache = StudipCacheFactory::getCache();
            self::$privileges = unserialize($cache->read(MVV::CACHE_KEY . '/privileges'));
        }

        if (self::$privileges[$mvv_table] === null) {
            $config_dir = $GLOBALS['STUDIP_BASE_PATH'] . '/config/mvvconfig';
            if ($config_dir) {
                $config_file = $config_dir . '/' . $mvv_table . '.php';
                if (filetype($config_file) === 'file') {
                    // TODO: This should be refactored in a way that $config_file returns an array
                    include $config_file; // Defines $privileges
                    self::$privileges[$mvv_table] = $privileges ?? [];
                }
                $cache = StudipCacheFactory::getCache();
                $cache->write(MVV::CACHE_KEY . '/privileges', serialize(self::$privileges));
            }
        }
        return isset(self::$privileges[$mvv_table]);
    }

    public static function refreshPrivileges($mvv_table)
    {
        self::$privileges[$mvv_table] = null;
        return self::getPrivileges($mvv_table);
    }

    /**
     * Returns all ids of institutes the user is assigned with at least one role.
     * Have the user at least one role globally an empty array is returned.
     *
     * @param string $user_id Optional. The ID of the user. If not set the ID of
     * the current user.
     * @param array $mvv_roles Optional. An array of roles. All Mvv-Roles if not set.
     * @return array An array of institute ids or an empty array.
     */
    public static function getOwnInstitutes($user_id = null, $mvv_roles = null)
    {
        $user_id = $user_id ?: $GLOBALS['user']->id;
        $roles = [];
        if (is_null($mvv_roles)) {
            $roles = self::getRoles($user_id);
        } else {
            foreach (self::getRoles($user_id) as $role) {
                if (in_array($role->rolename, (array) $mvv_roles)) {
                    $roles[] = $role;
                }
            }
        }

        if (!isset(self::$user_role_institutes[$user_id])) {
            $institutes = [];
            foreach ($roles as $role) {

                // don't check system roles or roles not related to MVV
                if (stripos($role->rolename, 'MVV') !== 0) continue;

                if ($GLOBALS['perm']->have_perm('root', $user_id)) {
                    $institutes = [];
                    break;
                }
                $institutes_assigned_role = RolePersistence::getAssignedRoleInstitutes($user_id, $role->roleid);
                if (count($institutes_assigned_role) === 0) {
                    // this role is globally defined for this user
                    $institutes = [];
                    break;
                }
                $institutes = array_merge((array) $institutes, array_filter(
                    $institutes_assigned_role, function ($inst) {
                        return $inst != '';
                    }));
            }

            if (count($institutes)) {
                $stmt = DBManager::get()->prepare('SELECT DISTINCT(Institut_id) '
                        . 'FROM Institute WHERE Institut_id IN (:inst_ids) '
                        . 'OR fakultaets_id IN (:inst_ids)');
                $stmt->execute(['inst_ids' => $institutes]);
                self::$user_role_institutes[$user_id] = $stmt->fetchAll(PDO::FETCH_COLUMN);
            } else {
                self::$user_role_institutes[$user_id] = [];
            }
        }
        return self::$user_role_institutes[$user_id];
    }

}