如何在注册期间为用户分配角色?( 委托 )( 拉拉维尔 5.1 )



我刚刚在我的 laravel 5.1 项目中安装了 entrust 包,你可以在这里找到这个包 委托包 Github .我想在注册发布按钮后为用户分配一个角色,因为之后每个用户将完成不同的配置文件。您可以在上面看到身份验证控制器.php。

<?php

namespace AppHttpControllersAuth;
use AppUser;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
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 IlluminateContractsValidationValidator
 */
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);
    }
}

}

从用户表中删除角色列,并使用此命令迁移委托表。

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

我解决了这个问题,最后我不得不返回用户。

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;
}

相关内容

  • 没有找到相关文章

最新更新