加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel 5.1用户,角色和行动

发布时间:2020-12-14 19:36:52 所属栏目:大数据 来源:网络整理
导读:我正在使用Laravel 5.1创建一个包含用户,角色和操作的应用程序. 表格设置如下: 用户 id name1 John Smith2 Fred Smith 角色 id name1 Administrator2 Customer ROLE_USER user_id role_id1 12 1 行动 id name description path1 dashboard ability to acces
我正在使用Laravel 5.1创建一个包含用户,角色和操作的应用程序.

表格设置如下:

用户

id    name
1     John Smith
2     Fred Smith

角色

id    name
1     Administrator
2     Customer

ROLE_USER

user_id    role_id
1          1
2          1

行动

id    name        description                    path
1     dashboard   ability to access dashboard    /admin
2     editAdmin   ability to edit admins         /admin/users/administrators/{id}/edit

action_role

action_id    role_id
1            1
2            1

用户表包含站点上的所有用户,包括管理员和客户.

角色表包含??用户可以拥有的所有可能角色.例如管理员或客户.

role_user表是一个将角色与用户链接的数据透视表.

操作表列出了应用程序上可能的所有操作(即URL或路由).

action_role是一个数据透视表,用于将操作与角色相关联.

总结一下:

>用户有角色
>角色有行动
>用户可以拥有许多角色
>角色可以有很多动作

我想要一个中间件设置,如果用户有权查看当前页面,它会检查页面加载.为了做到这一点,我需要能够使用如下调用来访问用户操作:

$user->roles()->actions();

如何设置我雄辩的关系来支持这种呼叫?

UPDATE

我的关系在我的模型中设置如下:

用户

/**
 * The roles that belong to the user.
 *
 * @return Object
 */
public function roles()
{
    return $this->belongsToMany('AppModelsUserRole')->withTimestamps();
}

角色

/**
 * The actions that belong to the role.
 *
 * @return Object
 */
public function actions() 
{
    return $this->belongsToMany('AppModelsUserAction');
}

/**
 * The users that belong to the role.
 *
 * @return Object
 */
public function users()
{
    return $this->belongsToMany('AppModelsUserUser');
}

行动

/**
 * The roles that belong to the action.
 *
 * @return Object
 */
public function roles() 
{
    return $this->belongsToMany('AppModelsUserRole');
}

解决方法

Eloquent在两个数据透视表中没有HasManyThrough关系.

1.您可以通过懒惰的加载角色和操作来获取操作,然后提取它们:

$actions = $user->load('roles.actions')->roles->pluck('actions')->collapse()->unique();

2.您可以使用whereHas约束直接在数据库中检查操作:

$allowed = $user->roles()->whereHas('actions',function ($query) use ($action) {
    $query->where('name',$action);
})->exists();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读