[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Security / IfHasRoleViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\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 use TYPO3\CMS\Core\Context\Context;
18 use TYPO3\CMS\Core\Context\UserAspect;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
21
22 /**
23 * This view helper implements an ifHasRole/else condition for FE users/groups.
24 *
25 * = Examples =
26 *
27 * <code title="Basic usage">
28 * <f:security.ifHasRole role="Administrator">
29 * This is being shown in case the current FE user belongs to a FE usergroup (aka role) titled "Administrator" (case sensitive)
30 * </f:security.ifHasRole>
31 * </code>
32 * <output>
33 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in FE user belongs to the specified role.
34 * </output>
35 *
36 * <code title="Using the usergroup uid as role identifier">
37 * <f:security.ifHasRole role="1">
38 * This is being shown in case the current FE user belongs to a FE usergroup (aka role) with the uid "1"
39 * </f:security.ifHasRole>
40 * </code>
41 * <output>
42 * Everything inside the <f:ifHasRole> tag is being displayed if the logged in FE user belongs to the specified role.
43 * </output>
44 *
45 * <code title="IfRole / then / else">
46 * <f:security.ifHasRole role="Administrator">
47 * <f:then>
48 * This is being shown in case you have the role.
49 * </f:then>
50 * <f:else>
51 * This is being displayed in case you do not have the role.
52 * </f:else>
53 * </f:security.ifHasRole>
54 * </code>
55 * <output>
56 * Everything inside the "then" tag is displayed if the logged in FE user belongs to the specified role.
57 * Otherwise, everything inside the "else"-tag is displayed.
58 * </output>
59 */
60 class IfHasRoleViewHelper extends AbstractConditionViewHelper
61 {
62 /**
63 * Initializes the "role" argument.
64 * Renders <f:then> child if the current logged in FE user belongs to the specified role (aka usergroup)
65 * otherwise renders <f:else> child.
66 */
67 public function initializeArguments()
68 {
69 $this->registerArgument('role', 'string', 'The usergroup (either the usergroup uid or its title).');
70 }
71
72 /**
73 * This method decides if the condition is TRUE or FALSE. It can be overridden in extending viewhelpers to adjust functionality.
74 *
75 * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
76 * @return bool
77 */
78 protected static function evaluateCondition($arguments = null)
79 {
80 $role = $arguments['role'];
81 /** @var UserAspect $userAspect */
82 $userAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
83 if (!$userAspect->isLoggedIn()) {
84 return false;
85 }
86 if (is_numeric($role)) {
87 $groupIds = $userAspect->getGroupIds();
88 return in_array((int)$role, $groupIds, true);
89 }
90 return in_array($role, $userAspect->getGroupNames(), true);
91 }
92 }