[TASK] Remove closing PHP tags
[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 if ($this->backendUserHasRole($role)) {
75 return $this->renderThenChild();
76 } else {
77 return $this->renderElseChild();
78 }
79 }
80
81 /**
82 * Determines whether the currently logged in BE user belongs to the specified usergroup
83 *
84 * @param string $role The usergroup (either the usergroup uid or its title)
85 * @return boolean TRUE if the currently logged in BE user belongs to $role
86 */
87 protected function backendUserHasRole($role) {
88 if (!is_array($GLOBALS['BE_USER']->userGroups)) {
89 return FALSE;
90 }
91 if (is_numeric($role)) {
92 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
93 if ((integer) $userGroup['uid'] === (integer) $role) {
94 return TRUE;
95 }
96 }
97 } else {
98 foreach ($GLOBALS['BE_USER']->userGroups as $userGroup) {
99 if ($userGroup['title'] === $role) {
100 return TRUE;
101 }
102 }
103 }
104 return FALSE;
105 }
106 }