Laravel使用zizaco信任和委托添加用户和角色


Laravel add user and role using zizaco confide and entrust

嗨,我

正在使用Laravel,我正在使用几个用于用户身份验证和角色的软件包,它们是Zizaco Confide和Zizaco 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:(添加&

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