Auth.php 3.3 KB

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