Skip to content

Instantly share code, notes, and snippets.

@jonathanablanida
Last active November 18, 2016 00:18
Show Gist options
  • Save jonathanablanida/7490d66f9e98c00284e8217a9cebe918 to your computer and use it in GitHub Desktop.
Save jonathanablanida/7490d66f9e98c00284e8217a9cebe918 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class AuthenticateAdmin
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest() || !Auth::guard($guard)->user()->is_admin) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
// return abort('401');
return redirect('/');
}
}
return $next($request);
}
}
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
// Binding eloquent.admin to our EloquentAdminUserProvider
Auth::provider('eloquent.admin', function($app, array $config) {
return new EloquentAdminUserProvider($app['hash'], $config['model']);
});
}
}
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Support\Str;
class EloquentAdminUserProvider extends EloquentUserProvider
{
public function retrieveByCredentials(array $credentials)
{
// Of course here, you could perform the query yourself with the is_admin comparison, but
// I think it's best to avoid as much duplication as possible
$user = parent::retrieveByCredentials($credentials);
return $user && $user->is_admin === false
? null
: $user;
}
}
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.admin' => \App\Http\Middleware\AuthenticateAdmin::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}
<?php
Route::group(['middleware' => ['auth.admin:admin, web']], function()
{
Route::get('/dashboard', function () {
return 'Admin Home';
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment