I looked around and found some tutorials about Laravel 4
authentication using Sentry
or Confide
and Entrust
. Which are good but a little vague for me, I am Laravel
beginner and this is my first framework.
Does anyone know of any tutorial or suggestions implementing user authentication with user roles.
Here is what I am trying to make. - Its an internal website for work. Where writers can sign in and submit articles. - Admins Can go over those articles. - These articles are not public so no one can view them. - Writers cannot see each others articles, but admins have access to everything.
I am just looking for tutorial that goes over user roles and how to implement them.
Edit
This is what I ended up doing.After Installing Sentry in the way specified by @Antonio Carlos Ribeiro.
I had Users,Groups and few other tables (I just had to use user and groups).
Here is my seeder that I initially used for creating users and groups. It can be made more efficient, but for anyone who wants to just get started this would work.
class SentrySeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
DB::table('groups')->delete();
DB::table('users_groups')->delete();
Sentry::getUserProvider()->create(array(
'email' => 'admin@admin.com',
'password' => "admin",
'first_name' => 'John',
'last_name' => 'McClane',
'activated' => 1,
));
Sentry::getUserProvider()->create(array(
'email' => 'user@user.com',
'password' => "user",
'first_name' => 'Saad',
'last_name' => 'Kabir',
'activated' => 1,
));
Sentry::getUserProvider()->create(array(
'email' => 'jack@user.com',
'password' => "user",
'first_name' => 'Jack',
'last_name' => 'Doe',
'activated' => 1,
));
Sentry::getUserProvider()->create(array(
'email' => 'jon@user.com',
'password' => "user",
'first_name' => 'Jon',
'last_name' => 'Doe',
'activated' => 1,
));
Sentry::getGroupProvider()->create(array(
'name' => 'Admin',
'permissions' => array('admin' => 1),
));
Sentry::getGroupProvider()->create(array(
'name' => 'Writer',
'permissions' => array('writer' => 1),
));
// Assign user permissions
$adminUser = Sentry::getUserProvider()->findByLogin('admin@admin.com');
$adminGroup = Sentry::getGroupProvider()->findByName('Admin');
$adminUser->addGroup($adminGroup);
$userUser = Sentry::getUserProvider()->findByLogin('user@user.com');
$userGroup = Sentry::getGroupProvider()->findByName('Writer');
$userUser->addGroup($userGroup);
$userUser = Sentry::getUserProvider()->findByLogin('jack@user.com');
$userGroup = Sentry::getGroupProvider()->findByName('Writer');
$userUser->addGroup($userGroup);
$userUser = Sentry::getUserProvider()->findByLogin('jon@user.com');
$userGroup = Sentry::getGroupProvider()->findByName('Writer');
$userUser->addGroup($userGroup);
}
}
After adding the initial users I was using a form to add new users, So in my controller I had something like this. Again this is just for learning/testing the framework, original implementation is very different. But for testing purposes this should work.
Assuming you have a form that submits to a controller@function, you can have something like this,
$user = Sentry::getUserProvider()->create(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'activated' => 1,
));
$writerGroup = Sentry::getGroupProvider()->findByName('writer');
$user->addGroup($writerGroup);
Rest you can find in Sentry documentation: Sentry Docs
Feel free to edit this question to make it more informative or add new examples.