How to: implement sentry 2 permissions with Laravel 4?

前端 未结 2 829
误落风尘
误落风尘 2020-12-24 07:16

I\'m trying to use cartalyst sentry 2 in my site being built with Laravel 4. Basically I don\'t understand how to implement permissions.

The examples I\'ve seen for

2条回答
  •  梦毁少年i
    2020-12-24 07:42

    Basically you have to..

    Create your groups

    Sentry::getGroupProvider()->create([
        'name' => 'Super Administrators',
        'permissions' => [
            'system' => 1,
        ],
    ]);
    
    Sentry::getGroupProvider()->create([
        'name' => 'Managers',
        'permissions' => [
            'system.products' => 1,
            'system.store' => 1,
            'system.profile' => 1,
        ],
    ]);
    

    Set a group to a particular user, in this case it is setting Managers to the current logged user

    Sentry::getUser()->addGroup( Sentry::getGroupProvider()->findByName('Managers') );
    

    Check if a user has a particular access

    if ( Sentry::getUser()->hasAnyAccess(['system','system.products']) )
    {
        // Will be able to do a thing
    }
    

    Check if a user is Super Administrator (only this group has the 'system' access)

    if ( Sentry::getUser()->hasAnyAccess(['system']) )
    {
        // Will be able to do a thing
    }
    

    Get all groups from a particular user

    try
    {
        // Find the user using the user id
        $user = Sentry::getUserProvider()->findById(1);
    
        // Get the user groups
        $groups = $user->getGroups();
    }
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
    {
        echo 'User was not found.';
    }
    

提交回复
热议问题