AdminUsersTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Models\Traits;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Config;
  5. trait AdminUsersTrait
  6. {
  7. public function cachedRoles()
  8. {
  9. $userPrimaryKey = $this->primaryKey;
  10. $cacheKey = 'admin_roles_for_user_'.$this->$userPrimaryKey;
  11. return Cache::tags(Config::get('admin.role_user_table'))->remember($cacheKey, Config::get('admin.cache.ttl'), function () {
  12. return $this->roles()->get();
  13. });
  14. }
  15. public function save(array $options = [])
  16. { //both inserts and updates
  17. $result = parent::save($options);
  18. Cache::tags(Config::get('admin.role_user_table'))->flush();
  19. return $result;
  20. }
  21. public function delete(array $options = [])
  22. { //soft or hard
  23. $result = parent::delete($options);
  24. Cache::tags(Config::get('admin.role_user_table'))->flush();
  25. return $result;
  26. }
  27. public function restore()
  28. { //soft delete undo's
  29. $result = parent::restore();
  30. Cache::tags(Config::get('admin.role_user_table'))->flush();
  31. return $result;
  32. }
  33. /**
  34. * 与角色的多对多关系
  35. *
  36. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  37. */
  38. public function roles()
  39. {
  40. return $this->belongsToMany(Config::get('admin.role'), Config::get('admin.role_user_table'),
  41. Config::get('admin.user_foreign_key'), Config::get('admin.role_foreign_key'));
  42. }
  43. /**
  44. * 当删除的时候,把用户关系也删除
  45. *
  46. * @return void|bool
  47. */
  48. public static function boot()
  49. {
  50. parent::boot();
  51. static::deleting(function($user) {
  52. if (!method_exists(Config::get('auth.providers.users.model'), 'bootSoftDeletes')) {
  53. $user->roles()->sync([]);
  54. }
  55. return true;
  56. });
  57. }
  58. /**
  59. * 检查用户是是否有角色.
  60. *
  61. * @param string|array $name 角色名.
  62. * @param bool $requireAll 是否有全部请求权限
  63. *
  64. * @return bool
  65. */
  66. public function hasRole($name, $requireAll = false)
  67. {
  68. if (is_array($name)) {
  69. foreach ($name as $roleName) {
  70. $hasRole = $this->hasRole($roleName);
  71. if ($hasRole && !$requireAll) {
  72. return true;
  73. } elseif (!$hasRole && $requireAll) {
  74. return false;
  75. }
  76. }
  77. // If we've made it this far and $requireAll is FALSE, then NONE of the roles were found
  78. // If we've made it this far and $requireAll is TRUE, then ALL of the roles were found.
  79. // Return the value of $requireAll;
  80. return $requireAll;
  81. } else {
  82. foreach ($this->cachedRoles() as $role) {
  83. if ($role->name == $name) {
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. /**
  91. * 检查用户是否有权限
  92. *
  93. * @param string|array $permission 权限名
  94. * @param bool $requireAll 是否有全部请求权限
  95. *
  96. * @return bool
  97. */
  98. public function can($permission, $requireAll = false)
  99. {
  100. if (is_array($permission)) {
  101. foreach ($permission as $permName) {
  102. $hasPerm = $this->can($permName);
  103. if ($hasPerm && !$requireAll) {
  104. return true;
  105. } elseif (!$hasPerm && $requireAll) {
  106. return false;
  107. }
  108. }
  109. // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found
  110. // If we've made it this far and $requireAll is TRUE, then ALL of the perms were found.
  111. // Return the value of $requireAll;
  112. return $requireAll;
  113. } else {
  114. foreach ($this->cachedRoles() as $role) {
  115. // Validate against the Permission table
  116. foreach ($role->cachedPermissions() as $perm) {
  117. if (str_is( $permission, $perm->name) ) {
  118. return true;
  119. }
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * 保存角色
  127. *
  128. * @param mixed $roles
  129. */
  130. public function saveRoles($roles)
  131. {
  132. if (!empty($roles)) {
  133. $this->roles()->sync($roles);
  134. } else {
  135. $this->roles()->detach();
  136. }
  137. }
  138. }