2022-12-10 18:29:42 +08:00
|
|
|
<?php
|
|
|
|
|
2022-12-14 19:25:52 +08:00
|
|
|
namespace Modules\User\Models\Traits;
|
2022-12-10 18:29:42 +08:00
|
|
|
|
|
|
|
use Catch\CatchAdmin;
|
|
|
|
use Catch\Support\Module\ModuleRepository;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2022-12-14 19:25:52 +08:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
2022-12-23 19:47:13 +08:00
|
|
|
use Modules\Permissions\Models\Permissions;
|
2022-12-10 18:29:42 +08:00
|
|
|
|
|
|
|
trait UserRelations
|
|
|
|
{
|
2022-12-14 19:25:52 +08:00
|
|
|
protected bool $isPermissionModuleEnabled = false;
|
2022-12-10 18:29:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* init traits
|
|
|
|
*/
|
|
|
|
public function initializeUserRelations(): void
|
|
|
|
{
|
2022-12-14 19:25:52 +08:00
|
|
|
$this->isPermissionModuleEnabled = app(ModuleRepository::class)->enabled('permissions');
|
|
|
|
|
|
|
|
if ($this->isPermissionModuleEnabled) {
|
|
|
|
$this->with = ['roles', 'jobs'];
|
|
|
|
}
|
2022-12-10 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* roles
|
|
|
|
*
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function roles(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany($this->getRolesModel(), 'user_has_roles', 'user_id', 'role_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jobs
|
|
|
|
*
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function jobs(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany($this->getJobsModel(), 'user_has_jobs', 'user_id', 'job_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* permissions
|
|
|
|
*/
|
|
|
|
public function withPermissions(): self
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
/* @var Permissions $permissionsModel */
|
2022-12-10 18:29:42 +08:00
|
|
|
$permissionsModel = app($this->getPermissionsModel());
|
|
|
|
|
|
|
|
if ($this->isSuperAdmin()) {
|
|
|
|
$permissions = $permissionsModel->get();
|
|
|
|
} else {
|
2022-12-18 22:44:58 +08:00
|
|
|
$permissions = Collection::make();
|
2022-12-10 18:29:42 +08:00
|
|
|
|
2022-12-18 22:44:58 +08:00
|
|
|
app($this->getRolesModel())->with(['permissions'])->get()
|
|
|
|
|
2022-12-23 19:47:13 +08:00
|
|
|
->each(function ($role) use (&$permissions) {
|
2022-12-18 22:44:58 +08:00
|
|
|
$permissions = $permissions->concat($role->permissions);
|
|
|
|
});
|
|
|
|
|
|
|
|
$permissions = $permissions->unique();
|
2022-12-10 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
2022-12-14 19:25:52 +08:00
|
|
|
$this->setAttribute('permissions', $permissions->each(fn ($permission) => $permission->setAttribute('hidden', $permission->isHidden())));
|
2022-12-10 18:29:42 +08:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:25:52 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* permission module controller.action
|
|
|
|
*
|
|
|
|
* @param string|null $permission
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function can(string $permission = null): bool
|
|
|
|
{
|
|
|
|
if (! $this->isPermissionModuleEnabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isSuperAdmin()) {
|
2022-12-23 19:47:13 +08:00
|
|
|
return true;
|
2022-12-14 19:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->withPermissions();
|
|
|
|
|
|
|
|
$actions = Collection::make();
|
|
|
|
|
2022-12-18 22:44:58 +08:00
|
|
|
$this->getAttribute('permissions')->each(function ($permission) use (&$actions) {
|
2022-12-14 19:25:52 +08:00
|
|
|
if ($permission->isAction()) {
|
|
|
|
[$controller, $action] = explode('@', $permission->permission_mark);
|
|
|
|
|
|
|
|
$actions->add(CatchAdmin::getModuleControllerNamespace($permission->module).$controller.'Controller@'.$action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $actions->contains($permission ?: Route::currentRouteAction());
|
|
|
|
}
|
2022-12-10 18:29:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get RolesModel
|
|
|
|
*
|
2022-12-23 19:47:13 +08:00
|
|
|
* @see \Modules\Permissions\Models\Roles
|
2022-12-10 18:29:42 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getRolesModel(): string
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Roles';
|
2022-12-10 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get JobsModel
|
|
|
|
*
|
2022-12-23 19:47:13 +08:00
|
|
|
* @see \Modules\Permissions\Models\Jobs
|
2022-12-10 18:29:42 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getJobsModel(): string
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Jobs';
|
2022-12-10 18:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get PermissionsModel
|
|
|
|
*
|
|
|
|
* @return string
|
2022-12-23 19:47:13 +08:00
|
|
|
*@see \Modules\Permissions\Models\Permissions
|
2022-12-10 18:29:42 +08:00
|
|
|
*/
|
|
|
|
protected function getPermissionsModel(): string
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
return '\\'.CatchAdmin::getModuleModelNamespace('permissions').'Permissions';
|
2022-12-10 18:29:42 +08:00
|
|
|
}
|
|
|
|
}
|