Passed
Push — master ( fb791a...9a88de )
by Timo
41:42
created

IfHasAccessToModuleViewHelper::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Backend\Security;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
30
31
/**
32
 * This view helper implements an ifHasAccessToModule/else condition for BE users/groups.
33
 *
34
 * Class IfHasAccessToModuleViewHelper
35
 */
36
class IfHasAccessToModuleViewHelper extends AbstractConditionViewHelper
37
{
38
    /**
39
     * Message for that case, if $arguments['signature'] was used and module does not exist
40
     * @var string
41
     */
42
    const ERROR_APPENDIX_FOR_WRONG_SIGNATURE_ARGUMENT = 'Please check spelling and style by setting signature="mainName_ExtKeySubmoduleName".';
43
44
    /**
45
     * Message for that case, if $arguments['extension'], $arguments['main'] and $arguments['sub'] are used and module couldn't be resolved
46
     * @var string
47
     */
48
    const ERROR_APPENDIX_FOR_SIGNATURE_RESOLUTION = 'It was generated by setting extension="%s", main="%s", sub="%s", please check spelling and style by setting this arguments.';
49
50
    /**
51
     * Initializes following arguments: extension, main, sub, signature
52
     * Renders <f:then> child if the current logged in BE user belongs to the specified role (aka usergroup)
53
     * otherwise renders <f:else> child.
54
     */
55
    public function initializeArguments()
56
    {
57
        parent::initializeArguments();
58
        $this->registerArgument('extension', 'string', 'The extension key.');
59
        $this->registerArgument('main', 'string', 'The main module name.');
60
        $this->registerArgument('sub', 'string', 'The sub module name.');
61
        $this->registerArgument('signature', 'string', 'The full signature of module. Simply mainmodulename_submodulename in most cases.');
62
    }
63
64
65
    protected static function evaluateCondition($arguments = null)
66
    {
67
        /* @var BackendUserAuthentication $beUser */
68
        $beUser = $GLOBALS['BE_USER'];
69
        $hasAccessToModule = $beUser->modAccess(
70
            self::getModuleConfiguration(self::getModuleSignatureFromArguments($arguments)),
71
            false
72
        );
73
        return $hasAccessToModule;
74
    }
75
76
    /**
77
     * Renders <f:then> child if $condition is true, otherwise renders <f:else> child.
78
     *
79
     * @todo This copy of the render method is just required for TYPO3 8 backwards compatibility, can be dropped when TYPO3 8 support is dropped.
80
     *
81
     * @param bool $condition View helper condition
82
     * @return string the rendered string
83
     */
84
    public function render()
85
    {
86
        if (static::evaluateCondition($this->arguments)) {
87
            return $this->renderThenChild();
88
        }
89
        return $this->renderElseChild();
90
    }
91
92
    protected static function getModuleConfiguration(string $moduleSignature)
93
    {
94
        return $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
95
    }
96
97
    /**
98
     * Resolves
99
     *
100
     * @param array $arguments
101
     * @return mixed|string
102
     */
103
    protected static function getModuleSignatureFromArguments(array $arguments)
104
    {
105
        $moduleSignature = $arguments['signature'];
106
107
        $possibleErrorMessageAppendix = self::ERROR_APPENDIX_FOR_WRONG_SIGNATURE_ARGUMENT;
108
        $possibleErrorCode = 1496311009;
109
        if (!is_string($moduleSignature)) {
110
            $moduleSignature = $arguments['main'];
111
            $subModuleName = $arguments['extension'] . GeneralUtility::underscoredToUpperCamelCase($arguments['sub']);
112
            $moduleSignature .= '_' . $subModuleName;
113
            $possibleErrorMessageAppendix = vsprintf(self::ERROR_APPENDIX_FOR_SIGNATURE_RESOLUTION, [$arguments['extension'], $arguments['main'], $arguments['sub']]);
114
            $possibleErrorCode = 1496311010;
115
        }
116
        if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
117
            throw new \RuntimeException(vsprintf('Module with signature "%s" is not configured or couldn\'t be resolved. ' . $possibleErrorMessageAppendix, [$moduleSignature]), $possibleErrorCode);
118
        }
119
        return $moduleSignature;
120
    }
121
122
    /**
123
     * Validates arguments given to this view helper.
124
     *
125
     * It checks if either signature or extension and main and sub are set.
126
     */
127
    public function validateArguments()
128
    {
129
        parent::validateArguments();
130
131
        if (empty($this->arguments['signature'])
132
            && (empty($this->arguments['extension']) || empty($this->arguments['main']) || empty($this->arguments['sub'])
133
            )
134
        ) {
135
            throw new \InvalidArgumentException('ifHasAccessToModule view helper requires either "signature" or all three other arguments: "extension", "main" and "sub". Please set arguments properly.', 1496314352);
136
        }
137
    }
138
}
139