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

AuthorizationChecker::denyAccessUnlessGranted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
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
18
class AuthorizationChecker
19
{
20
    /**
21
     * objectManager
22
     *
23
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
24
     * @inject
25
     */
26
    protected $objectManager;
27
28
    /**
29
     * security
30
     *
31
     * @var \EWW\Dpf\Security\Security
32
     * @inject
33
     */
34
    protected $security = null;
35
36
37
    const ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
38
    const ROLE_RESEARCHER = "ROLE_RESEARCHER";
39
    const ROLE_LIBRARIAN = "ROLE_LIBRARIAN";
40
41
    public function denyAccessUnlessLoggedIn()
42
    {
43
        $security = $this->objectManager->get(\EWW\Dpf\Security\Security::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $security is dead and can be removed.
Loading history...
44
45
        if (
46
            $this->security->getUserRole() === Security::ROLE_LIBRARIAN ||
47
            $this->security->getUserRole() === Security::ROLE_RESEARCHER
48
        ) {
49
            return;
50
        } else {
51
            header('Temporary-Header: True', true, 403);
52
            header_remove('Temporary-Header');
53
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.access_denied';
54
            $accessDeniedMessage = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
55
            die($accessDeniedMessage);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
56
        }
57
    }
58
59
    public function denyAccessUnlessGranted($attribute, $subject = NULL)
60
    {
61
        if($this->isGranted($attribute, $subject)) {
62
            return;
63
        } else {
64
            header('Temporary-Header: True', true, 403);
65
            header_remove('Temporary-Header');
66
            $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.access_denied';
67
            $accessDeniedMessage = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
68
            die($accessDeniedMessage);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
69
        }
70
    }
71
72
73
    /**
74
     * @param string $attribute
75
     * @param object $subject
76
     * @return bool
77
     */
78
    public function isGranted($attribute, $subject = NULL) {
79
        $voters = Voter::getVoters();
80
81
        foreach ($voters as $voter) {
82
            if ($voter->supports($attribute, $subject)) {
83
                return $voter->voteOnAttribute($attribute, $subject);
84
            }
85
        }
86
87
        return FALSE;
88
    }
89
90
}