Auth.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. if (!defined('USER_ROLE_KEY')) {
  3. define('USER_ROLE_KEY', 'Role');
  4. }
  5. if (!defined('USER_RIGHT_KEY')) {
  6. define('USER_RIGHT_KEY', 'Right');
  7. }
  8. App::uses('CakeSession', 'Model/Datasource');
  9. /**
  10. * Convenience wrapper to access Auth data and check on rights/roles.
  11. * Expects the Role session infos to be either
  12. * `Auth.User.role_id` (single) or
  13. * `Auth.User.Role` (multi - flat array of roles, or array role data)
  14. * and can be adjusted via defined().
  15. * Same for Right.
  16. *
  17. * @author Mark Scherer
  18. * @license MIT
  19. * @php 5
  20. * @cakephp 2
  21. */
  22. class Auth {
  23. /**
  24. * Get the user id of the current session.
  25. *
  26. * This can be used anywhere to check if a user is logged in.
  27. *
  28. * @return mixed User id if existent, null otherwise.
  29. */
  30. public static function id() {
  31. return CakeSession::read('Auth.User.id');
  32. }
  33. /**
  34. * Get the role(s) of the current session.
  35. *
  36. * It will return the single role for single role setup, and a flat
  37. * list of roles for multi role setup.
  38. *
  39. * @return mixed String or array of roles or null if inexistent
  40. */
  41. public static function roles() {
  42. $roles = CakeSession::read('Auth.User.' . USER_ROLE_KEY);
  43. if (!is_array($roles)) {
  44. return $roles;
  45. }
  46. if (isset($roles[0]['id'])) {
  47. $roles = Hash::extract($roles, '{n}.id');
  48. }
  49. return $roles;
  50. }
  51. /**
  52. * Get the user data of the current session.
  53. *
  54. * @param string $key (dot syntax)
  55. * @return mixed Data
  56. */
  57. public static function user($key = null) {
  58. if ($key) {
  59. $key = '.' . $key;
  60. }
  61. return CakeSession::read('Auth.User' . $key);
  62. }
  63. /**
  64. * Check if the current session has this right.
  65. *
  66. * @param mixed $role
  67. * @param mixed $providedRights
  68. * @return boolean Success
  69. */
  70. public static function hasRight($ownRight, $providedRights = null) {
  71. if ($providedRights !== null) {
  72. $rights = $providedRights;
  73. } else {
  74. $rights = CakeSession::read('Auth.User.' . USER_RIGHT_KEY);
  75. }
  76. $rights = (array)$rights;
  77. if (array_key_exists($ownRight, $rights) && !empty($rights[$ownRight])) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. /**
  83. * Check if the current session has this role.
  84. *
  85. * @param mixed $role
  86. * @param mixed $providedRoles
  87. * @return boolean Success
  88. */
  89. public static function hasRole($ownRole, $providedRoles = null) {
  90. if ($providedRoles !== null) {
  91. $roles = $providedRoles;
  92. } else {
  93. $roles = self::roles();
  94. }
  95. if (is_array($roles)) {
  96. if (in_array($ownRole, $roles)) {
  97. return true;
  98. }
  99. } elseif (!empty($roles)) {
  100. if ($ownRole == $roles) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Check if the current session has oen of these roles.
  108. *
  109. * @param mixed $roles
  110. * @param boolean $oneRoleIsEnough (if all $roles have to match instead of just one)
  111. * @param mixed $providedRoles
  112. * @return boolean Success
  113. */
  114. public static function hasRoles($ownRoles, $oneRoleIsEnough = true, $providedRoles = null) {
  115. if ($providedRoles !== null) {
  116. $roles = $providedRoles;
  117. } else {
  118. $roles = self::roles();
  119. }
  120. $ownRoles = (array)$ownRoles;
  121. if (empty($ownRoles)) {
  122. return false;
  123. }
  124. $count = 0;
  125. foreach ($ownRoles as $role) {
  126. if (self::hasRole($role, $roles)) {
  127. if ($oneRoleIsEnough) {
  128. return true;
  129. }
  130. $count++;
  131. } else {
  132. if (!$oneRoleIsEnough) {
  133. return false;
  134. }
  135. }
  136. }
  137. if ($count === count($ownRoles)) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. }