1

Im new to Cartalyst Sentinel and this concept of ACL. I've managed to create a user, perform activation and login and logout.

I'd like to take my learning to the next level. I would like 2 types of Users on this laravel app. 1 is Administrator another is Subscriber. I'm assuming my account creation method should by default create the user a subscriber.

public function postCreate() {

        /* Validation */
        $validation = Validator::make(Input::all(), [
            'email' => 'required|email|max:50|unique:users',
            'username' => 'required|min:3|max:20|unique:users',
            'password' => 'required|min:6',
            'password_repeat' => 'required|same:password',
        ]);

        if ($validation->fails()) {

            return Redirect('login')->withErrors($validation)->withInput();

        } else {

            $credentials = Input::all();
            $user = Sentinel::register($credentials);

            $activation = Activation::create($user);

            $activation_code = $activation->code;

            if ($user) {

                Mail::send('emails.auth.activate', ['link' => URL::route('account-activate', [$user->id, $activation_code]), 'username' => $user->username], function($message) use ($user) {

                    $message->to($user->email, $user->username)->subject('Activate your account');

                });

                return Redirect::route('home')->with('global', 'Thank you for registering! We have sent you an email to activate your account');

            }

        }       

    }

Do i alter the code like so

$user = Sentinel::register($credentials);

$user = Sentinel::findById(1);

$role = Sentinel::findRoleByName('Subscribers');

$role->users()->attach($user);

The thing is i have not even created any roles to begin with. Where do we write that functionality? Right now i have the following Controllers

  1. AccountController - handles activation
  2. AuthController - handles login/logout
  3. RegistrationController - handles registration of user
  4. RolesController - i've not written anything inside here yet. Im a bit lost.

Please guide me. Any help is greatly appreciated.

jrenk
  • 1,387
  • 3
  • 24
  • 46
arkhamDev
  • 1,028
  • 1
  • 15
  • 32

1 Answers1

5

You do not need to do a search for your user if you already registered them, the register method returns the user. You can do the following to attach a role to a user:

$user = Sentinel::register($credentials);
$role = Sentinel::findRoleByName('Subscribers');
$role->users()->attach($user);
// OR
$user->roles()->attach($role); 

you have both a user and a role object and they have a many to many relation so it doesn't matter which one you use.

You will need to create a db seeder or a method to create your permissions. But to create your Subscribers Role you will need to do the following:

Sentinel::getRoleRepository()->createModel()->create([
    'name' => 'Subscribers',
    'slug' => 'subscribers',
    'permissions' => [
       'user.view'   => true,
       'user.delete' => false,
       // any other permissions you want your Subscribers to have
    ]
]);

A similar call can build your Administrator roles as well.

Your Roles Model and Controller are already built for you, you just need to access them through Sentinel, which you already have Sentinel::findRoleByName('Subscribers'); call.

Cartalyst has some pretty decent documentation about setting up roles and permissions for your users:

It's just a matter of figuring out what you want each role to do or not do. Also, you can set specific permissions per user to override the role permissions.

codivist
  • 505
  • 1
  • 5
  • 14
  • I have a question, what is `user` in `user.view` or `user.delete` stand for, is it model name or what,.? – Praditha Apr 18 '16 at 10:19
  • 1
    Those refer to the actual permission names that you reference when checking a permission, so a permission name `user.view => true` just means later in your code you would call `if ($user->hasAccess(['user.view']))` and that says, does this current user have access to view the called user. – codivist Apr 20 '16 at 20:37
  • Ya, I got it know, that only the naming convention for later to check the permission. thanks @codivist. – Praditha Apr 21 '16 at 04:18
  • @codivist : I see question is about "how do you create roles in Sentinel?" But I don't find it in your answer. yeah its in link of-course. I am stuck with update & delete role can you guide me how to do that ? – Vishal Tarkar Mar 01 '17 at 11:46