‪TYPO3CMS  11.5
PermissionsViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22 
30 class ‪PermissionsViewHelper extends AbstractViewHelper
31 {
32  use CompileWithRenderStatic;
33 
34  protected const ‪MASKS = [1, 16, 2, 4, 8];
35 
41  protected ‪$escapeOutput = false;
42 
46  protected static ‪$permissionLabels = [];
47 
51  public function ‪initializeArguments()
52  {
53  $this->registerArgument('permission', 'int', 'Current permission', true);
54  $this->registerArgument('scope', 'string', '"user" / "group" / "everybody"', true);
55  $this->registerArgument('pageId', 'int', 'Page ID to evaluate permission for', true);
56  }
57 
67  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
68  {
69  $icon = '';
70  foreach (self::MASKS as $mask) {
71  if ($arguments['permission'] & $mask) {
72  $permissionClass = 'fa-check text-success';
73  $mode = 'delete';
74  } else {
75  $permissionClass = 'fa-times text-danger';
76  $mode = 'add';
77  }
78 
79  $label = ‪self::resolvePermissionLabel($mask);
80  $icon .= '<button'
81  . ' aria-label="' . htmlspecialchars($label) . ', ' . htmlspecialchars($mode) . ', ' . htmlspecialchars($arguments['scope']) . '"'
82  . ' title="' . htmlspecialchars($label) . '"'
83  . ' data-bs-toggle="tooltip"'
84  . ' data-page="' . htmlspecialchars((string)$arguments['pageId']) . '"'
85  . ' data-permissions="' . htmlspecialchars((string)$arguments['permission']) . '"'
86  . ' data-who="' . htmlspecialchars($arguments['scope']) . '"'
87  . ' data-bits="' . htmlspecialchars((string)$mask) . '"'
88  . ' data-mode="' . htmlspecialchars($mode) . '"'
89  . ' class="t3-icon btn-clear change-permission fa ' . htmlspecialchars($permissionClass) . '"></button>';
90  }
91 
92  return '<span id="' . htmlspecialchars($arguments['pageId'] . '_' . $arguments['scope']) . '">' . $icon . '</span>';
93  }
94 
95  protected static function ‪resolvePermissionLabel(int $mask): string
96  {
97  if (!isset(self::$permissionLabels[$mask])) {
98  self::$permissionLabels[$mask] = ‪LocalizationUtility::translate(
99  'LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $mask,
100  'be_user'
101  );
102  }
103  return self::$permissionLabels[$mask];
104  }
105 }
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\initializeArguments
‪initializeArguments()
Definition: PermissionsViewHelper.php:48
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Beuser\ViewHelpers
Definition: ArrayElementViewHelper.php:16
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper
Definition: PermissionsViewHelper.php:31
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\MASKS
‪const MASKS
Definition: PermissionsViewHelper.php:33
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: PermissionsViewHelper.php:39
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\resolvePermissionLabel
‪static resolvePermissionLabel(int $mask)
Definition: PermissionsViewHelper.php:92
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\$permissionLabels
‪static array $permissionLabels
Definition: PermissionsViewHelper.php:43
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: PermissionsViewHelper.php:64