2016-05-12 5 views
0

私はLaravel 5.2とZizaco/entrustを使用していますが、私の質問は
です。次の例では、ユーザーの役割を保存する方法は?Laravel 5.2:この例では、ユーザーの役割を保存する方法は?

**登録フォーム**すぐに使用できるフォームに基づいて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アウト・オブ・ボックス。

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

私はRolesControllerを作成してattachRole()関数を書くことができます。したがって、ユーザーにロールを手動で割り当てることができます。このように:

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

は今、私は、レジスタフォームから受け取った役割を保存しますが、()上記attachRole経由役割を添付しないようにしたいです。
AuthControllerのcreate機能を変更するにはどうすればよいですか?

答えて

1

Laravelの認証機能を使用してhttps://laravel.com/docs/5.2/authorizationとすることができます。

例コード:

$gate->before(function ($user, $ability) { 
    if ($user->isSuperAdmin()) { 
     return true; 
    } 
}); 
関連する問題