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