HasPermissions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Dcat\Admin\Traits;
  3. use Dcat\Admin\Models\Role;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Support\Collection;
  7. trait HasPermissions
  8. {
  9. protected $allPermissions;
  10. /**
  11. * Get all permissions of user.
  12. *
  13. * @return mixed
  14. */
  15. public function allPermissions(): Collection
  16. {
  17. if ($this->allPermissions) {
  18. return $this->allPermissions;
  19. }
  20. return $this->allPermissions =
  21. $this->roles
  22. ->pluck('permissions')
  23. ->flatten()
  24. ->keyBy($this->getKeyName());
  25. }
  26. /**
  27. * Check if user has permission.
  28. *
  29. * @param $ability
  30. *
  31. * @return bool
  32. */
  33. public function can($ability): bool
  34. {
  35. if (! $ability) {
  36. return false;
  37. }
  38. if ($this->isAdministrator()) {
  39. return true;
  40. }
  41. $permissions = $this->allPermissions();
  42. return $permissions->pluck('slug')->contains($ability) ?:
  43. $permissions
  44. ->pluck('id')
  45. ->contains($ability);
  46. }
  47. /**
  48. * Check if user has no permission.
  49. *
  50. * @param $permission
  51. *
  52. * @return bool
  53. */
  54. public function cannot(string $permission): bool
  55. {
  56. return ! $this->can($permission);
  57. }
  58. /**
  59. * Check if user is administrator.
  60. *
  61. * @return mixed
  62. */
  63. public function isAdministrator(): bool
  64. {
  65. return $this->isRole(Role::ADMINISTRATOR);
  66. }
  67. /**
  68. * Check if user is $role.
  69. *
  70. * @param string $role
  71. *
  72. * @return mixed
  73. */
  74. public function isRole(string $role): bool
  75. {
  76. /* @var Collection $roles */
  77. $roles = $this->roles;
  78. return $roles->pluck('slug')->contains($role) ?:
  79. $roles->pluck('id')->contains($role);
  80. }
  81. /**
  82. * Check if user in $roles.
  83. *
  84. * @param string|array|Arrayable $roles
  85. *
  86. * @return mixed
  87. */
  88. public function inRoles($roles = []): bool
  89. {
  90. /* @var Collection $all */
  91. $all = $this->roles;
  92. $roles = Helper::array($roles);
  93. return $all->pluck('slug')->intersect($roles)->isNotEmpty() ?:
  94. $all->pluck('id')->intersect($roles)->isNotEmpty();
  95. }
  96. /**
  97. * If visible for roles.
  98. *
  99. * @param $roles
  100. *
  101. * @return bool
  102. */
  103. public function visible($roles = []): bool
  104. {
  105. if (empty($roles)) {
  106. return false;
  107. }
  108. if ($this->isAdministrator()) {
  109. return true;
  110. }
  111. return $this->inRoles($roles);
  112. }
  113. /**
  114. * Detach models from the relationship.
  115. *
  116. * @return void
  117. */
  118. protected static function boot()
  119. {
  120. parent::boot();
  121. static::deleting(function ($model) {
  122. $model->roles()->detach();
  123. });
  124. }
  125. }