HasPermissions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * 来自 laravel-admin
  4. */
  5. namespace App\Utils;
  6. use Illuminate\Support\Collection;
  7. trait HasPermissions
  8. {
  9. /**
  10. * 获取所有权限, 包含角色中的
  11. *
  12. * @return Collection
  13. */
  14. public function allPermissions()
  15. {
  16. return $this
  17. ->roles()
  18. ->with('permissions')
  19. ->get()
  20. ->pluck('permissions')
  21. ->flatten()
  22. ->merge($this->permissions)
  23. ->unique('id')
  24. ->values();
  25. }
  26. /**
  27. * 判断是否有某个权限
  28. *
  29. * @param string $ability
  30. * @param array $arguments
  31. *
  32. * @return bool
  33. */
  34. public function can($ability, $arguments = [])
  35. {
  36. if ($this->isAdministrator()) {
  37. return true;
  38. }
  39. if ($this->permissions->pluck('slug')->contains($ability)) {
  40. return true;
  41. }
  42. return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($ability);
  43. }
  44. /**
  45. * 判断是不是没权限
  46. *
  47. * @param string $ability
  48. * @param array $arguments
  49. *
  50. * @return bool
  51. */
  52. public function cannot($ability, $arguments = [])
  53. {
  54. return !$this->can($ability);
  55. }
  56. /**
  57. * 判断是不是超级管理员
  58. *
  59. * @return bool
  60. */
  61. public function isAdministrator()
  62. {
  63. return $this->isRole('administrator');
  64. }
  65. /**
  66. * 判断是不是普通超级管理员
  67. *
  68. * @return bool
  69. */
  70. public function isNormalAdministrator()
  71. {
  72. return $this->isRole('normalAdministrator');
  73. }
  74. /**
  75. * 判断是不是区域管理员
  76. *
  77. * @return bool
  78. */
  79. public function isAreaAdmin()
  80. {
  81. return $this->inRoles(['admin-area']);
  82. }
  83. /**
  84. * 判断是不是特定的角色
  85. *
  86. * @param string $role
  87. *
  88. * @return bool
  89. */
  90. public function isRole(string $role)
  91. {
  92. return $this->roles->pluck('slug')->contains($role);
  93. }
  94. /**
  95. * 判断是不是角色之一
  96. *
  97. * @param array $roles
  98. *
  99. * @return bool
  100. */
  101. public function inRoles($roles = [])
  102. {
  103. return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty();
  104. }
  105. /**
  106. * 判断是不是能通过这些角色的验证
  107. *
  108. * @param mixed $roles 角色数组, 其中元素应该是有 slug 键的 ArrayAccess
  109. *
  110. * @return bool
  111. */
  112. public function visible($roles = [])
  113. {
  114. if (empty($roles)) {
  115. return true;
  116. }
  117. $roles = array_column($roles, 'slug');
  118. return $this->inRoles($roles) || $this->isAdministrator();
  119. }
  120. protected static function boot()
  121. {
  122. parent::boot();
  123. static::deleting(function ($model) {
  124. $model->roles()->detach();
  125. $model->permissions()->detach();
  126. });
  127. }
  128. }