Study Guide for Laravel 4 - Authentication

I looked around and found some Laravel 4 authentication manuals using Sentry or Confide and Entrust . Which are good, but a little vague for me, I'm starting a Laravel newbie, and this is my first base.

Does anyone know any tutorial or recommendations for implementing user authentication with user roles.

Here is what I am trying to do. - Internal website for work. Where writers can enter and post articles. - Admins can iterate over these articles. - These articles are not publicly available, so no one sees them. - Writers cannot see each other’s articles, but admins have access to everything.

I'm just looking for a tutorial on the role of users and how to implement them.

Edit

This is what I ended up doing.

After installing Sentry in the order specified by @ Antonio Carlos Ribeiro .

I had users, groups, and several other tables (I just needed to use users and groups).

Here is my seeder that I originally used to create users and groups. It can be made more efficient, but for those who just want to get started, it will 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 used the form to add new users, so in my controller I had something like this. Again, this is only for studying / testing the structure, the original implementation is very different. But for testing purposes this should work.

Assuming you have a form that obeys the function of the @ controller, you could 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); 

You can find rest in the documentation of Sentry: Sentry Docs

Feel free to edit this question to make it more informative or add new examples.

+6
source share
1 answer

Well, this is not quite an article about this, but it covers most of what we use for auth and roles in Sentry2. So basically you have to

Install composer by doing

 curl -sS https://getcomposer.org/installer | php 

Put it in the executable folder, renaming it

 sudo mv composer.phar /bin/composer 

Set the executable bit

 sudo chmod +x /bin/composer 

Install laravel by doing

 composer create-project laravel/laravel 

Install Sentry 2

 composer require cartalyst/sentry:2.0.* 

Then you just need 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 for a specific user, in this case he installs Managers for the current registered user

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

Check if the user can publish the added article

 if ( Sentry::getUser()->hasAnyAccess(['system','system.articles','system.articles.publish']) ) { // will be able to publish something } 

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

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

Get all groups from a specific 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.'; } 
+14
source

Source: https://habr.com/ru/post/947681/


All Articles