UserRoles.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace OpenMind\System;
  3. /**
  4. * Global user roles: a volatile list of roles present throughout the application.
  5. * The list is not linked to users.
  6. */
  7. class UserRoles
  8. {
  9. private const F3_KEY = 'USERROLES';
  10. /**
  11. * Initialize the global user-role list when it does not exist.
  12. *
  13. * @side-effects Creates the global user-role list when needed.
  14. */
  15. public function __construct()
  16. {
  17. global $f3;
  18. if ($f3->get(self::F3_KEY) === null) {
  19. $f3->set(self::F3_KEY, []);
  20. }
  21. }
  22. /**
  23. * Get all defined user roles.
  24. *
  25. * @return array All configured user roles.
  26. * @side-effects Reads the global user-role list.
  27. */
  28. public static function getAllRoles(): array
  29. {
  30. global $f3;
  31. return $f3->get(self::F3_KEY);
  32. }
  33. /**
  34. * Retrieve the value for a permission.
  35. *
  36. * @param string $permissionName Permission name to retrieve.
  37. * @return mixed Configured permission value.
  38. * @side-effects Reads the global user-role list.
  39. */
  40. public function get(string $permissionName)
  41. {
  42. global $f3;
  43. return $f3->get(self::F3_KEY)[$permissionName];
  44. }
  45. /**
  46. * Add a new permission to the roles list.
  47. *
  48. * @param string $permissionName Permission name to add.
  49. * @return void
  50. * @side-effects Mutates the global user-role list.
  51. */
  52. public function add(string $permissionName)
  53. {
  54. global $f3;
  55. $roles = &$f3->ref(self::F3_KEY);
  56. $roles[] = $permissionName;
  57. }
  58. }