HasPermissions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @param array|mixed $arguments
  30. * @return bool
  31. */
  32. public function can($ability, $paramters = []): bool
  33. {
  34. if (! $ability) {
  35. return false;
  36. }
  37. if ($this->isAdministrator()) {
  38. return true;
  39. }
  40. $permissions = $this->allPermissions();
  41. return $permissions->pluck('slug')->contains($ability) ?:
  42. $permissions
  43. ->pluck('id')
  44. ->contains($ability);
  45. }
  46. /**
  47. * Check if user has no permission.
  48. *
  49. * @param $permission
  50. * @return bool
  51. */
  52. public function cannot(string $permission): bool
  53. {
  54. return ! $this->can($permission);
  55. }
  56. /**
  57. * Check if user is administrator.
  58. *
  59. * @return mixed
  60. */
  61. public function isAdministrator(): bool
  62. {
  63. $roleModel = config('admin.database.roles_model');
  64. return $this->isRole($roleModel::ADMINISTRATOR);
  65. }
  66. /**
  67. * Check if user is $role.
  68. *
  69. * @param string $role
  70. * @return mixed
  71. */
  72. public function isRole(string $role): bool
  73. {
  74. /* @var Collection $roles */
  75. $roles = $this->roles;
  76. return $roles->pluck('slug')->contains($role) ?:
  77. $roles->pluck('id')->contains($role);
  78. }
  79. /**
  80. * Check if user in $roles.
  81. *
  82. * @param string|array|Arrayable $roles
  83. * @return mixed
  84. */
  85. public function inRoles($roles = []): bool
  86. {
  87. /* @var Collection $all */
  88. $all = $this->roles;
  89. $roles = Helper::array($roles);
  90. return $all->pluck('slug')->intersect($roles)->isNotEmpty() ?:
  91. $all->pluck('id')->intersect($roles)->isNotEmpty();
  92. }
  93. /**
  94. * If visible for roles.
  95. *
  96. * @param $roles
  97. * @return bool
  98. */
  99. public function visible($roles = []): bool
  100. {
  101. if (empty($roles)) {
  102. return false;
  103. }
  104. if ($this->isAdministrator()) {
  105. return true;
  106. }
  107. return $this->inRoles($roles);
  108. }
  109. /**
  110. * Detach models from the relationship.
  111. *
  112. * @return void
  113. */
  114. protected static function bootHasPermissions()
  115. {
  116. static::deleting(function ($model) {
  117. $model->roles()->detach();
  118. });
  119. }
  120. }