php - 我如何在注册期间为用户分配角色? (委托(delegate))(Laravel 5.1)

标签 php mysql laravel entrust

我刚刚在我的 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 个表:

  • roles — 存储角色记录
  • permissions — 存储权限记录
  • 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

关于php - 我如何在注册期间为用户分配角色? (委托(delegate))(Laravel 5.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711118/

相关文章:

php - 使用 XAMPP 访问 Laravel Homestead 中的 phpMyAdmin

php - 带有基本路径的 Ckeditor 链接

php - Amazon S3 - 您建议的上传小于允许的最小大小

php - 使用 .php 文件生成 MySQL 转储

mysql - 对另一个类似表中具有匹配值的记录进行分组、计数和排除

laravel - Laravel 5.3 Blade 支持 <font> 标签吗?

php - 在带有种子的 laravel 8 中,我有这个问题目标类 [TableSeeder] 不存在

php - 如何从 MySQL/PHP 中获取每个科目的科目名称和错过的作业总数

php - 按前缀在数据库表中搜索字符串

mysql - drupal mysql 备份数据库中缺少用户表