Reputation: 2550
I need a role based access for my project and I have received reccomandations not to reinvent the wheel and use Sentinel. I've checked it out but I am a bit confused on how it works. The documentation really only covers how to use it but not how it works.
So, I understand the registration, users, throttling, roles and permissions. However I don't understand what a persistance, activation, checkpoint is.
How do I create permissions and attach them to roles? Inside the application code? Can I have a prrmissions table from which I retrieve them?
How to link permissions to resources? Individually in every route add a middleware?
What if I need multiple middleware for different routes?
I know I have a bunch of questions but anything would help now. My problem here is that I don't want to stary using Sentinel and realize it won't work properly with what I need to do and then start all from scratch.
Thanks
Upvotes: 2
Views: 2072
Reputation: 836
In my opinion, sentinel is a better option.
For example, you can seed your database with Sentinel:
Create roles
Example EXA Role
$role = \Sentinel::getRoleRepository()->createModel()->create([
'name' => 'Example',
'slug' => 'EXA',
]);
$role->permissions = [
'servicio_dash' => true,
'servicio_widget' => true,
];
$role->save();
User Role USR
$role = \Sentinel::getRoleRepository()->createModel()->create([
'name' => 'User',
'slug' => 'USR',
]);
$role->permissions = [
'servicio_dash' => true,
'servicio_widget' =>false,
];
$role->save();
Create 50users and asignate EXA role(Using faker)
$usr_role = \Sentinel::findRoleBySlug('EXA');
factory(App\User::class, 50)->make()->each(function ($u) use ($usr_role) {
\Sentinel::registerAndActivate($u['attributes']);
});
Bonus Track: Factory example
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'email' => $faker->safeEmail,
'password' => 'p4ssw0rd',
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'recycle' => false,
'phone' => $faker->phoneNumber,
'alt_email' => $faker->email
];
});
Only one user
$yo = factory(App\User::class)->make(['email' => '[email protected]']);
\Sentinel::registerAndActivate($yo['attributes']);
$jperez = User::where('email', '[email protected]')->firstOrFail();
$epa_role->users()->attach($jperez);
Authenticate Controller for API REST
public function authenticateCredentials(Request $request)
{
$credentials = $request->only('email', 'password');
$user = \Sentinel::authenticate($credentials);
return response()->json($user);
}
Authenticate with token (use JWT) and sentinel
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
Note: For this, you need configure JWT options with custom Auth provider, you can find this here
In any controller
public function hasPermission($type)
{
//$sentinel = \Sentinel::findById(\JWTAuth::parseToken()->authenticate()->id); //->this is for a token
$sentinel = \Sentinel::findById(1); //if you now the id
if($sentinel->hasAccess([$type]))
return response()->json(true, 200);
//yout custom handle for noAccess here
}
Upvotes: 2