[!!!][FEATURE] Switch to standalone Fluid template engine
[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 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 /**
18 * This view helper implements an ifHasRole/else condition for BE users/groups.
19 *
20 * = Examples =
21 *
22 * <code title="Basic usage">
23 * <f:be.security.ifHasRole role="Administrator">
24 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) titled "Administrator" (case sensitive)
25 * </f:be.security.ifHasRole>
26 * </code>
27 * <output>
28 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
29 * </output>
30 *
31 * <code title="Using the usergroup uid as role identifier">
32 * <f:be.security.ifHasRole role="1">
33 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) with the uid "1"
34 * </f:be.security.ifHasRole>
35 * </code>
36 * <output>
37 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
38 * </output>
39 *
40 * <code title="IfRole / then / else">
41 * <f:be.security.ifHasRole role="Administrator">
42 * <f:then>
43 * This is being shown in case you have the role.
44 * </f:then>
45 * <f:else>
46 * This is being displayed in case you do not have the role.
47 * </f:else>
48 * </f:be.security.ifHasRole>
49 * </code>
50 * <output>
51 * Everything inside the "then" tag is displayed if the logged in BE user belongs to the specified role.
52 * Otherwise, everything inside the "else"-tag is displayed.
53 * </output>
54 *
55 * @api
56 */
57 class IfHasRoleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper
58 {
59 /**
60 * Initializes the "role" argument.
61 * Renders <f:then> child if the current logged in BE user belongs to the specified role (aka usergroup)
62 * otherwise renders <f:else> child.
63 *
64 * @return void
65 */
66 public function initializeArguments()
67 {
68 $this->registerArgument('role', 'string', 'The usergroup (either the usergroup uid or its title).');
69 }
70
71 /**
72 * This method decides if the condition is TRUE or FALSE. It can be overriden in extending viewhelpers to adjust functionality.
73 *
74 * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
75 * @return bool
76 */
77 protected static function evaluateCondition($arguments = null)
78 {
79 $role = $arguments['role'];
80 if (!is_array($GLOBALS['BE_USER']->userGroups)) {
81 return false;
82 }
83 if (is_numeric($role)) {
84 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
85 if ((int)$userGroup['uid'] === (int)$role) {
86 return true;
87 }
88 }
89 } else {
90 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
91 if ($userGroup['title'] === $role) {
92 return true;
93 }
94 }
95 }
96 return false;
97 }
98
99 /**
100 * @return mixed
101 */
102 public function render()
103 {
104 if (static::evaluateCondition($this->arguments)) {
105 return $this->renderThenChild();
106 } else {
107 return $this->renderElseChild();
108 }
109 }
110 }