login system laravel with permission levels

0

I'm trying to set up a login system, but I can not seem to get the result I need.

There are three types of users:

  • Admin
  • User (students)
  • Teachers

I've adapted the make:auth with roles to create teacher and student, it's working. But admin will register both users, so only he can have access to a few pages.

I want to know how I can differentiate access to pages, such as the registration page being accessed only by admin . I saw something from Laravel gates permissions etc. But I did not quite understand.

I even created a field in the is_admin table, but I do not know what to do and how to check the pages.

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('is_admin');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    
asked by anonymous 24.10.2017 / 19:29

2 answers

0

From what I understand, you are using the is_admin column that was provided in the Laravel documentation example but in that case it would not be the most appropriate because your permission levels are 3 rather than 2 (something that is_admin would behave easily ).

I suggest you change the column to integer and check the permission based on the level:

$table->integer('permissao')->default(0);

And verification for:

@if($user->permissao == valordepermissao)
  //LIBERE ALGUMA PAGINA
@endif
    
26.10.2017 / 03:31
0
public function up()
{
    Schema::create('user', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->boolean('is_admin')->default(0);
        $table->rememberToken();
        $table->timestamps();
    });

Now just check the page

@if($user->is_admin)
 //código html etc
@endif

The best thing would be to create a Rules Table

//Tabela Regra
Schema::create('role', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});

//Tabela Regra_usuario 
Schema::create('user_role', function (Blueprint $table) {
    $table->bigInteger('user_id')->unsigned();
    $table->integer('role_id')->unsigned();
    $table->foreign('user_id')
        ->references('id')->on('user');
    $table->foreign('role_id')
        ->references('id')->on('role');
});

Now in User_Model

public function isAdministrator() {
   return $this->roles()->where('name', 'Administrator')->exists();
}

//na View
@if(Auth::user()->isAdministrator())
    // html etc
@endif
    
24.10.2017 / 19:33