Well, this is not exactly an article about it, but it covers most of what we use on auth and roles in Sentry2. So, basically you have to
Install composer by executing
curl -sS https://getcomposer.org/installer | php
Put it on a executable folder, renaming it
sudo mv composer.phar /bin/composer
Set the executable bit
sudo chmod +x /bin/composer
Install laravel by executing
composer create-project laravel/laravel
Install Sentry 2
composer require cartalyst/sentry:2.0.*
Then you just have to use Sentry:
Create your user groups and permissions for each group:
Sentry::getGroupProvider()->create(array(
'name' => 'Super Administrators',
'permissions' => array(
'system' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'name' => 'Managers',
'permissions' => array(
'system.articles' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'name' => 'Publishers',
'permissions' => array(
'system.articles.add' => 1,
'system.articles.edit' => 1,
'system.articles.delete' => 1,
'system.articles.publish' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'name' => 'Authors',
'permissions' => array(
'system.articles.add' => 1,
'system.articles.edit' => 1,
'system.articles.delete' => 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('Author') );
Check if a user can publish and an added article
if ( Sentry::getUser()->hasAnyAccess(['system','system.articles','system.articles.publish']) )
{
// will be able to publish something
}
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.';
}