我是一个使用Laravel5.1的完全初学者。我做了3到4年的PHP开发人员,我一直在使用Java,我刚刚回到PHP环境,找到了一个完整的框架列表。
经过一些研究,并利用一些调查结果,我发现拉拉维尔是最终的结果。现在,我成功地使用了拉文森来安装它,并运行了我的第一个新应用程序。我稍微了解了一条路线是如何工作的,这没关系。
现在,我需要使用Sentinel2.0,以便将正确的角色/auth应用到我的应用程序中,然后添加社会化部分。
因此,要做到这一点,我需要了解的东西很少:
谢谢
发布于 2016-08-03 10:18:10
可以,停那儿吧。例如,这是我使用JWT和Sentinel编写的API rest代码。您可以使用哨兵为您的数据库添加种子:
创建角色
示例EXA角色
$role = \Sentinel::getRoleRepository()->createModel()->create([
'name' => 'Example',
'slug' => 'EXA',
]);
$role->permissions = [
'servicio_dash' => true,
'servicio_widget' => true,
];
$role->save();用户角色USR
$role = \Sentinel::getRoleRepository()->createModel()->create([
'name' => 'User',
'slug' => 'USR',
]);
$role->permissions = [
'servicio_dash' => true,
'servicio_widget' =>false,
];
$role->save();创建50个用户并标识EXA角色(使用faker)
$usr_role = \Sentinel::findRoleBySlug('EXA');
factory(App\User::class, 50)->make()->each(function ($u) use ($usr_role) {
\Sentinel::registerAndActivate($u['attributes']);
});奖金跟踪:工厂示例
$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
];});
只有一个用户
$yo = factory(App\User::class)->make(['email' => 'jpaniorte@openmailbox.org']);
\Sentinel::registerAndActivate($yo['attributes']);
$jperez = User::where('email', 'jpaniorte@openmailbox.org')->firstOrFail();
$epa_role->users()->attach($jperez);用于API 的认证控制器
public function authenticateCredentials(Request $request)
{
$credentials = $request->only('email', 'password');
$user = \Sentinel::authenticate($credentials);
return response()->json($user);
}使用令牌(使用JWT)和哨兵进行身份验证
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'));
}注意:为此,您需要使用自定义Auth提供程序配置JWT选项,您可以找到这个https://gist.github.com/iolson/8a4c6d689a334f6de48e
任意控制器中的
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
}https://stackoverflow.com/questions/33181353
复制相似问题