我创建了一个角色和组表,内容如下:
组:汽车 角色:所有者,客户,代理商。
我有一张桌子,上面有上面的东西:
分组表
角色表
角色组映射表
我在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');
}
}
答案 0 :(得分:0)
您必须创建用于所有者/代理/客户身份验证的中间件。为此,请输入以下内容:
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";
}]);
将isOwner()方法添加到用户模型以检查其是否为管理员。
public function isOwner()
{
return $this->owner; // this looks for an owner column in your users table
}
对于其他角色,例如代理商/客户,您也可以这样做。