RafaelMunoznl
1 month ago
103
1
Laravel

Policies returning to different views instead trowing a 403

Posted 1 month ago by RafaelMunoznl

Hi there,

I have implemented several policies to filter different actions. For example:

  • User deleted himself its profile
  • User was suspended
  • User still didn't confirm his email address
  • User has not been reactivated for the admin after a suspension

In all those cases the User is not allowed to visit the profile page, but for different reasons.

But policies just throw the same message: This action is unauthorized. which is not very user friendly.

So my desire is to redirect the user to a different page in each case giving a differnt reason why he can't visit the page.

So I did this in a UserDeletedPolicy file:

    public function view(User $user, Profile $profile)
    {
        if ($profile->status === 'deleted') {
             $message = "Profile deleted. Please contact the admin if you think it is an error";
            return view('errors.profile_deleted', compact('user', 'profile', 'message'));
        } 
        else if {
           $message = "You have to confirm your email."
            return view('errors.confirm_email', compact('user', 'profile', 'message'));
       } else {
     // hier the case of suspension 
       }
    }

but it does not work.

Any idea how could I achieve sometihng like this?

Please sign in or create an account to participate in this conversation.