Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnw65's avatar

Application Level Security

I'm relatively new to Laravel, and I have a following question.

Currently, different users have access to different applications. I know it's a relatively general question, but what is the best method to make sure that an user can only access their authorized application. I have seen codes where in the constructor method of a controller, they check to see whether an user has authorized access, but not sure whether that is the best approach.

I do have an user roles table which lists all of the applications that an user have access to and also whether it's read or read/write access.

Thanks in advance.

0 likes
6 replies
johnw65's avatar

CorvS: Thanks for the information. Makes sense!

martinbean's avatar

@johnw65 You should use policies to determine whether users can view particular resources.

johnw65's avatar

Martin,

Thanks, so instead of using middleware, I need to use authorization via policies.

So I can grab a list of application that an user has along with whether its' read access or write access from a user_role table.

So how can I create a policy to perform 3 functions?

  1. Access/Deny a particular application ( I will have many applications). If they accidently enter, route the user back to the main menu.
  2. If user has read/write access, enable update button (for example). For read access, disable update button.
  3. If possible, perform this once so I don't have to query the database each time.

Thanks!

martinbean's avatar
Level 80

@johnw65 Policies have methods to determine whether a user can view, update, delete, etc a particular model: https://laravel.com/docs/8.x/authorization#writing-policies

So you can use a policy to first check if a user can actually view an application based on their role/assigned applications, and then use that policy in your Blade views to conditionally show things like edit buttons:

@can('update', $application)
    <a href="{{ route('application.edit', compact('application')) }}">
        {{ __('Edit') }}
    </a>
@endcan
johnw65's avatar

Martin, Thanks! Let me read the documentation regarding policies.

Please or to participate in this conversation.