1

I have just installed in my laravel 5.1 project the entrust package you can find this package here Entrust Package Github . I want to assign a role to a user after the sign up post button because after that each user will complete a different profile. You can see the AuthController.php above.

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

}

arispapapro
  • 319
  • 1
  • 3
  • 10

2 Answers2

0

Delete the role column from the users table and migrate Entrust tables using this command.

php artisan entrust:migration

The 4 tables will be created:

  • roles — stores role records
  • permissions — stores permission records
  • role_user — stores many-to-many relations between roles and users
  • permission_role — stores many-to-many relations between roles and permissions

From your DB, you can add roles and permissions manually. The simple way to attach the role to the user.

$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
Med
  • 2,772
  • 1
  • 11
  • 14
0

I solved this, i had to return the user in the end.

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;
}
arispapapro
  • 319
  • 1
  • 3
  • 10