繁体   English   中英

我如何在注册过程中为用户分配角色? (委托)(Laravel 5.1)

[英]How i can assign a role to a user during signup? ( Entrust ) ( Laravel 5.1 )

我刚刚在laravel 5.1项目中安装了entrust软件包,您可以在Entrust Package Github中找到此软件包。 我想在注册发布按钮后为用户分配一个角色,因为在此之后,每个用户都将完成一个不同的配置文件。 您可以在上方看到AuthController.php。

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

}

从用户表中删除角色列,然后使用此命令迁移Entrust表。

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

暂无
暂无

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

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