简体   繁体   English

Laravel 5根据用户角色选择行的范围

[英]Laravel 5 scope for selecting rows based on user role

I have a timesheet application which I have written in Laravel 5. I now want to select timesheets based on the user permissions (role). 我有一个用Laravel 5编写的时间表应用程序。我现在想根据用户权限(角色)选择时间表。 I have 3 levels of user. 我有3个级别的用户。

  • User 用户
  • Admin 管理员
  • Supervisor 主管

A Supervisor supersedes a User . 主管取代用户 Admin supersedes a Supervisor . 管理员取代主管

The admin user should be able to see ALL user timesheets. 管理员用户应该能够查看所有用户时间表。 The supervisor should only be able to see timesheets of users associated with them. 主管只能查看与他们关联的用户的时间表。 Users will only be abl to see their own timesheets. 用户将只能看到自己的时间表。

I have the following method setup to select timesheets awaiting approval. 我有以下方法设置来选择等待批准的时间表。 This method only works for the User level at the moment as I have added a whereUserId clause. 由于我添加了whereUserId子句,此方法目前仅适用于用户级别。

How can I make the following method work for all roles of user (admin, user, supervisor)? 如何使以下方法适用于用户的所有角色(管理员,用户,主管)?

/**
 * Load timesheets awaiting approval.
 *
 */
public function timesheetsAwaitingApproval() 
{
    return $this->timesheet->whereUserId($this->userid)->whereStatus('Submitted')->get();
}

Try using the hasManyThrough relationship between your models to get the timesheets you need, check the doc . 尝试使用模型之间的hasManyThrough关系来获取所需的时间表,请检查doc Of course, this will only work if you 当然,这只有在您

Supervisor example 主管示例

public function timesheets()
{
    return $this->hasManyThrough('Timesheet::class', 'User::class', 'timesheet_id', 'user_id');
}

public function timesheetsAwaitingApproval() 
{
    return $this->timesheets->whereStatus('Submitted')->get();
}

Or something like that, thats just an idea that might work for you. 或类似的东西,那只是一个可能对您有用的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM