Schlaefer /
Saito
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Saito - The Threaded Web Forum |
||
| 7 | * |
||
| 8 | * @copyright Copyright (c) the Saito Project Developers |
||
| 9 | * @link https://github.com/Schlaefer/Saito |
||
| 10 | * @license http://opensource.org/licenses/MIT |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace App\View\Helper; |
||
| 14 | |||
| 15 | use Cake\View\Helper\HtmlHelper; |
||
| 16 | use Cake\View\Helper\UrlHelper; |
||
| 17 | use Saito\App\Registry; |
||
| 18 | use Saito\User\Permission\Permissions; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class UserHelper |
||
| 22 | * |
||
| 23 | * @package App\View\Helper |
||
| 24 | * @property HtmlHelper $Html |
||
| 25 | * @property UrlHelper $Url |
||
| 26 | */ |
||
| 27 | class PermissionsHelper extends AppHelper |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Permissions |
||
| 31 | * |
||
| 32 | * @var Permissions |
||
| 33 | */ |
||
| 34 | private $Permissions; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritDoc} |
||
| 38 | */ |
||
| 39 | public function initialize(array $config) |
||
| 40 | { |
||
| 41 | parent::initialize($config); |
||
| 42 | |||
| 43 | /** @var Permissions */ |
||
| 44 | $permissions = Registry::get('Permissions'); |
||
| 45 | $this->Permissions = $permissions; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get an array to use as $option in a select field with role-IDs |
||
| 50 | * |
||
| 51 | * @param bool $includeAnon Include anon-user |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function rolesSelectId(bool $includeAnon = false): array |
||
| 55 | { |
||
| 56 | $roles = $this->Permissions->getRoles()->getAvailable($includeAnon); |
||
| 57 | |||
| 58 | return array_map(function ($role) { |
||
| 59 | return ['value' => $role['id'], 'text' => $this->roleAsString($role['id'])]; |
||
| 60 | }, $roles); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get an array to use as $option in a select field with role-types |
||
| 65 | * |
||
| 66 | * @param bool $includeAnon Include anon-user |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function rolesSelectType(bool $includeAnon = false): array |
||
| 70 | { |
||
| 71 | $roles = $this->Permissions->getRoles()->getAvailable($includeAnon); |
||
| 72 | |||
| 73 | return array_map(function ($role) { |
||
| 74 | return ['value' => $role['type'], 'text' => $this->roleAsString($role['id'])]; |
||
| 75 | }, $roles); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * L10n a role. |
||
| 80 | * |
||
| 81 | * @param int|string $id Role-ID or role-type. |
||
| 82 | * @return string The localized string. |
||
| 83 | */ |
||
| 84 | public function roleAsString($id): string |
||
| 85 | { |
||
| 86 | if (is_string($id)) { |
||
| 87 | $id = $this->Permissions->getRoles()->typeToId($id); |
||
| 88 | } |
||
| 89 | |||
| 90 | return __d('nondynamic', 'permission.role.' . $id); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 91 | } |
||
| 92 | } |
||
| 93 |