How can i make policy for user, Like, StudentPolicy and TeacherPolicy For User Model, Or Some other option.
Multiple Policy For User Model
I have different permissions like this
'view-admin'
'create-admin'
'edit-admin'
'delete-admin'
'view-student',
'create-student'
'edit-student'
'delete-student'
'view-teacher'
'create-teacher'
'edit-teacher'
'delete-teacher'
i register policy in my AuthServiceProvider
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Models\User' => 'App\Policies\Admin\AdminPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
}
}
in my AdminPolicy
<?php
namespace App\Policies\Admin;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class AdminPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any models.
*
* @param \App\User $user
* @return mixed
*/
public function viewAny(User $user)
{
if ($user->can('view-admin')) {
return true;
}
}
/**
* Determine whether the user can view the model.
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function view(User $user, User $model)
{
if ($user->can('view-admin')) {
return true;
}
}
/**
* Determine whether the user can create models.
*
* @param \App\User $user
* @return mixed
*/
public function create(User $user)
{
if ($user->can('create-admin')) {
return true;
}
}
/**
* Determine whether the user can update the model.
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function update(User $user, User $model)
{
if ($user->can('edit-admin')) {
return true;
}
}
/**
* Determine whether the user can delete the model.
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function delete(User $user, User $model)
{
if ($user->can('delete-admin')) {
return true;
}
}
/**
* Determine whether the user can restore the model.
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function restore(User $user, User $model)
{
//
}
/**
* Determine whether the user can permanently delete the model.
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function forceDelete(User $user, User $model)
{
//
}
}
i have role admin,student,teacher. i want to make policy for student and teacher, How can i achieve this.
@vinayprajapati No, because you’re approaching polices the wrong way. You create policies for resources, not a user class. The policy checks if the authenticated user can perform the requested action of the target resource, so it’s in these policy methods that you would check permissions.
Take an article. Imagine an admin user can update any article, and the author can update only their own articles. The corresponding article policy method may look like this:
class ArticlePolicy
{
public function update(User $user, Article $article)
{
// Admins can update any article
if ($user->isAdmin()) {
return true;
}
// Authors can update their own articles only
if ($user->is($article->author)) {
return true;
}
// If neither checks pass, user cannot update article
return false;
}
}
You can then use this policy in your controller method:
class ArticleController extends Controller
{
public function update(UpdateArticleRequest $request, Article $article)
{
// Will call ArticlePolicy::update
$this->authorize('update', Article::class);
$article->update($request->validated());
// Return response
}
}
Please or to participate in this conversation.