Auth.php 4.1 KB

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