Role.php 3.3 KB

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