Role
6 years ago
CapabilitiesProvider.php
6 years ago
Capability.php
6 years ago
Role.php
6 years ago
RolesProvider.php
6 years ago
RolesProvider.php
63 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Access; |
| 10 | |
| 11 | use Piwik\Access\Role\Admin; |
| 12 | use Piwik\Access\Role\View; |
| 13 | use Piwik\Access\Role\Write; |
| 14 | use Piwik\Piwik; |
| 15 | use Exception; |
| 16 | |
| 17 | class RolesProvider |
| 18 | { |
| 19 | /** |
| 20 | * @return Role[] |
| 21 | */ |
| 22 | public function getAllRoles() |
| 23 | { |
| 24 | return array( |
| 25 | new View(), |
| 26 | new Write(), |
| 27 | new Admin() |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns the list of the existing Access level. |
| 33 | * Useful when a given API method requests a given acccess Level. |
| 34 | * We first check that the required access level exists. |
| 35 | * |
| 36 | * @return array |
| 37 | */ |
| 38 | public function getAllRoleIds() |
| 39 | { |
| 40 | $ids = array(); |
| 41 | foreach ($this->getAllRoles() as $role) { |
| 42 | $ids[] = $role->getId(); |
| 43 | } |
| 44 | return $ids; |
| 45 | } |
| 46 | |
| 47 | public function isValidRole($roleId) |
| 48 | { |
| 49 | $roles = $this->getAllRoleIds(); |
| 50 | |
| 51 | return in_array($roleId, $roles, true); |
| 52 | } |
| 53 | |
| 54 | public function checkValidRole($roleId) |
| 55 | { |
| 56 | if (!$this->isValidRole($roleId)) { |
| 57 | $roles = $this->getAllRoleIds(); |
| 58 | throw new Exception(Piwik::translate("UsersManager_ExceptionAccessValues", implode(", ", $roles))); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | } |
| 63 |