Commit 4a247b2b by Aan Choesni Herlingga

login user berdasarkan role

parent 9058c99a
...@@ -61,6 +61,7 @@ class Kernel extends HttpKernel ...@@ -61,6 +61,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'role' => \App\Http\Middleware\Role::class,
]; ];
/** /**
......
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
class Role
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function __construct(Request $request)
{
$this->req = $request;
}
public function handle($request, Closure $next, $role)
{
if (!auth()->user()) {
return redirect()->route('login');
}
if (auth()->user()->is_active != 1) {
auth()->logout();
return redirect()->route('login');
}
$roles = explode('|', $role);
$accessPage = 0;
foreach ($roles as $r) {
if (auth()->user()->role == $r) {
$accessPage = 1;
}
}
if ($accessPage == 1) {
return $next($request);
}
return redirect('home');
}
}
...@@ -19,6 +19,8 @@ class CreateUsersTable extends Migration ...@@ -19,6 +19,8 @@ class CreateUsersTable extends Migration
$table->string('email')->unique(); $table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable(); $table->timestamp('email_verified_at')->nullable();
$table->string('password'); $table->string('password');
$table->string('role', 100);
$table->string('is_active', 100);
$table->rememberToken(); $table->rememberToken();
$table->timestamps(); $table->timestamps();
}); });
......
...@@ -21,6 +21,8 @@ class UsersTableSeeder extends Seeder ...@@ -21,6 +21,8 @@ class UsersTableSeeder extends Seeder
'email_verified_at' => null, 'email_verified_at' => null,
'password' => Hash::make('anchuz2020'), 'password' => Hash::make('anchuz2020'),
'remember_token' => null, 'remember_token' => null,
'role' => 'admin',
'is_active' => 1,
]); ]);
} }
} }
<?php <?php
Route::group(['middleware' => 'auth'], function () { Route::group(['middleware' => 'auth'], function () {
// Route::group(['middleware' => 'role:admin'], function () { Route::group(['middleware' => 'role:admin|editor'], function () {
Route::group(['namespace' => 'Webprofile\Backend', 'prefix' => 'webprofile'], function () { Route::group(['namespace' => 'Webprofile\Backend', 'prefix' => 'webprofile'], function () {
Route::resource('category', 'CategoryController'); Route::resource('category', 'CategoryController');
Route::resource('posts', 'PostController'); Route::resource('posts', 'PostController');
...@@ -18,5 +18,5 @@ Route::group(['middleware' => 'auth'], function () { ...@@ -18,5 +18,5 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('menu_up/{id}', 'MenuController@menu_up')->name('menu_up'); Route::get('menu_up/{id}', 'MenuController@menu_up')->name('menu_up');
Route::get('menu_down/{id}', 'MenuController@menu_down')->name('menu_down'); Route::get('menu_down/{id}', 'MenuController@menu_down')->name('menu_down');
}); });
// }); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment