add: full multi-tenancy control
This commit is contained in:
86
packages/Webkul/Admin/src/Http/Middleware/Bouncer.php
Executable file
86
packages/Webkul/Admin/src/Http/Middleware/Bouncer.php
Executable file
@@ -0,0 +1,86 @@
|
||||
<?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()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
packages/Webkul/Admin/src/Http/Middleware/Locale.php
Normal file
40
packages/Webkul/Admin/src/Http/Middleware/Locale.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Admin\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class Locale
|
||||
{
|
||||
/**
|
||||
* The middleware instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
Application $app,
|
||||
Request $request
|
||||
) {
|
||||
$this->app = $app;
|
||||
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
app()->setLocale(
|
||||
core()->getConfigData('general.general.locale_settings.locale')
|
||||
?: app()->getLocale()
|
||||
);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user