【发布时间】:2015-12-09 16:18:04
【问题描述】:
我正在使用 Laravel 5.1 和 users、roles 和 actions 创建一个应用程序。
表格设置如下:
用户
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
user 表包含站点上的所有用户,包括管理员和客户。
role 表包含用户可以拥有的所有可能角色。例如 Administrator 或 Customer。
role_user 表是一个将role 链接到user 的数据透视表。
action 表列出了应用程序上所有可能的操作(即 URL 或路由)。
action_role 是一个将action 链接到role 的数据透视表。
总结一下:
- 用户具有角色
- 角色有动作
- 一个用户可以有多个角色
- 一个角色可以有很多动作
我想要一个中间件设置,如果用户有权查看当前页面,它会检查页面加载。为此,我需要能够使用如下调用访问用户操作:
$user->roles()->actions();
我如何建立我的雄辩关系来支持这种呼叫?
更新
我的关系在我的模型中是这样设置的:
用户
/**
* The roles that belong to the user.
*
* @return Object
*/
public function roles()
{
return $this->belongsToMany('App\Models\User\Role')->withTimestamps();
}
角色
/**
* The actions that belong to the role.
*
* @return Object
*/
public function actions()
{
return $this->belongsToMany('App\Models\User\Action');
}
/**
* The users that belong to the role.
*
* @return Object
*/
public function users()
{
return $this->belongsToMany('App\Models\User\User');
}
动作
/**
* The roles that belong to the action.
*
* @return Object
*/
public function roles()
{
return $this->belongsToMany('App\Models\User\Role');
}
【问题讨论】:
-
除了已经提供的代码之外,您还想实现什么?
-
我的问题在帖子的底部。我基本上只是想检查用户是否有权查看当前页面。通过上述表格设置实现这一目标的最佳方法是什么?
-
Laravel 5 还是 laravel 5.1?