O
O
ortsuev332019-11-10 10:39:39
Laravel
ortsuev33, 2019-11-10 10:39:39

What is the best way to implement such a restriction?

I have user rights admin and user.
There is a link to the article editor, with the help of php I would make a stupid condition for checking if the line in the database matches the line admin, is there something ready in laravel, I just read about guard (or is it access to the page) I didn’t really understand anything

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-11-11
@ortsuev33

You need policies for the article model to make a policy to prescribe access and restrict using can in templates. In docks detailed examples for different situations. Or just make a check from the user. As I understand it, you have no roles and you just made the admin field in users. Then in the User model do a type check

public function isAdmin()
{
  return $this->admin === 1;
}

and in templates
@if (Auth::user()->isAdmin())
  показать кнопку
@endif

but better politicians because clearer and easier to refactor. Even with one role. For example, you want to disable the creation of articles in the configs. With policies, you add this config in the create method in ArticlePolicy and your logic works throughout the application and you don’t need to duplicate code everywhere if something changes. I advise you to spend time understanding than to write your bicycles later. Otherwise what's the point of using Laravel?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question