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

CanAccessViewHelper::render()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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