donglu8779 2018-07-04 09:36
浏览 21
已采纳

在laravel中管理多个角色

I have created a roles and group table following things:

Group : Car Role : Owner,Customer,Agent.

I have table where I include above things :

Group table

Role table

Role Group Mapping table

I have login screen in laravel default I just change a login layout using show_login() override a method. I want to implement role base login. Views are restricted to role (Owner,Agent,Customer).

Login Controller:

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function showLoginForm()
    {
        return view('customlogin');
    }


}

User Table Model :

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name');
            $table->string('email',150)->unique();
            $table->string('password');
            $table->integer('roleid')->unsigned();
            $table->rememberToken();
            /*Common Fields*/
            $table->integer('status');
            $table->integer('createdby');
            $table->integer('modifiedby');
            $table->string('publicguid');
            $table->string('privateguid');
            $table->timestamps();

           /*From other table */


        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */

    public function down()
    {
        Schema::dropIfExists('users');
    }
}
  • 写回答

1条回答 默认 最新

  • douzhu7507 2018-07-04 09:48
    关注

    You have to create a middleware for owner/agent/customer authentication. Do so by typing this:

    php artisan make:middleware Owner
    

    This creates a middleware file called Owner.php

    Register the middleware on Kernel.php

    'owner' => \App\Http\Middleware\Owner::class
    

    Add the owner middleware to a route in route.php file

    get('protected', ['middleware' => ['auth', 'owner'], function() {
        return "this page requires that you be logged in and an Owner";
    }]);
    

    Add the isOwner() method to User model for checking if it is admin or not.

    public function isOwner()
    {
        return $this->owner; // this looks for an owner column in your users table
    }
    

    You may do the same for other roles like agent/customer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大