AdminRoleTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Models\Traits;
  3. use Illuminate\Support\Facades\Config;
  4. use Illuminate\Support\Facades\Cache;
  5. trait AdminRoleTrait
  6. {
  7. public function cachedPermissions()
  8. {
  9. $rolePrimaryKey = $this->primaryKey;
  10. $cacheKey = 'admin_permissions_for_role_'.$this->$rolePrimaryKey;
  11. return Cache::tags(Config::get('admin.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
  12. return $this->perms()->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.permission_role_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.permission_role_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.permission_role_table'))->flush();
  31. return $result;
  32. }
  33. /**
  34. * 与用户的多对多关系
  35. *
  36. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  37. */
  38. public function users()
  39. {
  40. return $this->belongsToMany(Config::get('auth.providers.users.model'), Config::get('admin.role_user_table'),
  41. Config::get('admin.role_foreign_key'),Config::get('admin.user_foreign_key'));
  42. }
  43. /**
  44. * 与权限的多对多关系
  45. *
  46. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  47. */
  48. public function perms()
  49. {
  50. return $this->belongsToMany(Config::get('admin.permission'), Config::get('admin.permission_role_table'));
  51. }
  52. /**
  53. * 与菜单的多对多关系
  54. *
  55. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  56. */
  57. public function menus()
  58. {
  59. return $this->belongsToMany(Config::get('admin.menu'), Config::get('admin.menu_role_table'),
  60. Config::get('admin.role_foreign_key'), Config::get('admin.menu_foreign_key'));
  61. }
  62. /**
  63. * 当删除的时候,把角色关系也删除
  64. *
  65. * @return void|bool
  66. */
  67. public static function boot()
  68. {
  69. parent::boot();
  70. static::deleting(function($role) {
  71. if (!method_exists(Config::get('admin.role'), 'bootSoftDeletes')) {
  72. $role->users()->sync([]);
  73. $role->perms()->sync([]);
  74. $role->menus()->sync([]);
  75. }
  76. return true;
  77. });
  78. }
  79. /**
  80. * 检查是否有权限
  81. *
  82. * @param string|array $name 权限名
  83. * @param bool $requireAll All 是否检查全部权限
  84. *
  85. * @return bool
  86. */
  87. public function hasPermission($name, $requireAll = false)
  88. {
  89. if (is_array($name)) {
  90. foreach ($name as $permissionName) {
  91. $hasPermission = $this->hasPermission($permissionName);
  92. if ($hasPermission && !$requireAll) {
  93. return true;
  94. } elseif (!$hasPermission && $requireAll) {
  95. return false;
  96. }
  97. }
  98. // If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found
  99. // If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found.
  100. // Return the value of $requireAll;
  101. return $requireAll;
  102. } else {
  103. foreach ($this->cachedPermissions() as $permission) {
  104. if ($permission->name == $name) {
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. /**
  112. * 保存权限
  113. *
  114. * @param mixed $inputPermissions
  115. *
  116. * @return void
  117. */
  118. public function savePermissions($inputPermissions)
  119. {
  120. if (!empty($inputPermissions)) {
  121. $this->perms()->sync($inputPermissions);
  122. } else {
  123. $this->perms()->detach();
  124. }
  125. }
  126. }