HasPermissions.php 2.8 KB

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