feat:动态权限
This commit is contained in:
@@ -89,7 +89,7 @@ class UserController extends Controller
|
||||
public function online(Request $request)
|
||||
{
|
||||
/* @var Users $user */
|
||||
$user = $this->getLoginUser();
|
||||
$user = $this->getLoginUser()->withPermissions();
|
||||
|
||||
if ($request->isMethod('post')) {
|
||||
return $user->updateBy($user->id, $request->all());
|
||||
|
98
modules/User/Models/Traits/UserRelations.php
Normal file
98
modules/User/Models/Traits/UserRelations.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace Modules\User\Models\Traits;
|
||||
|
||||
|
||||
use Catch\CatchAdmin;
|
||||
use Catch\Support\Module\ModuleRepository;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
trait UserRelations
|
||||
{
|
||||
|
||||
/**
|
||||
* init traits
|
||||
*/
|
||||
public function initializeUserRelations(): void
|
||||
{
|
||||
if (app(ModuleRepository::class)->enabled('permissions')) {
|
||||
$this->with = ['roles', 'jobs'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/* @var \Modules\Permissions\Models\PermissionsModel $permissionsModel */
|
||||
$permissionsModel = app($this->getPermissionsModel());
|
||||
|
||||
if ($this->isSuperAdmin()) {
|
||||
$permissions = $permissionsModel->get();
|
||||
} else {
|
||||
$roles = app($this->getRolesModel())->with(['permissions'])->get();
|
||||
|
||||
$permissions = [];
|
||||
}
|
||||
|
||||
$this->setAttribute('permissions', $permissions->each(fn($permission) => $permission->setAttribute('hidden', $permission->isHidden())));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get RolesModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\RolesModel
|
||||
* @return string
|
||||
*/
|
||||
protected function getRolesModel(): string
|
||||
{
|
||||
return '\\' . CatchAdmin::getModuleModelNamespace('permissions') . 'RolesModel';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get JobsModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\JobsModel
|
||||
* @return string
|
||||
*/
|
||||
protected function getJobsModel(): string
|
||||
{
|
||||
return '\\'. CatchAdmin::getModuleModelNamespace('permissions') . 'JobsModel';
|
||||
}
|
||||
|
||||
/**
|
||||
* get PermissionsModel
|
||||
*
|
||||
* @see \Modules\Permissions\Models\PermissionsModel
|
||||
* @return string
|
||||
*/
|
||||
protected function getPermissionsModel(): string
|
||||
{
|
||||
return '\\'. CatchAdmin::getModuleModelNamespace('permissions') . 'PermissionsModel';
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ namespace Modules\User\Models;
|
||||
use Catch\Base\CatchModel as Model;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Modules\User\Models\Traits\UserRelations;
|
||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
|
||||
@@ -24,7 +25,7 @@ use Illuminate\Auth\Authenticatable;
|
||||
*/
|
||||
class Users extends Model implements AuthenticatableContract, JWTSubject
|
||||
{
|
||||
use Authenticatable;
|
||||
use Authenticatable, UserRelations;
|
||||
|
||||
protected $fillable = [
|
||||
'id', 'username', 'email', 'avatar', 'password', 'remember_token', 'creator_id', 'status', 'login_ip', 'login_at', 'created_at', 'updated_at', 'deleted_at'
|
||||
|
Reference in New Issue
Block a user