Passed
Pull Request — master (#1317)
by
unknown
21:24
created

IfHasAccessToModuleViewHelper::evaluateCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
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 2 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 TYPO3\CMS\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
    protected static function getModuleConfiguration(string $moduleSignature)
77
    {
78
        return $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
79
    }
80
81
    /**
82
     * Resolves
83
     *
84
     * @param array $arguments
85
     * @return mixed|string
86
     */
87
    protected static function getModuleSignatureFromArguments(array $arguments)
88
    {
89
        $moduleSignature = $arguments['signature'];
90
91
        $possibleErrorMessageAppendix = self::ERROR_APPENDIX_FOR_WRONG_SIGNATURE_ARGUMENT;
92
        $possibleErrorCode = 1496311009;
93
        if (!is_string($moduleSignature)) {
94
            $moduleSignature = $arguments['main'];
95
            $subModuleName = $arguments['extension'] . GeneralUtility::underscoredToUpperCamelCase($arguments['sub']);
96
            $moduleSignature .= '_' . $subModuleName;
97
            $possibleErrorMessageAppendix = vsprintf(self::ERROR_APPENDIX_FOR_SIGNATURE_RESOLUTION, [$arguments['extension'], $arguments['main'], $arguments['sub']]);
98
            $possibleErrorCode = 1496311010;
99
        }
100
        if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
101
            throw new \RuntimeException(vsprintf('Module with signature "%s" is not configured or couldn\'t be resolved. ' . $possibleErrorMessageAppendix, [$moduleSignature]), $possibleErrorCode);
102
        }
103
        return $moduleSignature;
104
    }
105
106
    /**
107
     * Validates arguments given to this view helper.
108
     *
109
     * It checks if either signature or extension and main and sub are set.
110
     */
111
    public function validateArguments()
112
    {
113
        parent::validateArguments();
114
115
        if (empty($this->arguments['signature'])
116
            && (empty($this->arguments['extension']) || empty($this->arguments['main']) || empty($this->arguments['sub'])
117
            )
118
        ) {
119
            throw new \InvalidArgumentException('ifHasAccessToModule view helper requires either "signature" or all three other arguments: "extension", "main" and "sub". Please set arguments properly.', 1496314352);
120
        }
121
    }
122
}
123