【问题标题】:Manage multiple Role in laravel在 laravel 中管理多个角色
【发布时间】:2018-07-05 16:07:44
【问题描述】:

我创建了一个角色和组表,内容如下:

组:汽车 角色:所有者、客户、代理。

我有一张桌子,上面包含了上面的东西:

组表

角色表

角色组映射表

我在 laravel 默认有登录屏幕,我只是使用 show_login() 覆盖一个方法来更改登录布局。我想实现基于角色的登录。视图仅限于角色(所有者、代理、客户)。

登录控制器:

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


}

用户表模型:

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

【问题讨论】:

标签: php laravel laravel-5


【解决方案1】:

您必须为所有者/代理/客户身份验证创建一个中间件。输入以下内容:

php artisan make:middleware Owner

这将创建一个名为 Owner.php 的中间件文件

在Kernel.php上注册中间件

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

将所有者中间件添加到 route.php 文件中的路由

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

在 User 模型中添加 isOwner() 方法,用于检查是否为管理员。

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

您可以对代理/客户等其他角色执行相同操作。

【讨论】:

  • 我认为你没有得到我
  • 我已经更新了。把这个放在哪里。 '所有者' => \App\Http\Middleware\Owner::class
  • 这是什么意思,这会在你的用户表中查找所有者列
猜你喜欢
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多