outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
273 stars 97 forks source link

Authorisation in provider boot method #72

Closed chriswest101 closed 3 years ago

chriswest101 commented 3 years ago

How can you authorise the user per a setting in the provider boot method when Laravel hasn't loaded the user or session?

Tarpsvo commented 3 years ago

Heya!

If you mean per field, the fields use the canSee callbacks for authorization.

See the docs here: https://nova.laravel.com/docs/3.0/resources/authorization.html#fields

Example:

Text::make('Name')
  ->canSee(function ($request) {
    return $request->user()->can('viewProfile', $this);
  }),

This runs when everything has already booted and you have access to the session and user.

If you want to hide the whole Settings items, you can call ->canSee on the tool in NovaServiceProvider@tools, like so:

(new \OptimistDigital\NovaSettings\NovaSettings())
  ->canSee(function($request) {
    return $request->user()->isAdmin();
  }),

Hope this helps. Good luck!