Laravel 5.7 - how to define a super user to manage all users registration

1.8k Views Asked by At

need some help about Laravel user auth. my knowledge about Laravel is very little, my experiences were deploying web app based on raw/pure PHP (never have experience with the framework).

The idea is to let an operator input the first and only one user to the system through the Laravel standard registration process, and this user will automatically become Super-User.

The super-user function is to manage normal-user registration (inside super-user session), by creating user & password and define the role of the normal-users.

I define the user's table like this, I add 'user_role' column to distinguished roles between users.

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->char('user_role')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });

'user_role' value for super-user must be 0, while another normal-users role should be numbers between 1-9.

content/views of each section of the webpage will be displayed depends on the 'user_role' value.

need help how to :

  1. display user-registration page/url if none users defined in the user's table.
  2. I need check 'user_role' every time I need to display a section of the page. how to achieve it?
1

There are 1 best solutions below

2
On BEST ANSWER

From your table structure i assume you manage roles within users table. Now when you want to allow a specific role user for some part, just check the role of the user.

if(Auth::user()->user_role==0){
 //do something its super user
  }

In blade template you can check in the following way.

@if(Auth::user()->user_role==0)
<!--do some thing, its super user-->
@endif

To show registration form if there is no user in the table. In your blade you can select data from users table and then check if users table is empty.

<?php $users=App\User::all(); ?>

@if($users ===null)
 <!--users table is empty show registration form-->
@endif