php - Correct data structure Laravel -
i have structure example:
in example, can see possible bind multiple users, 1 book or more.
problem expierincing follows:
there's 1 main owner of book. users bind afterwards need have permissions, edit, delete etcetera. correct way of dealing in structure providing? need store permissions in pivot table user_book, or have seperate table?
here abstract idea:
the book table may contain field owner owner_id (or user_id) in case can create one-to-one relationship book , it's owner. method relationship in book model might like:
public function owner() { // book belongs 1 owner (user) return $this->belongsto('app\user', 'owner_id', 'id'); } so, $book->owner return owner/user owns book. owner_id needs set when creating book. reverse relation one-to-many in user model need create books method example:
public function books() { // user has many books return $this->hasmany('app\book', 'owner_id', 'id'); } so, user model can call:
$books = user::find(1)->books; // user::with('books')->find(1); so far owner , book relationship has been declared. users permissions may need create many-to-many relationship between user , book model using pivot table (your current structure), example:
users -> book_user <- books in both models ('user' , 'book') need create method this:
// in book model public function permittedusers() // $book->permittedusers { return $this->belongstomany( 'app\user', 'book_user', 'book_id', 'user_id' )->withpivot('permission'); } // in user model (books used owner) public function permittedbooks() // $user->permittedbooks { return $this->belongstomany( 'app\book', 'book_user', 'user_id', 'book_id' )->withpivot('permission'); } your book_user table may this:
id | book_id | user_id | permission now, when bind users books, save access level in permission field using unix system's permission algorithm (similar not same). this, @ first, create config file in config folder using unique file name, may use book.php , declare/return following array:
// config/book.php return [ 'editable' => 1, 'deletable' => 2, 'all' => 3 ]; the algorithm:
if permission value == 1 user can edit book if permission value == 2 user can delete book if permission value == 3 user can both (1+2=3) so, when bind users <-> books saving book_id & user_id in book_user pivot table, save permission level in permission field. achieve this, may use check boxes , set value of check boxes permission's value, example;
<input name="permission[]" type="checkbox" value=1 /> edit <input name="permission[]" type="checkbox" value=2 /> delete when submitting form, check if permission set , sum/addition values like:
// if(isset('permission')) if($permission = request::get('permission')) { $permissionlevel = array_sum($permission); } // otherwise invalid data, @ least 1 permission must given save pivot data including $permissionlevel in pivot table. so, when need check book's permissions user, may check if permission field matches value in book_permission table. result can check using this:
if($user->permittedbooks->first()->pivot->permission == config('book.edit')) { // can edit book } if($user->permittedbooks->first()->pivot->permission == config('book.all')) { // can } that's , it's idea. there other ways. may check this article idea, system wise acl helpfull. may check unix permissions calculator.

Comments
Post a Comment