[TASK] PHP7 compliance - part 1
[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 * renders <f:then> child if the current logged in BE user belongs to the specified role (aka usergroup)
67 * otherwise renders <f:else> child.
68 *
69 * @param string $role The usergroup (either the usergroup uid or its title)
70 * @return string the rendered string
71 * @api
72 */
73 public function render($role = '') {
74 return parent::render($role);
75 }
76
77 /**
78 * This method decides if the condition is TRUE or FALSE. It can be overriden in extending viewhelpers to adjust functionality.
79 *
80 * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
81 * @return bool
82 */
83 static protected function evaluateCondition($arguments = NULL) {
84 $role = $arguments['role'];
85 if (!is_array($GLOBALS['BE_USER']->userGroups)) {
86 return FALSE;
87 }
88 if (is_numeric($role)) {
89 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
90 if ((int)$userGroup['uid'] === (int)$role) {
91 return TRUE;
92 }
93 }
94 } else {
95 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
96 if ($userGroup['title'] === $role) {
97 return TRUE;
98 }
99 }
100 }
101 return FALSE;
102 }
103 }