sarga/packages/Webkul/User/src/Http/Middleware/Bouncer.php

84 lines
2.0 KiB
PHP
Raw Normal View History

2018-06-20 05:06:27 +00:00
<?php
namespace Webkul\User\Http\Middleware;
2018-12-04 04:26:15 +00:00
use Illuminate\Support\Facades\Route;
2018-06-20 05:06:27 +00:00
2018-10-17 07:21:47 +00:00
class Bouncer
2018-06-20 05:06:27 +00:00
{
/**
2021-10-08 06:16:54 +00:00
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, \Closure $next, $guard = 'admin')
2018-06-20 05:06:27 +00:00
{
2021-10-08 06:16:54 +00:00
if (! auth()->guard($guard)->check()) {
2018-06-25 11:00:42 +00:00
return redirect()->route('admin.session.create');
2018-06-20 05:06:27 +00:00
}
2021-10-28 08:12:37 +00:00
/**
* If user status is changed by admin. Then session should be
* logged out.
*/
if (! (bool) auth()->guard($guard)->user()->status) {
auth()->guard($guard)->logout();
return redirect()->route('admin.session.create');
}
2021-10-08 06:16:54 +00:00
/**
* If somehow the user deleted all permissions, then it should be
* auto logged out and need to contact the administrator again.
*/
if ($this->isPermissionsEmpty()) {
auth()->guard('admin')->logout();
return redirect()->route('admin.session.create');
}
2018-10-17 07:21:47 +00:00
2018-06-20 05:06:27 +00:00
return $next($request);
}
2018-10-17 07:21:47 +00:00
2020-03-05 13:37:08 +00:00
/**
2021-10-08 06:16:54 +00:00
* Check for user, if they have empty permissions or not except admin.
*
* @return bool
*/
public function isPermissionsEmpty()
2018-10-17 07:21:47 +00:00
{
2020-02-20 07:14:00 +00:00
if (! $role = auth()->guard('admin')->user()->role) {
2018-10-17 07:21:47 +00:00
abort(401, 'This action is unauthorized.');
2020-02-20 07:14:00 +00:00
}
2018-10-17 07:21:47 +00:00
2021-10-08 06:16:54 +00:00
if ($role->permission_type === 'all') {
return false;
}
if ($role->permission_type !== 'all' && empty($role->permissions)) {
return true;
}
$this->checkIfAuthorized();
return false;
}
/**
* Check authorization.
*
* @return null
*/
public function checkIfAuthorized()
{
$acl = app('acl');
2021-10-08 06:16:54 +00:00
if ($acl && isset($acl->roles[Route::currentRouteName()])) {
bouncer()->allow($acl->roles[Route::currentRouteName()]);
2018-10-17 07:21:47 +00:00
}
}
}