繁体   English   中英

Laravel使用Zizaco Confide and Entrust添加用户和角色

[英]Laravel add user and role using zizaco confide and entrust

嗨,我正在使用Laravel,我正在使用几个用于用户身份验证和角色的软件包,分别是Zizaco ConfideZizaco Entrust 我已经完成设置过程,并且拥有角色和权限。 这样就创建了下表:

用户,角色,assigned_roles,权限,permission_role

表的结构如下,有两个用户:

users table
id | username    | email 
1  | adminuser   | admin@test.com
2  | subscriber  | subscriber@test.com

roles
id | name 
1  | admin
2  | subscriber

assigned_roles
id | user_id | role_id
1  | 1       | 1
2  | 1       | 2
3  | 2       | 2

permissions
id | name              | display_name
1  | can_view_admin    | Can View Admin Display
2  | can_view_articles | Can View Articles

permission_role
id | permission_id | role_id 
1  | 1             | 1
2  | 1             | 2
3  | 2             | 2

我正在使用由confide补充的标准控制器来添加用户,但是我向控制器添加了几行并注册了表单(我正在使用的自定义视图,发布了配置文件并在其中进行了编辑),如下所示:

public function create()
    {

        $roles =  DB::table('roles')->orderBy('name', 'asc')->lists('name', 'id'); 
        return View::make(Config::get('confide::signup_form'),['roles' => $roles ]);
    }

这将返回数据库中的roles ,以便我可以将它们传递给多选对象,因为用户可以有多个角色。 在我看来,我会执行以下操作:

<form method="POST" action="{{{ URL::to('users') }}}" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{{ Session::getToken() }}}">
<input class="form-control" placeholder="{{{ Lang::get('confide::confide.username') }}}" type="text" name="username" id="username" value="{{{ Input::old('username') }}}">

@if(count($roles)>0)
        {{ Form::label('select_client', 'Select Role', array('class' => 'label'));  }}
        {{ Form::select('role', $roles , Input::old('role'), array('class' => 'select', 'multiple')) }}
@endif 

现在,此表单将转至confide软件包随附的标准控制器,我不介意,但这是函数:

public function store()
    {
        $repo = App::make('UserRepository');

        $user = $repo->signup(Input::all());

        if ($user->id) {
            if (Config::get('confide::signup_email')) {
                Mail::queueOn(
                    Config::get('confide::email_queue'),
                    Config::get('confide::email_account_confirmation'),
                    compact('user'),
                    function ($message) use ($user) {
                        $message
                            ->to($user->email, $user->username)
                            ->subject(Lang::get('confide::confide.email.account_confirmation.subject'));
                    }
                );
            }

            return Redirect::action('UsersController@login')
                ->with('notice', Lang::get('confide::confide.alerts.account_created'));
        } else {
            $error = $user->errors()->all(':message');

            return Redirect::action('UsersController@create')
                ->withInput(Input::except('password'))
                ->with('error', $error);
        }
    }

如果注册成功并使用刚创建的用户ID和选择了多个角色的角色ID,该如何成功更新assigned_roles表? 我需要在这里进行foreach循环吗? 任何帮助将不胜感激!

进入UserRepository类,并将此代码添加到signup()方法中:

$this->save($user);

$role = array_get($input, 'role');
$user->roles()->attach($role);

现在仍然存在问题,因为使用$this->save()$user在插入后不会获得ID。 您可以直接使用$user->save()进行操作:

$user->save();
$roles = array_get($input, 'role');
$user->attachRoles($roles);

或修改存储库中的save方法,以通过引用传递$user :(add &

public function save(User &$instance){
    return $instance->save()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM