Auth.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. *
  12. * It can be used anywhere in the application due to static access.
  13. * So in the view we can use this shortcut to check if a user is logged in:
  14. *
  15. * if (Auth::id()) {
  16. * // Display element
  17. * }
  18. *
  19. * Expects the Role session infos to be either
  20. * - `Auth.User.role_id` (single) or
  21. * - `Auth.User.Role` (multi - flat array of roles, or array role data)
  22. * and can be adjusted via constants and defined().
  23. * Same goes for Right data.
  24. *
  25. * @author Mark Scherer
  26. * @license MIT
  27. * @php 5
  28. * @cakephp 2
  29. */
  30. class Auth {
  31. /**
  32. * Get the user id of the current session.
  33. *
  34. * This can be used anywhere to check if a user is logged in.
  35. *
  36. * @return mixed User id if existent, null otherwise.
  37. */
  38. public static function id() {
  39. return CakeSession::read('Auth.User.id');
  40. }
  41. /**
  42. * Get the role(s) of the current session.
  43. *
  44. * It will return the single role for single role setup, and a flat
  45. * list of roles for multi role setup.
  46. *
  47. * @return mixed String or array of roles or null if inexistent.
  48. */
  49. public static function roles() {
  50. $roles = CakeSession::read('Auth.User.' . USER_ROLE_KEY);
  51. if (!is_array($roles)) {
  52. return $roles;
  53. }
  54. if (isset($roles[0]['id'])) {
  55. $roles = Hash::extract($roles, '{n}.id');
  56. }
  57. return $roles;
  58. }
  59. /**
  60. * Get the user data of the current session.
  61. *
  62. * @param string $key Key in dot syntax.
  63. * @return mixed Data
  64. */
  65. public static function user($key = null) {
  66. if ($key) {
  67. $key = '.' . $key;
  68. }
  69. return CakeSession::read('Auth.User' . $key);
  70. }
  71. /**
  72. * Check if the current session has this role.
  73. *
  74. * @param mixed $role
  75. * @param mixed $providedRoles
  76. * @return boolean Success
  77. */
  78. public static function hasRole($ownRole, $providedRoles = null) {
  79. if ($providedRoles !== null) {
  80. $roles = $providedRoles;
  81. } else {
  82. $roles = self::roles();
  83. }
  84. if (is_array($roles)) {
  85. if (in_array($ownRole, $roles)) {
  86. return true;
  87. }
  88. } elseif (!empty($roles)) {
  89. if ($ownRole == $roles) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Check if the current session has one of these roles.
  97. *
  98. * You can either require one of the roles (default), or you can require all
  99. * roles to match.
  100. *
  101. * @param mixed $roles
  102. * @param boolean $oneRoleIsEnough (if all $roles have to match instead of just one)
  103. * @param mixed $providedRoles
  104. * @return boolean Success
  105. */
  106. public static function hasRoles($ownRoles, $oneRoleIsEnough = true, $providedRoles = null) {
  107. if ($providedRoles !== null) {
  108. $roles = $providedRoles;
  109. } else {
  110. $roles = self::roles();
  111. }
  112. $ownRoles = (array)$ownRoles;
  113. if (empty($ownRoles)) {
  114. return false;
  115. }
  116. $count = 0;
  117. foreach ($ownRoles as $role) {
  118. if (self::hasRole($role, $roles)) {
  119. if ($oneRoleIsEnough) {
  120. return true;
  121. }
  122. $count++;
  123. } else {
  124. if (!$oneRoleIsEnough) {
  125. return false;
  126. }
  127. }
  128. }
  129. if ($count === count($ownRoles)) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Check if the current session has this right.
  136. *
  137. * Rights can be an additional element to give permissions, e.g.
  138. * the right to send messages/emails, to friend request other users,...
  139. * This can be set via Right model and stored in the Auth array upon login
  140. * the same way the roles are.
  141. *
  142. * @param mixed $role
  143. * @param mixed $providedRights
  144. * @return boolean Success
  145. */
  146. public static function hasRight($ownRight, $providedRights = null) {
  147. if ($providedRights !== null) {
  148. $rights = $providedRights;
  149. } else {
  150. $rights = CakeSession::read('Auth.User.' . USER_RIGHT_KEY);
  151. }
  152. $rights = (array)$rights;
  153. if (array_key_exists($ownRight, $rights) && !empty($rights[$ownRight])) {
  154. return true;
  155. }
  156. return false;
  157. }
  158. }