AuthUserTrait.php 4.3 KB

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