Passed
Pull Request — master (#155)
by
unknown
09:48
created

Security::getUserRole()   B

Complexity

Conditions 9
Paths 54

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 1
b 0
f 0
nc 54
nop 0
dl 0
loc 34
rs 8.0555
1
<?php
2
namespace EWW\Dpf\Security;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
class Security
18
{
19
    /**
20
     * frontendUserGroupRepository
21
     *
22
     * @var \EWW\Dpf\Domain\Repository\FrontendUserGroupRepository
23
     * @inject
24
     */
25
    protected $frontendUserGroupRepository = null;
26
27
    /**
28
     * frontendUserRepository
29
     *
30
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
31
     * @inject
32
     */
33
    protected $frontendUserRepository = null;
34
35
    const ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
36
    const ROLE_RESEARCHER = "ROLE_RESEARCHER";
37
    const ROLE_LIBRARIAN = "ROLE_LIBRARIAN";
38
39
40
    /**
41
     * Gets the current logged in frontend user
42
     *
43
     * @return null|\EWW\Dpf\Domain\Model\FrontendUser
44
     */
45
    public function getUser()
46
    {
47
        $user = $GLOBALS['TSFE']->fe_user->user;
48
        if (!empty($user) && is_array($user) && array_key_exists('uid', $user)) {
49
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
50
        } else {
51
            return NULL;
52
        }
53
    }
54
55
    /**
56
     * Get the role the user has in the current client
57
     *
58
     * @return string
59
     */
60
    public function getUserRole() {
61
62
        // Get frontend user groups of the client.
63
        $clientFrontendGroups = array();
64
        foreach ($this->frontendUserGroupRepository->findAll() as $clientGroup) {
65
            if ($clientGroup->getKitodoRole()) {
66
                $clientFrontendGroups[$clientGroup->getUid()] = $clientGroup;
67
            }
68
        }
69
70
        // Get frontend user groups of the user.
71
        $frontendUserGroups = array();
72
        $frontendUser = $this->getUser();
73
        if ($frontendUser) {
74
            foreach ($frontendUser->getUsergroup() as $userGroup) {
75
                // Because getUsergroup() does not return objects of the class
76
                // \EWW\Dpf\Domain\Repository\FrontendUserRepository
77
                $userGroup = $this->frontendUserGroupRepository->findByUid($userGroup->getUid());
78
                $frontendUserGroups[$userGroup->getUid()] = $userGroup;
79
            }
80
        }
81
82
        // Get the roles the user has in the current client.
83
        $roles = array();
84
        foreach ($frontendUserGroups as $uid => $group) {
85
            if (array_key_exists($uid, $clientFrontendGroups)) {
86
                $roles[$uid] = $group->getKitodoRole();
87
            }
88
        }
89
90
        if (in_array(self::ROLE_LIBRARIAN, $roles)) return self::ROLE_LIBRARIAN;
91
        if (in_array(self::ROLE_RESEARCHER, $roles)) return self::ROLE_RESEARCHER;
92
93
        return NULL;
94
    }
95
96
97
}