UserRelations.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Modules\User\Models\Traits;
  3. use Catch\CatchAdmin;
  4. use Catch\Support\Module\ModuleRepository;
  5. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Route;
  8. use Illuminate\Support\Str;
  9. use Modules\Permissions\Models\Permissions;
  10. trait UserRelations
  11. {
  12. protected bool $isPermissionModuleEnabled = false;
  13. /**
  14. * init traits
  15. */
  16. public function initializeUserRelations(): void
  17. {
  18. $this->isPermissionModuleEnabled = app(ModuleRepository::class)->enabled('permissions');
  19. if ($this->isPermissionModuleEnabled) {
  20. $this->with = ['roles', 'jobs'];
  21. }
  22. }
  23. /**
  24. * roles
  25. *
  26. * @return BelongsToMany
  27. */
  28. public function roles(): BelongsToMany
  29. {
  30. return $this->belongsToMany($this->getRolesModel(), 'user_has_roles', 'user_id', 'role_id');
  31. }
  32. /**
  33. * jobs
  34. *
  35. * @return BelongsToMany
  36. */
  37. public function jobs(): BelongsToMany
  38. {
  39. return $this->belongsToMany($this->getJobsModel(), 'user_has_jobs', 'user_id', 'job_id');
  40. }
  41. /**
  42. * permissions
  43. */
  44. public function withPermissions(): self
  45. {
  46. if (! $this->isPermissionModuleEnabled) {
  47. return $this;
  48. }
  49. /* @var Permissions $permissionsModel */
  50. $permissionsModel = app($this->getPermissionsModel())->where('hidden',1)->orderByDesc('sort');
  51. if ($this->isSuperAdmin()) {
  52. $permissions = $permissionsModel->get();
  53. } else {
  54. $permissions = Collection::make();
  55. $roleIds = $this->roles()->pluck('role_id')->toArray();
  56. app($this->getRolesModel())->whereIn('id', $roleIds)->with(['permissions' => function($query){
  57. return $query->where('hidden',1)->orderByDesc('sort');
  58. }])->get()
  59. ->each(function ($role) use (&$permissions) {
  60. $permissions = $permissions->concat($role->permissions);
  61. });
  62. $permissions = $permissions->unique('id');
  63. }
  64. $this->setAttribute('permissions', $permissions->each(fn ($permission) => $permission->setAttribute('hidden', $permission->isHidden())));
  65. return $this;
  66. }
  67. /**
  68. *
  69. * permission module controller.action
  70. *
  71. * @param string|null $permission
  72. * @return bool
  73. */
  74. public function can(string $permission = null): bool
  75. {
  76. if (! $this->isPermissionModuleEnabled) {
  77. return true;
  78. }
  79. if ($this->isSuperAdmin()) {
  80. return true;
  81. }
  82. $this->withPermissions();
  83. $actions = Collection::make();
  84. $this->getAttribute('permissions')->each(function ($permission) use (&$actions) {
  85. if ($permission->isAction()) {
  86. [$controller, $action] = explode('@', $permission->permission_mark);
  87. $actions->add(strtolower(CatchAdmin::getModuleControllerNamespace($permission->module).$controller.'Controller@'.$action));
  88. }
  89. });
  90. if ($permission) {
  91. [$module, $controller, $action] = explode('@', $permission);
  92. $permission = strtolower(CatchAdmin::getModuleControllerNamespace($module).$controller.'Controller@'.$action);
  93. }else{
  94. $permission = strtolower(Route::currentRouteAction());
  95. }
  96. return $actions->contains($permission);
  97. }
  98. /**
  99. * get RolesModel
  100. *
  101. * @see \Modules\Permissions\Models\Roles
  102. * @return string
  103. */
  104. protected function getRolesModel(): string
  105. {
  106. return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Roles';
  107. }
  108. /**
  109. * get JobsModel
  110. *
  111. * @see \Modules\Permissions\Models\Jobs
  112. * @return string
  113. */
  114. protected function getJobsModel(): string
  115. {
  116. return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Jobs';
  117. }
  118. /**
  119. * get PermissionsModel
  120. *
  121. * @return string
  122. *@see \Modules\Permissions\Models\Permissions
  123. */
  124. protected function getPermissionsModel(): string
  125. {
  126. return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Permissions';
  127. }
  128. }