Auth.php 4.1 KB

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