for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\Http\Middleware;
use Closure;
class CheckIfAdmin
{
/**
* Checked that the logged in user is an administrator.
*
* --------------
* VERY IMPORTANT
* If you have both regular users and admins inside the same table,
* change the contents of this method to check that the logged in user
* is an admin, and not a regular user.
* @param [type] $user [description]
[type]
0
* @return bool [description]
*/
private function checkIfUserIsAdmin($user)
return ($user->isAdmin());
}
* Answer to unauthorized access request.
* @param [type] $request [description]
* @return [type] [description]
private function respondToUnauthorizedRequest($request)
if ($request->ajax() || $request->wantsJson()) {
return response(trans('backpack::base.unauthorized'), 401);
trans
If this is a false-positive, you can also ignore this issue in your code via the ignore-call annotation
ignore-call
return response(/** @scrutinizer ignore-call */ trans('backpack::base.unauthorized'), 401);
response
return /** @scrutinizer ignore-call */ response(trans('backpack::base.unauthorized'), 401);
} else {
return redirect()->guest(backpack_url('login'));
backpack_url
return redirect()->guest(/** @scrutinizer ignore-call */ backpack_url('login'));
redirect
return /** @scrutinizer ignore-call */ redirect()->guest(backpack_url('login'));
* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
public function handle($request, Closure $next)
if (backpack_auth()->guest()) {
backpack_auth
if (/** @scrutinizer ignore-call */ backpack_auth()->guest()) {
return $this->respondToUnauthorizedRequest($request);
if (!$this->checkIfUserIsAdmin(backpack_user())) {
backpack_user
if (!$this->checkIfUserIsAdmin(/** @scrutinizer ignore-call */ backpack_user())) {
return $next($request);