How about a model called PostAssignment that contains post_id, user_id, and permission_id?
It can be checked to see if the user_id for a given post_id has the permission_id?
$post->givePermissionTo('edit_post', $user_id);
// Post.php
public function giveUserPermissionTo($permission, $user) {
$this->post->assignments()->create([
'permission_id' => $permission->id,
'user_id' => $user->id
]);
return $this;
}
public function userHasPermission($permission, $user) {
return Post::whereHas('post_assignments', function($query) use ($permission, $user) {
$query->where([
['user_id' => $user->id],
['permission_id' => $permission->id],
])->get()
->isNotEmpty();
});
}
Edit:
I don't like 3-way pivots but.... I suppose we could restructure the tables. Hrm. Need to sleep on it.
Edit2:
Okay, I'll sleep in a bit. You can probably has a hasMany to a post_permission table and a hasManyThrough it to a post_assignment table to avoid a 3-way pivot.
post_permission = id, post_id, permission_id
post_assignment = id, post_permissions_id, user_id
Chain through it to all permitted editors and see if the returned collection contains the user id?