[BUGFIX] IfViewHelpers use method signature from parent
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Be / Security / IfHasRoleViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Be\Security;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * This view helper implements an ifHasRole/else condition for BE users/groups.
25 *
26 * = Examples =
27 *
28 * <code title="Basic usage">
29 * <f:be.security.ifHasRole role="Administrator">
30 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) titled "Administrator" (case sensitive)
31 * </f:be.security.ifHasRole>
32 * </code>
33 * <output>
34 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
35 * </output>
36 *
37 * <code title="Using the usergroup uid as role identifier">
38 * <f:be.security.ifHasRole role="1">
39 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) with the uid "1"
40 * </f:be.security.ifHasRole>
41 * </code>
42 * <output>
43 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
44 * </output>
45 *
46 * <code title="IfRole / then / else">
47 * <f:be.security.ifHasRole role="Administrator">
48 * <f:then>
49 * This is being shown in case you have the role.
50 * </f:then>
51 * <f:else>
52 * This is being displayed in case you do not have the role.
53 * </f:else>
54 * </f:be.security.ifHasRole>
55 * </code>
56 * <output>
57 * Everything inside the "then" tag is displayed if the logged in BE user belongs to the specified role.
58 * Otherwise, everything inside the "else"-tag is displayed.
59 * </output>
60 *
61 * @api
62 */
63 class IfHasRoleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
64
65 /**
66 * Initializes the "role" argument.
67 * Renders <f:then> child if the current logged in BE user belongs to the specified role (aka usergroup)
68 * otherwise renders <f:else> child.
69 *
70 * @return void
71 */
72 public function initializeArguments() {
73 $this->registerArgument('role', 'string', 'The usergroup (either the usergroup uid or its title).');
74 }
75
76 /**
77 * This method decides if the condition is TRUE or FALSE. It can be overriden in extending viewhelpers to adjust functionality.
78 *
79 * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
80 * @return bool
81 */
82 static protected function evaluateCondition($arguments = NULL) {
83 $role = $arguments['role'];
84 if (!is_array($GLOBALS['BE_USER']->userGroups)) {
85 return FALSE;
86 }
87 if (is_numeric($role)) {
88 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
89 if ((int)$userGroup['uid'] === (int)$role) {
90 return TRUE;
91 }
92 }
93 } else {
94 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
95 if ($userGroup['title'] === $role) {
96 return TRUE;
97 }
98 }
99 }
100 return FALSE;
101 }
102 }