UserRelations.php 4.1 KB

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