What have you actually tried?
How to use roles and permissions through gates
i am creating a new laravel project and i want to implement the concept of roles and permissions without using plugin and i trying to use gates but can't figure out how to use gates
@tykus this is what i use
if (Auth::attempt($userData)){
if (Auth::user() && Auth::user()->is_admin == 1){
return redirect('admin');
}else {
return redirect('user');
}
}else {
return response("failed!");
}
and for create and edit view i also do that but i want to use gates for this how can i do that?
@laksh and - where is the Gate in your code?
Check the docs:
writing gates: https://laravel.com/docs/9.x/authorization#writing-gates
authorizing via Gates: https://laravel.com/docs/9.x/authorization#authorizing-actions-via-gates
@SilenceBringer i dont know yet how to use the concept of gates
@laksh follow the steps in the docs:
- Write Gate
- Register gate
- Use Gate
@SilenceBringer if i want that non-admin can't create a new user so how can i write the gate for that?
@laksh like described in docs
Gate::define('create-user', function (User $user) {
return $user->is_admin;
});
and use it like
if (! Gate::allows('create-user')) {
abort(403);
}
@SilenceBringer But when i trying to define the gate like in docs. it automatically go like that Gate::define('create-user', function(User::class)) what should i do for this?
@jaseofspades88 umm no i tried those documentation links and after trying i got that error which i showing
I created a policy belong to a model class contacts and i want that admin can't able to create a contact and i use this code for that
public function create(User $user) { return $user->is_admin = 0; }
it gives me error of 403 on both admin and non-admin users how can i resolve that?
Please or to participate in this conversation.