CdmsRoles.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Models;
  3. use Encore\Admin\Traits\DefaultDatetimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6. class CdmsRoles extends Model
  7. {
  8. use DefaultDatetimeFormat;
  9. protected $fillable = ['name', 'slug'];
  10. /**
  11. * Create a new Eloquent model instance.
  12. *
  13. * @param array $attributes
  14. */
  15. public function __construct(array $attributes = [])
  16. {
  17. $connection = config('tenancy.database.connection') ?: config('database.default');
  18. $this->setConnection($connection);
  19. $this->setTable(config('tenancy.database.roles_table'));
  20. parent::__construct($attributes);
  21. }
  22. /**
  23. * A role belongs to many users.
  24. *
  25. * @return BelongsToMany
  26. */
  27. public function administrators(): BelongsToMany
  28. {
  29. $pivotTable = config('tenancy.database.role_users_table');
  30. $relatedModel = config('tenancy.database.users_model');
  31. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
  32. }
  33. /**
  34. * A role belongs to many permissions.
  35. *
  36. * @return BelongsToMany
  37. */
  38. public function permissions(): BelongsToMany
  39. {
  40. $pivotTable = config('tenancy.database.role_permissions_table');
  41. $relatedModel = config('tenancy.database.permissions_model');
  42. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id');
  43. }
  44. /**
  45. * A role belongs to many menus.
  46. *
  47. * @return BelongsToMany
  48. */
  49. public function menus(): BelongsToMany
  50. {
  51. $pivotTable = config('tenancy.database.role_menu_table');
  52. $relatedModel = config('tenancy.database.menu_model');
  53. return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'menu_id');
  54. }
  55. /**
  56. * Check user has permission.
  57. *
  58. * @param $permission
  59. *
  60. * @return bool
  61. */
  62. public function can(string $permission): bool
  63. {
  64. return $this->permissions()->where('slug', $permission)->exists();
  65. }
  66. /**
  67. * Check user has no permission.
  68. *
  69. * @param $permission
  70. *
  71. * @return bool
  72. */
  73. public function cannot(string $permission): bool
  74. {
  75. return !$this->can($permission);
  76. }
  77. /**
  78. * Detach models from the relationship.
  79. *
  80. * @return void
  81. */
  82. protected static function boot()
  83. {
  84. parent::boot();
  85. static::deleting(function ($model) {
  86. $model->administrators()->detach();
  87. $model->permissions()->detach();
  88. });
  89. }
  90. public function organization()
  91. {
  92. return $this->hasOne(Organization::class,'id','org_id');
  93. }
  94. }