Auth.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * Convinience 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)
  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 or return empty/null
  26. *
  27. * @return mixed $userId
  28. */
  29. public static function id() {
  30. return CakeSession::read('Auth.User.id');
  31. }
  32. /**
  33. * get the role(s) of the current session or return empty/null
  34. *
  35. * @return mixed $roles
  36. */
  37. public static function roles() {
  38. return CakeSession::read('Auth.User.' . USER_ROLE_KEY);
  39. }
  40. /**
  41. * get the user data of the current session or return empty/null
  42. *
  43. * @param string $key (dot syntax)
  44. * @return mixed $data
  45. */
  46. public static function user($key = null) {
  47. if ($key) {
  48. $key = '.' . $key;
  49. }
  50. return CakeSession::read('Auth.User' . $key);
  51. }
  52. /**
  53. * check if the current session has this right
  54. *
  55. * @param mixed $role
  56. * @param mixed $existingRolesToCheckAgainst
  57. * @return bool $success
  58. */
  59. public static function hasRight($ownRight, $providedRights = null) {
  60. if ($providedRights !== null) {
  61. $rights = $providedRights;
  62. } else {
  63. $rights = CakeSession::read('Auth.User.' . USER_RIGHT_KEY);
  64. }
  65. $rights = (array)$rights;
  66. if (array_key_exists($ownRight, $rights) && !empty($rights[$ownRight])) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. /**
  72. * check if the current session has this role
  73. *
  74. * @param mixed $role
  75. * @param mixed $existingRolesToCheckAgainst
  76. * @return bool $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 oen of these roles
  97. *
  98. * @param mixed $roles
  99. * @param bool $oneRoleIsEnough (if all $roles have to match instead of just one)
  100. * @param mixed $existingRolesToCheckAgainst
  101. * @return bool $success
  102. */
  103. public static function hasRoles($ownRoles, $oneRoleIsEnough = true, $providedRoles = null) {
  104. if ($providedRoles !== null) {
  105. $roles = $providedRoles;
  106. } else {
  107. $roles = self::roles();
  108. }
  109. $ownRoles = (array)$ownRoles;
  110. if (empty($ownRoles)) {
  111. return false;
  112. }
  113. $count = 0;
  114. foreach ($ownRoles as $role) {
  115. if (self::hasRole($role, $roles)) {
  116. if ($oneRoleIsEnough) {
  117. return true;
  118. }
  119. $count++;
  120. } else {
  121. if (!$oneRoleIsEnough) {
  122. return false;
  123. }
  124. }
  125. }
  126. if ($count === count($ownRoles)) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. }