# 前準備
### 6系のmake:auth
$ php composer.phar require laravel/ui
$ php artisan ui vue –auth
### usersテーブルとuser_idにbelongsToのrolesテーブル
$ php artisan make:model Role -m
create_users_table
-> role_idは便宜上、nullableにしておく
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('role_id')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
create_roles_table
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
$ php artisan migrate
mysql> insert into roles (name) values (‘administrator’);
mysql> insert into roles (name) values (‘subscriber’);
### font end
$ npm install
$ npm run dev
### ユーザーデータ作成
$ php artisan serve –host 192.168.33.10 –port 8000
// ユーザ作成
http://192.168.33.10:8000/register
->田中太郎(‘administrator’)
->山田一郎(‘subscriber’)
// role_idを付与
mysql> update users set role_id=1 where id=1;
mysql> update users set role_id=2 where id=2;
mysql> select * from users;
ここまでは大体、10~15分位
# auth認証
### middleware作成
$ php artisan make:middleware IsAdmin
app/Http/Kernel.php
protected $routeMiddleware = [
//省略
'IsAdmin'=>\App\Http\Middleware\IsAdmin::class,
];
### Userモデルで権限判定処理
User.php
public function IsAdmin(){
if($this->role->name == 'administrator'){
return true;
}
return false;
}
Route
Route::get('/', function () {
// return view('welcome');
$user = Auth::user();
if($user->IsAdmin()){
echo "this user is administartor";
}
});
### middlewareの処理
app/Http/Middleware/IsAdmin.php
-> adminでなければtopにリダイレクト
use Illuminate\Support\Facades\Auth;
public function handle($request, Closure $next)
{
$user = Auth::user();
if(!$user->isAdmin()){
return redirect()->intended('/');
}
return $next($request);
}
$ php artisan make:controller –resource AdminController
$ php artisan make:controller –resource SubscriberController
### route
Route::group(['middleware' => 'auth'], function(){
Route::get('/admin/', 'AdminController@index');
Route::get('/', 'SubscriberController@index');
});
### admin, subscriberのview作成
resources/views/admin/index.blade.php
resources/views/subscriber/index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
this is {{ $user->role->name }}, {{ $user->name }}<br>
<form id="logout-form" action="{{ route('logout') }}" method="POST" name="logout">
@csrf
<a href="javascript:document.logout.submit()">ログアウト</a>
</form>
</body>
</html>
### controller
AdminController
public function __construct(){
$this->middleware('IsAdmin');
}
public function index()
{
//
$user = Auth::user();
return view('admin.index', compact('user'))
}
### ログイン後のリダイレクト先の変更
app/Http/Middleware/RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
// return redirect(RouteServiceProvider::HOME);
return redirect('/admin');
}
return $next($request);
}
app/Http/Controllers/Auth/LoginController.php
app/Http/Controllers/Auth/RegisterController.php
app/Http/Controllers/Auth/ResetPasswordController.php
protected $redirectTo = '/admin';
これだと、administratorは、’/admin’, ‘/’ 両方アクセスできるので、IsSubscriberのMiddlewareを作らないと駄目か。
subscriberの挙動は期待通りになる。