Passed
Pull Request — master (#155)
by
unknown
08:39
created

IsAccessGrantedViewHelper::render()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 18
nop 3
dl 0
loc 37
rs 8.6666
c 0
b 0
f 0
1
<?php
2
namespace EWW\Dpf\ViewHelpers;
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 TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Object\ObjectManager;
20
use EWW\Dpf\Security\AuthorizationChecker;
21
22
class IsAccessGrantedViewHelper extends AbstractViewHelper
23
{
24
    /**
25
     * Checks if access can be granted for the given attribute and subject.
26
     *
27
     * @param string $attribute
28
     * @param mixed $subject : A model object or a UID.
29
     * @param string $class : Model class name, in case of parameter 2 is a UID.
30
     * @return bool
31
     */
32
    public function render($attribute, $subject, $class="EWW\\Dpf\\Domain\\Model\\Document")
33
    {
34
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
35
        $authorizationChecker = $objectManager->get(AuthorizationChecker::class);
36
37
        if (empty($subject)) {
38
            $subject = $objectManager->get($class);
39
            return $authorizationChecker->isGranted($attribute, $subject);
40
        }
41
42
        if (is_object($subject)) {
43
            return $authorizationChecker->isGranted($attribute, $subject);
44
        }
45
46
        $uid = 0;
47
48
        if (is_int($subject)) {
49
            $uid = $subject;
50
        }
51
52
        if (is_string($subject)) {
53
            list($class, $uid) = explode(":", $subject);
54
        }
55
56
        $repositoryClass = str_replace("Model", "Repository", $class)."Repository";
57
        $repository = $objectManager->get($repositoryClass);
58
59
        if ($repository) {
60
            $subject = $repository->findByUid($uid);
61
        }
62
63
        if ($subject instanceof $class) {
64
            $authorizationChecker = $objectManager->get(AuthorizationChecker::class);
65
            return $authorizationChecker->isGranted($attribute, $subject);
66
        }
67
68
        return FALSE;
69
    }
70
71
}
72