ark*_*Dev 1 acl sentinel laravel-4
我是Cartalyst Sentinel的新手和ACL的概念.我设法创建了一个用户,执行激活,登录和注销.
我想把我的学习提升到一个新的水平.我想在这个laravel应用程序上有两种类型的用户.1是管理员另一个是订阅者.我假设我的帐户创建方法应该默认为用户创建一个订阅者.
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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
我这样改变代码吗?
$user = Sentinel::register($credentials);
$user = Sentinel::findById(1);
$role = Sentinel::findRoleByName('Subscribers');
$role->users()->attach($user);
Run Code Online (Sandbox Code Playgroud)
事情是我甚至没有创建任何角色开始.我们在哪里写这个功能?现在我有以下控制器
请指导我.任何帮助是极大的赞赏.
小智 5
如果已经注册了用户,则无需搜索用户,注册方法将返回用户.您可以执行以下操作将角色附加到用户:
$user = Sentinel::register($credentials);
$role = Sentinel::findRoleByName('Subscribers');
$role->users()->attach($user);
// OR
$user->roles()->attach($role);
Run Code Online (Sandbox Code Playgroud)
你有一个用户和一个角色对象,它们有很多关系,所以你使用哪一个并不重要.
您需要创建数据库浏览器或方法来创建权限.但要创建订阅者角色,您需要执行以下操作:
Sentinel::getRoleRepository()->createModel()->create([
'name' => 'Subscribers',
'slug' => 'subscribers',
'permissions' => [
'user.view' => true,
'user.delete' => false,
// any other permissions you want your Subscribers to have
]
]);
Run Code Online (Sandbox Code Playgroud)
类似的调用也可以构建您的管理员角色.
您的角色模型和控制器已经为您构建,您只需通过已经Sentinel::findRoleByName('Subscribers');
调用的Sentinel访问它们.
Cartalyst有一些关于为用户设置角色和权限的相当不错的文档:
这只是弄清楚你希望每个角色做什么或不做什么的问题.此外,您可以为每个用户设置特定权限以覆盖角色权限.
归档时间: |
|
查看次数: |
4592 次 |
最近记录: |