Laravel 5 Eloquent ORM - 多对多到多对多

Laravel 5 Eloquent ORM - Many to Many through Many to Many

我正在开发一个相当复杂的用户系统,该系统需要能够向个人或整个组授予访问权限。例如,您可以授予 Jane Doe 管理留言板的权限,或者授予通讯组的所有用户发送电子邮件、管理留言板和 post 事件的权限(分配给该组的所有用户都被授予访问这些权限的权限)添加时的特权)。

我不想严格跟踪每个用户的个人权限,而是想首先根据用户是否属于包含该权限的组来授予对功能的访问权限。如果他们不这样做,那么我想检查他们是否被授予个人访问权限(privilege_user pivot table)。

基本上,我想要这样的东西:

// 1 = Message Board Moderator
// See if any of the user's groups contain this privilege -
$hasAccess = Auth::user()->groups->privileges->contains(1); 

这是我的 table 结构:

用户

| Field          | Type             | Null | Key | Default             | Extra          |
+----------------+------------------+------+-----+---------------------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username       | varchar(80)      | NO   |     | NULL                |                |
| email          | varchar(120)     | NO   |     | NULL                |                |
| first_name     | varchar(20)      | NO   | MUL | NULL                |                |
| last_name      | varchar(45)      | NO   | MUL | NULL                |                |
| password       | varchar(140)     | NO   |     | NULL                |                |
| active         | tinyint(1)       | NO   |     | NULL                |                |
| remember_token | varchar(100)     | NO   |     | NULL                |                |
| last_login     | timestamp        | YES  |     | NULL                |                |
| last_login_ip  | varchar(45)      | YES  |     | NULL                |                |                
| updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| created_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| deleted_at     | timestamp        | YES  |     | NULL                |                |
+----------------+------------------+------+-----+---------------------+----------------+

群组 (table: user_groups)

+-------------+------------------+------+-----+---------------------+----------------+
| Field       | Type             | Null | Key | Default             | Extra          |
+-------------+------------------+------+-----+---------------------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| name        | varchar(255)     | NO   |     | NULL                |                |
| slug        | varchar(255)     | NO   |     | NULL                |                |
| description | text             | NO   |     | NULL                |                |
| created_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+------------------+------+-----+---------------------+----------------+

用户组枢轴(table:user_user_group)

+---------------+------------------+------+-----+---------+-------+
| Field         | Type             | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| user_id       | int(10) unsigned | NO   | MUL | NULL    |       |
| user_group_id | int(10) unsigned | NO   | MUL | NULL    |       |
+---------------+------------------+------+-----+---------+-------+

特权(table:特权)

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255)     | NO   |     | NULL    |                |
| slug  | varchar(255)     | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

特权用户组枢轴(table:privilege_user_group)

+---------------+------------------+------+-----+---------+-------+
| Field         | Type             | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| privilege_id  | int(10) unsigned | NO   | MUL | NULL    |       |
| user_group_id | int(10) unsigned | NO   | MUL | NULL    |       |
+---------------+------------------+------+-----+---------+-------+

很失望没有人在这里插话,但幸运的是我能够在 Laracasts.com 找到一些很大的帮助。

我选择了 Zizaco/entrust,这正是我所描述的。唯一缺少的部分是在没有将用户分配到组的情况下回答问题 "does this User have a Permission",但这很容易用 Eloquent 完成。委托所做的就是回答问题 "Does this User have access to this Permission through a Group?" 而这正是我所追求的。