简体   繁体   English

Laravel 5.2:在此示例中如何保存用户角色?

[英]Laravel 5.2 : How to save user's role in this example?

I am using Laravel 5.2 and Zizaco/entrust ,my question is: 我正在使用Laravel 5.2和Zizaco/entrust ,我的问题是:
In the following example,how to save user's role? 在以下示例中,如何保存用户角色?

**register form ** which was added a Role item on the basis of the out-of-the-box form. **注册表格**,它是根据现成的表格添加的Role项目。

<form id="register" class="form-horizontal" role="form" method="POST"  action="{{ url('/register') }}">
    {!! csrf_field() !!}
    <fieldset class="form-group">
        <label class="col-md-4 form-control-label">Role</label>
        <div class="col-md-6">
            <label class="c-input c-radio">
                <input id="role1" name="role" type="radio" value="1">
                <span class="c-indicator"></span>
                red team
            </label>
            <label class="c-input c-radio">
                <input id="role2" name="role" type="radio" value="2">
                <span class="c-indicator"></span>
                blue team
            </label>
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">username</label>
        <div class="col-md-6">
            <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
            @if ($errors->has('name'))
            <span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
            @endif
        </div>
    </fieldset>


    <fieldset class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">email</label>
        <div class="col-md-6">
            <input type="email" class="form-control" name="email" value="{{ old('email') }}">
            @if ($errors->has('email'))
            <span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
            @endif
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password</label>
        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password">
            @if ($errors->has('password'))
            <span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group{{ $errors->has('password_confirmation') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password confirmation</label>
        <div class="col-md-6">
            <input type="password" class="form-control" name="password_confirmation">
            @if ($errors->has('password_confirmation'))
            <span class="help-block"><strong>{{ $errors->first('password_confirmation') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary">submit</button>
        </div>
    </fieldset>
</form>

AuthController out-of-the box. 开箱即用的AuthController

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

I can make a RolesController and write a attachRole() function,Thus,I can manually attach a role to a user. 我可以创建一个RolesController并编写一个attachRole()函数,因此,我可以手动将角色附加到用户。 like this: 像这样:

public function attachRole()
{
    $user = User::where('name', '=', 'foo')->first();
    $user->roles()->attach(2);
    return "attachRole done";
}

Now, I want to save the role that was received from register form,but not attach a role via attachRole() above. 现在,我想保存从注册表中收到的角色,但不通过上面的attachRole()附加角色。
How to modify the create function in AuthController? 如何在AuthController中修改create函数?

You can do it with the Authorization functionality in Laravel, https://laravel.com/docs/5.2/authorization . 您可以使用Laravel https://laravel.com/docs/5.2/authorization中的Authorization功能来实现。

Example code: 示例代码:

$gate->before(function ($user, $ability) {
    if ($user->isSuperAdmin()) {
        return true;
    }
});

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

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