Role.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Dcat\Admin\Models;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6. class Role extends Model
  7. {
  8. const ADMINISTRATOR = 'administrator';
  9. const ADMINISTRATOR_ID = 1;
  10. protected $fillable = ['name', 'slug'];
  11. /**
  12. * Create a new Eloquent model instance.
  13. *
  14. * @param array $attributes
  15. */
  16. public function __construct(array $attributes = [])
  17. {
  18. $connection = config('admin.database.connection') ?: config('database.default');
  19. $this->setConnection($connection);
  20. $this->setTable(config('admin.database.roles_table'));
  21. parent::__construct($attributes);
  22. }
  23. /**
  24. * A role belongs to many users.
  25. *
  26. * @return BelongsToMany
  27. */
  28. public function administrators() : BelongsToMany
  29. {
  30. $pivotTable = config('admin.database.role_users_table');
  31. $relatedModel = config('admin.database.users_model');
  32. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
  33. }
  34. /**
  35. * A role belongs to many permissions.
  36. *
  37. * @return BelongsToMany
  38. */
  39. public function permissions() : BelongsToMany
  40. {
  41. $pivotTable = config('admin.database.role_permissions_table');
  42. $relatedModel = config('admin.database.permissions_model');
  43. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id');
  44. }
  45. /**
  46. * Check user has permission.
  47. *
  48. * @param $permission
  49. *
  50. * @return bool
  51. */
  52. public function can(?string $permission) : bool
  53. {
  54. return $this->permissions()->where('slug', $permission)->exists();
  55. }
  56. /**
  57. * Check user has no permission.
  58. *
  59. * @param $permission
  60. *
  61. * @return bool
  62. */
  63. public function cannot(?string $permission) : bool
  64. {
  65. return !$this->can($permission);
  66. }
  67. /**
  68. * Get id of the permission by id.
  69. *
  70. * @param array $roleIds
  71. * @return \Illuminate\Support\Collection
  72. */
  73. public static function getPermissionId(array $roleIds)
  74. {
  75. if (!$roleIds) {
  76. return collect();
  77. }
  78. $related = config('admin.database.role_permissions_table');
  79. $model = new static;
  80. $keyName = $model->getKeyName();
  81. return $model->newQuery()
  82. ->select(['permission_id', 'role_id'])
  83. ->leftJoin($related, $keyName, '=', 'role_id')
  84. ->whereIn($keyName, $roleIds)
  85. ->get()
  86. ->groupBy('role_id')
  87. ->map(function ($v) {
  88. $v = $v instanceof Arrayable ? $v->toArray() : $v;
  89. return array_column($v, 'permission_id');
  90. });
  91. }
  92. /**
  93. * @param string $slug
  94. * @return bool
  95. */
  96. public static function isAdministrator(?string $slug)
  97. {
  98. return $slug === static::ADMINISTRATOR;
  99. }
  100. /**
  101. * Detach models from the relationship.
  102. *
  103. * @return void
  104. */
  105. protected static function boot()
  106. {
  107. parent::boot();
  108. static::deleting(function ($model) {
  109. $model->administrators()->detach();
  110. $model->permissions()->detach();
  111. });
  112. }
  113. }