2016-02-29 104 views
0

我剛纔在我的laravel 5.1項目中安裝了entrust包,你可以在這裏找到這個包Entrust Package Github。 我想在註冊發佈按鈕之後爲用戶分配一個角色,因爲之後每個用戶都將完成不同的配置文件。你可以看到上面的AuthController.php。如何在註冊期間爲用戶分配角色? (Entrust)(Laravel 5.1)

<?php 


namespace App\Http\Controllers\Auth; 

use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class AuthController extends Controller{ 

use AuthenticatesAndRegistersUsers, ThrottlesLogins; 


protected $redirectPath = '/'; 

protected $loginPath = '/'; 

/** 
* Create a new authentication controller instance. 
* 
* @return void 
*/ 
public function __construct() 
{ 
    $this->middleware('guest', ['except' => 'getLogout']); 
} 

/** 
* Get a validator for an incoming registration request. 
* 
* @param array $data 
* @return \Illuminate\Contracts\Validation\Validator 
*/ 
protected function validator(array $data) 
{ 
    return Validator::make($data, [ 
     'name' => 'required|max:255', 
     'email' => 'required|email|max:255|unique:users', 
     'password' => 'required|confirmed|min:6', 
     'role' => 'required|', 

    ]); 
} 

/** 
* Create a new user instance after a valid registration. 
* 
* @param array $data 
* @return User 
*/ 
protected function create(array $data) 
{ 
    return User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
     'role' => $data['role'], 
    ]); 

    $tutorschoolRole = DB::table('roles')->where('name', '=', 'Φροντιστήριο')->pluck('id'); 
    $studentRole = DB::table('roles')->where('name', '=', 'Μαθητής')->pluck('id'); 
    $teacherRole = DB::table('roles')->where('name', '=', 'Καθηγητής')->pluck('id'); 
    $parentRole = DB::table('roles')->where('name', '=', 'Γονέας')->pluck('id'); 

    if(User::role == "Φροντιστήριο"){ 
     User::roles()->attach($tutorschoolRole); 
    } 

    if(User::role == "Μαθητής"){ 
     User::roles()->attach($studentRole); 
    } 

    if(User::role == "Καθηγητής"){ 
     User::roles()->attach($teacherRole); 
    } 

    if(User::role == "Γονέας"){ 
     User::roles()->attach($parentRole); 
    } 
} 

}

回答

0

我解決了這個問題,我不得不返回用戶。

protected function create(array $data) 
{ 


    $user = User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
     'role' => $data['role'], 
    ]); 


    $role_value = $user->role; 
    $id = 0; 

    if($role_value == 'Φροντιστήριο') 
     $userRole = DB::table('roles')->where('name', '=', 'Tutorschool')->pluck('id'); 

    if($role_value == 'Μαθητής') 
     $userRole = DB::table('roles')->where('name', '=', 'Student')->pluck('id'); 

    if($role_value == 'Καθηγητής') 
     $userRole = DB::table('roles')->where('name', '=', 'Teacher')->pluck('id'); 

    if($role_value == 'Γονέας') 
     $userRole = DB::table('roles')->where('name', '=', 'Parent')->pluck('id'); 

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

    return $user; 
} 
0

刪除用戶表中的角色列和遷移使用此命令委託表。

php artisan entrust:migration 

的4代表將被創建:

  • 角色 - 賣場角色記錄
  • 權限 - 商店准許記錄
  • ROLE_USER - 店多到很多角色和用戶
  • 之間的關係
  • permission_role - 存儲角色之間的多對多關係和 權限

從您的數據庫中,您可以手動添加角色和權限。將角色附加到用戶的簡單方法。

$admin = new Role(); 
$admin->name   = 'admin'; 
$admin->display_name = 'User Administrator'; // optional 
$admin->description = 'User is allowed to manage and edit other users'; // optional 
$admin->save(); 
$user = User::find(1); 
$user->attachRole($admin); // parameter can be an Role object, array, or id 
相關問題