‪TYPO3CMS  ‪main
IfHasRoleViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
25 
75 final class ‪IfHasRoleViewHelper extends AbstractConditionViewHelper
76 {
82  public function ‪initializeArguments(): void
83  {
84  parent::initializeArguments();
85  $this->registerArgument('role', 'string', 'The usergroup (either the usergroup uid or its title).');
86  }
87 
88  public static function ‪verdict(array $arguments, RenderingContextInterface $renderingContext): bool
89  {
90  $role = $arguments['role'];
92  $userAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
93  if (!$userAspect->isLoggedIn()) {
94  return false;
95  }
96  if (is_numeric($role)) {
97  $groupIds = $userAspect->getGroupIds();
98  return in_array((int)$role, $groupIds, true);
99  }
100  return in_array($role, $userAspect->getGroupNames(), true);
101  }
102 }
‪TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper\verdict
‪static verdict(array $arguments, RenderingContextInterface $renderingContext)
Definition: IfHasRoleViewHelper.php:88
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper\initializeArguments
‪initializeArguments()
Definition: IfHasRoleViewHelper.php:82
‪TYPO3\CMS\Fluid\ViewHelpers\Security\IfHasRoleViewHelper
Definition: IfHasRoleViewHelper.php:76
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\ViewHelpers\Security
Definition: IfAuthenticatedViewHelper.php:18
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37