Passed
Push — master ( 2ba76b...3603b1 )
by Ralf
11:11
created

Security::getUserAccessToGroups()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 31
rs 8.4444
cc 8
nc 19
nop 0
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
use EWW\Dpf\Domain\Model\FrontendUser;
18
use EWW\Dpf\Domain\Model\FrontendUserGroup;
19
20
class Security
21
{
22
    /**
23
     * frontendUserRepository
24
     *
25
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
26
     * @TYPO3\CMS\Extbase\Annotation\Inject
27
     */
28
    protected $frontendUserRepository = null;
29
30
    /**
31
     * frontendUserGroupRepository
32
     *
33
     * @var \EWW\Dpf\Domain\Repository\FrontendUserGroupRepository
34
     * @TYPO3\CMS\Extbase\Annotation\Inject
35
     */
36
    protected $frontendUserGroupRepository = null;
37
38
    const ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
39
    const ROLE_RESEARCHER = "ROLE_RESEARCHER";
40
    const ROLE_LIBRARIAN = "ROLE_LIBRARIAN";
41
42
43
    /**
44
     * Gets the current logged in frontend user
45
     *
46
     * @return null|\EWW\Dpf\Domain\Model\FrontendUser
47
     */
48
    public function getUser()
49
    {
50
        $token = $GLOBALS['_GET']['tx_dpf_rest_api']['token'];
51
        $user = $GLOBALS['TSFE']->fe_user->user;
52
        if (!empty($user) && is_array($user) && array_key_exists('uid', $user)) {
53
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
54
        } else if ($token) {
55
            $token = htmlentities($token);
56
            $token = addslashes($token);
57
            return $this->frontendUserRepository->findOneByApiToken($token);
0 ignored issues
show
Bug introduced by
The method findOneByApiToken() does not exist on EWW\Dpf\Domain\Repository\FrontendUserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
            return $this->frontendUserRepository->/** @scrutinizer ignore-call */ findOneByApiToken($token);
Loading history...
Bug Best Practice introduced by
The expression return $this->frontendUs...ndOneByApiToken($token) also could return the type TYPO3\CMS\Extbase\Persis...Interface|array|integer which is incompatible with the documented return type EWW\Dpf\Domain\Model\FrontendUser|null.
Loading history...
58
        } else {
59
            return NULL;
60
        }
61
    }
62
63
    /**
64
     *
65
     */
66
    public function getUserAccessToGroups() {
67
        if ($this->getUser()) {
68
            $frontendUser = $this->getUser();
69
            $userGroups = $frontendUser->getUsergroup();
70
            $accessToIds = [];
71
            foreach ($userGroups as $userGroup) {
72
                // Because getUsergroup() does not return objects of the class
73
                // \EWW\Dpf\Domain\Model\FrontendUserRepository
74
                $userGroup = $this->frontendUserGroupRepository->findByUid($userGroup->getUid());
75
                if (!empty($userGroup->getAccessToGroups())) {
76
                    $accessToIds = array_merge($accessToIds, explode(',', $userGroup->getAccessToGroups()));
77
                }
78
79
                // get first subgroups // TODO How deep? Recursion needed?
80
                $subGroups = $userGroup->getSubgroup();
81
                if ($subGroups) {
82
                    foreach ($subGroups as $subGroup) {
83
                        $subGroup = $this->frontendUserGroupRepository->findByUid($subGroup->getUid());
84
                        if (!empty($subGroup->getAccessToGroups())) {
85
                            $accessToIds = array_merge($accessToIds, explode(',', $subGroup->getAccessToGroups()));
86
                        }
87
                    }
88
                }
89
            }
90
            if (empty($accessToIds[0])) {
91
                return null;
92
            } else {
93
                return $accessToIds;
94
            }
95
        }
96
        return NULL;
97
    }
98
99
    /**
100
     * Gets the role of the current frontend user
101
     * @return string
102
     */
103
    public function getUserRole()
104
    {
105
        if ($this->getUser()) {
106
            return $this->getUser()->getUserRole();
107
        }
108
        return '';
109
    }
110
111
    /**
112
     * Gets the name of the current frontend user
113
     * @return string
114
     */
115
    public function getUsername()
116
    {
117
        if ($this->getUser()) {
118
            return $this->getUser()->getUsername();
119
        }
120
        return '';
121
    }
122
123
    /**
124
     * Gets the fis person id of the current frontend user
125
     * @return string
126
     */
127
    public function getFisPersId()
128
    {
129
        if ($this->getUser()) {
130
            return $this->getUser()->getFisPersId();
131
        }
132
        return '';
133
    }
134
135
}
136