在Laravel中,我有一个团队、项目、用户、角色和权限之间的关系。
用户可以拥有多个团队,一个团队可以有多个项目,而用户可以拥有用户角色、团队角色和项目角色。我想知道如何获取用户在每个项目、每个团队以及仅限用户的权限。
User table
Teams table
Projects table
Roles table
Permissions table
Role_permission table
project_user table
team_user table
用户在一个项目中只能拥有一个角色,在一个团队中也只能拥有一个角色,而在用户自身也只能拥有一个角色,该角色可以是管理员或普通用户。
我正在尝试获取用户项目角色的方式,同时我已经跳过了团队的代码,因为我认为它与项目的代码非常相似。
class User extends Authenticatable
{
public function projectRole()
{
//I can't find the way to insert project_id like in where, because current_project_id is null in boot
return $this->belongsToMany(Role::class, 'project_user')->where('project_id', '=', $this->current_project_id)->first();
}
public function projectPermissions()
{
return $this->projectRole()->permissions;
}
public function permissions()
{
//I can't find the way to get all team permission, project permissions and user permissions
}
public function role() : BelongsTo
{
//Only can be User role, Admin or Support
return $this->belongsTo(Role::class);
}
public function userPermissions()
{
return $this->role->permissions;
}
}
class Role extends Model
{
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
}
我想使用权限作为门槛,传递给Inertia前端,我正在尝试类似这样的方法。
Gate::before(function ($user, $permission) {
return $user->projectPermissions($user->currentProject)->contains($permission) || $user->teamPermissions($user->currentTeam)->contains($permission) || $user->teamPermissions($user->currentTeam)->contains($permission)
|| $user->userPermissions()->contains($permission);
});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
根据您的解释,我按照以下方式进行操作:
表格 *************
project_user 表:
Schema::create('project_user', function (Blueprint $table) { $table->unsignedBigInteger('project_id'); $table->unsignedBigInteger('user_id'); });permission_role 表:
Schema::create('permission_role', function (Blueprint $table) { $table->unsignedBigInteger('permission_id'); $table->unsignedBigInteger('role_id'); });role_user 表:
Schema::create('role_user', function (Blueprint $table) { $table->unsignedBigInteger('role_id'); $table->unsignedBigInteger('user_id'); });Models *************
用户模型:
/** * Team Relationship * * @return BelongsToMany */ public function teams(): BelongsToMany { return $this->belongsToMany(Team::class); } /** * Project Relationship * * @return BelongsToMany */ public function projects(): BelongsToMany { return $this->belongsToMany(Project::class); } /** * Role Relationship * * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, 'role_user'); }团队模型:
/** * User Relationship * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class); } /** * Project Relationship * * @return HasMany */ public function projects(): HasMany { return $this->hasMany(Project::class); }项目模型:
/** * Team Relation * * @return BelongsTo */ public function team(): BelongsTo { return $this->belongsTo(Team::class); } /** * User Relation * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class); }角色模型:
/** * Permission Relation * * @return BelongsToMany */ public function permissions(): BelongsToMany { return $this->belongsToMany(Permission::class, 'permission_role'); } /** * User Relation * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class,'role_user'); }权限模型:
/** * Role Relation * * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, 'permission_role'); }首先,我们创建我们期望的角色。例如:
角色1:名称=超级管理员,类型=用户
角色2:名称=团队主管,类型=团队
角色3:名称=项目开发者,类型=项目
现在,我们将期望的角色分配给我们的用户。这些角色存储在role_user表中。
然后,根据这些角色,您可以确定每个用户在每个项目和团队中的职责。