Search code examples
laravellaravel-4cartalyst-sentry

Laravel 4, Sentry 2 and user permissions


I am about to begin building a system that will require a set questions to be selected and saved as a checklist document for distribution to an arbitrary set of users.

I am intending to use Laravel 4 as my framework and Sentry 2 to handle permissions.

My question is:

If I use a user with, lets say, Admin level access to create a check list and I have, say 100, Inspector level users, can I restrict access to a single checklists for every individual users using Sentry? I ideally want to allow a user to have access to a single checklist (collection of checks).


Solution

  • You can easily create separate groups for your checklists:

    // Create the group
    $group = Sentry::createGroup(array(
        'name'        => 'Administrators',
        'permissions' => array(
            'checklists.admin' => 1,
            'checklists.view' => 1,
        ),
    ));
    
    // Create the group
    $group = Sentry::createGroup(array(
        'name'        => 'checklists.view.101',
        'permissions' => array(
            'checklists.view.101' => 1,
        ),
    ));
    

    Add your user to a checklist group:

    $user = Sentry::getUser()->addGroup( Sentry::getGroupProvider()->findByName('Checklist101') );
    

    And check if it has access to it:

    return $user->hasAnyAccess(['checklists.view', 'checklists.view.101'])
    

    In this case, if your user is administrator, it will also be able to view it.