laravel 4.2使用auth和Entrust的用户角色

时间:2015-09-29 15:07:25

标签: laravel authentication laravel-4 role user-roles

我希望制作两个界面

  • 管理员(管理员角色)
  • 订阅者(订阅者角色)

我现在用于授权和委托(Github Link)Laravel 4

录制用户

在我的SubscriptionContoller.php中我做到了:

public function doRegister(){

    $password=Input::get('password');

    $data = array(
    'email'           => Input::get('email'),
    'password'        => Hash::make($password)
    ); 

    $rules = array(
        'email'           => 'required|email',
        'password'        => 'required'
    );

    $messages = array(
        'required' => 'The :attribute field is required.',
    );

    $validator = Validator::make($data, $rules, $messages);        

    if ($validator->fails()) {

        Session::flash('message', 'This email is already registered, please choose another one.');

        return Redirect::to('subscription/register')
            ->withErrors($validator);

    } else {

        $user = new User;

        $role = Role::where('name','=','agency')->first();

        $user->email = Input::get('email');
        $user->password = Hash::make($password); 
        $user->save();

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

        Session::flash('message', 'Successfully created account! Please login to submit your ad.');
        return Redirect::to('subscription/dashbord');
    }
}

我应该如何编写routes.php和filters.php ??

1 个答案:

答案 0 :(得分:0)

只需将此行放在 routes.php

之上
Entrust::routeNeedsRole( 'subscription*', 'agency' );

更多详情:

这只允许具有代理角色的用户访问订阅/ * 网址。如果用户没有该角色,则默认情况下会执行App::abort(403);,这将在 views / public / <中查找 403.blade.php / strong>文件夹。

如果你想做一些特定的事情,如果用户没有匹配该角色,你可以传递第三个参数如下:

Entrust::routeNeedsRole( 'subscription*', 'agency', Redirect::to('/not-authorized'));