‪TYPO3CMS  ‪main
PermissionsViewHelper.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 
22 use TYPO3\CMS\Core\Imaging\IconSize;
25 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28 
37 final class ‪PermissionsViewHelper extends AbstractViewHelper
38 {
39  use CompileWithRenderStatic;
40 
41  protected const ‪MASKS = [1, 16, 2, 4, 8];
42 
48  protected ‪$escapeOutput = false;
49 
50  protected static array ‪$cachePermissionLabels = [];
51 
52  public function ‪initializeArguments(): void
53  {
54  $this->registerArgument('permission', 'int', 'Current permission', true);
55  $this->registerArgument('scope', 'string', '"user" / "group" / "everybody"', true);
56  $this->registerArgument('pageId', 'int', 'Page ID to evaluate permission for', true);
57  }
58 
62  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
63  {
64  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
65 
66  $icon = '';
67  foreach (self::MASKS as $mask) {
68  if ($arguments['permission'] & $mask) {
69  $iconIdentifier = 'actions-check';
70  $iconClass = 'text-success';
71  $mode = 'delete';
72  } else {
73  $iconIdentifier = 'actions-close';
74  $iconClass = 'text-danger';
75  $mode = 'add';
76  }
77 
78  $label = ‪self::resolvePermissionLabel($mask);
79  $icon .= '<button'
80  . ' aria-label="' . htmlspecialchars($label) . ', ' . htmlspecialchars($mode) . ', ' . htmlspecialchars($arguments['scope']) . '"'
81  . ' title="' . htmlspecialchars($label) . '"'
82  . ' data-page="' . htmlspecialchars((string)$arguments['pageId']) . '"'
83  . ' data-permissions="' . htmlspecialchars((string)$arguments['permission']) . '"'
84  . ' data-who="' . htmlspecialchars($arguments['scope']) . '"'
85  . ' data-bits="' . htmlspecialchars((string)$mask) . '"'
86  . ' data-mode="' . htmlspecialchars($mode) . '"'
87  . ' class="btn btn-permission change-permission ' . htmlspecialchars($iconClass) . '">'
88  . $iconFactory->getIcon($iconIdentifier, IconSize::SMALL)->render(‪SvgIconProvider::MARKUP_IDENTIFIER_INLINE)
89  . '</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::$cachePermissionLabels[$mask])) {
98  self::$cachePermissionLabels[$mask] = htmlspecialchars(self::getLanguageService()->sL(
99  'LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $mask,
100  ));
101  }
102  return self::$cachePermissionLabels[$mask];
103  }
104 
105  protected static function ‪getLanguageService(): ‪LanguageService
106  {
107  return ‪$GLOBALS['LANG'];
108  }
109 }
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\$cachePermissionLabels
‪static array $cachePermissionLabels
Definition: PermissionsViewHelper.php:48
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\initializeArguments
‪initializeArguments()
Definition: PermissionsViewHelper.php:50
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: PermissionsViewHelper.php:60
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\MARKUP_IDENTIFIER_INLINE
‪const MARKUP_IDENTIFIER_INLINE
Definition: AbstractSvgIconProvider.php:32
‪TYPO3\CMS\Beuser\ViewHelpers
Definition: ArrayElementViewHelper.php:18
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper
Definition: PermissionsViewHelper.php:38
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\MASKS
‪const MASKS
Definition: PermissionsViewHelper.php:40
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: PermissionsViewHelper.php:46
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\resolvePermissionLabel
‪static resolvePermissionLabel(int $mask)
Definition: PermissionsViewHelper.php:93
‪TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider
Definition: SvgIconProvider.php:26
‪TYPO3\CMS\Beuser\ViewHelpers\PermissionsViewHelper\getLanguageService
‪static getLanguageService()
Definition: PermissionsViewHelper.php:103
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52