Passed
Pull Request — master (#203)
by
unknown
28:33 queued 13:24
created

Security::getFisPersId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 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
                $accessToIds = array_merge($accessToIds, explode(',', $userGroup->getAccessToGroups()));
76
            }
77
            if (empty($accessToIds[0])) {
78
                return null;
79
            } else {
80
                return $accessToIds;
81
            }
82
        }
83
        return NULL;
84
    }
85
86
    /**
87
     * Gets the role of the current frontend user
88
     * @return string
89
     */
90
    public function getUserRole()
91
    {
92
        if ($this->getUser()) {
93
            return $this->getUser()->getUserRole();
94
        }
95
        return '';
96
    }
97
98
    /**
99
     * Gets the name of the current frontend user
100
     * @return string
101
     */
102
    public function getUsername()
103
    {
104
        if ($this->getUser()) {
105
            return $this->getUser()->getUsername();
106
        }
107
        return '';
108
    }
109
110
    /**
111
     * Gets the fis person id of the current frontend user
112
     * @return string
113
     */
114
    public function getFisPersId()
115
    {
116
        if ($this->getUser()) {
117
            return $this->getUser()->getFisPersId();
118
        }
119
        return '';
120
    }
121
122
}
123