Files
growup-crm/packages/Webkul/Admin/src/Http/Middleware/Bouncer.php
2026-02-02 15:31:15 -03:00

87 lines
2.1 KiB
PHP
Executable File

<?php
namespace Webkul\Admin\Http\Middleware;
use Illuminate\Support\Facades\Route;
class Bouncer
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param string|null $guard
* @return mixed
*/
public function handle($request, \Closure $next, $guard = 'user')
{
if (! auth()->guard($guard)->check()) {
return redirect()->route('admin.session.create');
}
/**
* If user status is changed by admin. Then session should be
* logged out.
*/
if (! (bool) auth()->guard($guard)->user()->status) {
auth()->guard($guard)->logout();
session()->flash('error', __('admin::app.errors.401'));
return redirect()->route('admin.session.create');
}
/**
* 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($guard)->logout();
session()->flash('error', __('admin::app.errors.401'));
return redirect()->route('admin.session.create');
}
return $next($request);
}
/**
* Check for user, if they have empty permissions or not except admin.
*
* @return bool
*/
public function isPermissionsEmpty()
{
if (! $role = auth()->guard('user')->user()->role) {
abort(401, 'This action is unauthorized.');
}
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()
{
$roles = acl()->getRoles();
if (isset($roles[Route::currentRouteName()])) {
bouncer()->allow($roles[Route::currentRouteName()]);
}
}
}