Authorization

In addition to providing authentication feature out of the box, LaRecipe also provides a simple way to authorize user access against a given documentation.


Why?

We at BinaryTorch use LaRecipe internally to write documentation for our clients' projects and share the access with them. However, in some situations, we want to prevent a group of users access against specific resources for several reasons.

How?

LaRecipe provides an easy way to activate the authorization feature using Laravel Gate. Please have a look at the official documnetation if you're not familiar with Gates.

Register a new gate within boot method in your AuthServiceProvider.

use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerPolicies();

        Gate::define('viewLarecipe', function($user, $documentation) {
            return true;
        });
    }
}

Examples

1

You might check if the current user has admin role in order to access all docs.

Gate::define('viewLarecipe', function($user, $documentation) {
    return $user->isAdmin();
});
2

You may want to limit the access for small group of people listed manually.

Gate::define('viewLarecipe', function($user, $documentation) {
    return in_array($user->email, [
        'saleem@binary-torch.com',
        'admin@binary-torch.com',
    ]);
});
3

You may want to give access to all users except one person where his access limited to few pages only.

Gate::define('viewLarecipe', function($user, $documentation) {
    if($user->email == 'saleem@binary-torch.com')
    {
        if($documentation->title == 'Overview') {
            return true;
        }

        return false;
    }

    return true;
});
4

You may want to allow guest users to see some pages, authenticated users to see others, and admins (or any other groups you can think of) to see the rest. Note: you'll need to follow the steps explained in configuration settings to accomplish this.

Gate::define('viewLarecipe', function(?User $user, $documentation) {
    if($user && !$user->isAdmin()){
        //do some logic for regular authetnicated users
        if($documentation->title == 'Overview'){
            return true;
        }
        return false;
    }else if($user && $user->isAdmin()){
        //do some logic for admin users
        if($documentation->title == 'Admin Dashboard'){
            return true;
        }
        //or maybe just default to true
        return true;
    }else{
        //do some logic for guest users
        if($documentation->title == 'Features'){
            return true;
        }
        return false;
    }
});