[CLEANUP] Rework/simplify copyright header and remove @package
[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 backported from the TYPO3 Flow package "TYPO3.Fluid".
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 /**
20 * This view helper implements an ifHasRole/else condition for BE users/groups.
21 *
22 * = Examples =
23 *
24 * <code title="Basic usage">
25 * <f:be.security.ifHasRole role="Administrator">
26 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) titled "Administrator" (case sensitive)
27 * </f:be.security.ifHasRole>
28 * </code>
29 * <output>
30 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
31 * </output>
32 *
33 * <code title="Using the usergroup uid as role identifier">
34 * <f:be.security.ifHasRole role="1">
35 * This is being shown in case the current BE user belongs to a BE usergroup (aka role) with the uid "1"
36 * </f:be.security.ifHasRole>
37 * </code>
38 * <output>
39 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in BE user belongs to the specified role.
40 * </output>
41 *
42 * <code title="IfRole / then / else">
43 * <f:be.security.ifHasRole role="Administrator">
44 * <f:then>
45 * This is being shown in case you have the role.
46 * </f:then>
47 * <f:else>
48 * This is being displayed in case you do not have the role.
49 * </f:else>
50 * </f:be.security.ifHasRole>
51 * </code>
52 * <output>
53 * Everything inside the "then" tag is displayed if the logged in BE user belongs to the specified role.
54 * Otherwise, everything inside the "else"-tag is displayed.
55 * </output>
56 *
57 * @api
58 */
59 class IfHasRoleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
60
61 /**
62 * renders <f:then> child if the current logged in BE user belongs to the specified role (aka usergroup)
63 * otherwise renders <f:else> child.
64 *
65 * @param string $role The usergroup (either the usergroup uid or its title)
66 * @return string the rendered string
67 * @api
68 */
69 public function render($role) {
70 if ($this->backendUserHasRole($role)) {
71 return $this->renderThenChild();
72 } else {
73 return $this->renderElseChild();
74 }
75 }
76
77 /**
78 * Determines whether the currently logged in BE user belongs to the specified usergroup
79 *
80 * @param string $role The usergroup (either the usergroup uid or its title)
81 * @return bool TRUE if the currently logged in BE user belongs to $role
82 */
83 protected function backendUserHasRole($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
103 }