HasPermissions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. *
  30. * @return bool
  31. */
  32. public function can($ability): 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. *
  51. * @return bool
  52. */
  53. public function cannot(string $permission): bool
  54. {
  55. return ! $this->can($permission);
  56. }
  57. /**
  58. * Check if user is administrator.
  59. *
  60. * @return mixed
  61. */
  62. public function isAdministrator(): bool
  63. {
  64. $roleModel = config('admin.database.roles_model');
  65. return $this->isRole($roleModel::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. }