Passed
Pull Request — master (#166)
by
unknown
14:46 queued 04:32
created

FrontendUserHelper::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 1
dl 0
loc 34
rs 8.0555
1
<?php
2
namespace EWW\Dpf\Helper;
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
use EWW\Dpf\Security\Security;
18
19
class FrontendUserHelper
20
{
21
    /**
22
     * frontendUserGroupRepository
23
     *
24
     * @var \EWW\Dpf\Domain\Repository\FrontendUserGroupRepository
25
     * @inject
26
     */
27
    protected $frontendUserGroupRepository = null;
28
29
    /**
30
     * frontendUserRepository
31
     *
32
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
33
     * @inject
34
     */
35
    protected $frontendUserRepository = null;
36
37
    /**
38
     * Get the role the user has in the current client
39
     *
40
     * @param int $feUserUid;
41
     * @return string
42
     */
43
    public function getUserRole($feUserUid) {
44
45
        // Get frontend user groups of the client.
46
        $clientFrontendGroups = array();
47
        foreach ($this->frontendUserGroupRepository->findAll() as $clientGroup) {
48
            if ($clientGroup->getKitodoRole()) {
49
                $clientFrontendGroups[$clientGroup->getUid()] = $clientGroup;
50
            }
51
        }
52
53
        // Get frontend user groups of the user.
54
        $frontendUserGroups = array();
55
        $frontendUser = $this->frontendUserRepository->findByUid($feUserUid);
56
        if ($frontendUser) {
0 ignored issues
show
introduced by
$frontendUser is of type object, thus it always evaluated to true.
Loading history...
57
            foreach ($frontendUser->getUsergroup() as $userGroup) {
58
                // Because getUsergroup() does not return objects of the class
59
                // \EWW\Dpf\Domain\Repository\FrontendUserRepository
60
                $userGroup = $this->frontendUserGroupRepository->findByUid($userGroup->getUid());
61
                $frontendUserGroups[$userGroup->getUid()] = $userGroup;
62
            }
63
        }
64
65
        // Get the roles the user has in the current client.
66
        $roles = array();
67
        foreach ($frontendUserGroups as $uid => $group) {
68
            if (array_key_exists($uid, $clientFrontendGroups)) {
69
                $roles[$uid] = $group->getKitodoRole();
70
            }
71
        }
72
73
        if (in_array(Security::ROLE_LIBRARIAN, $roles)) return Security::ROLE_LIBRARIAN;
74
        if (in_array(Security::ROLE_RESEARCHER, $roles)) return Security::ROLE_RESEARCHER;
75
76
        return "";
77
    }
78
79
}
80