Issues (652)

app/Http/Middleware/CheckPermissions.php (7 issues)

1
<?php
2
3
namespace Uccello\Core\Http\Middleware;
4
5
use Closure;
6
use Auth;
7
use Uccello\Core\Models\Domain;
8
9
class CheckPermissions
10
{
11
    /**
12
     * Check if the user has permission to access the asked page or redirect to 403 page.
13
     * Rule: An user is allowed if he is admin or if he has the asked capability.
14
     *
15
     * @param  \Illuminate\Http\Request  $request
16
     * @param  \Closure  $next
17
     * @param  string $capability
18
     * @return mixed
19
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
20
     */
21
    public function handle($request, Closure $next, string $capability)
22
    {
23
        $user = Auth::user();
24
25
        $domain = $request->domain;
26
        $module = $request->module;
27
28
        // If we don't use multi domains, find the first one
29
        if (!uccello()->useMultiDomains()) {
0 ignored issues
show
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
            $domain = Domain::firstOrFail();
31
        }
32
33
        $isModuleActive = $module->isActiveOnDomain($domain);
34
35
        // An user is allowed if he has the capability or if it is an admin module and the user can admin it
36
        $isUserAllowed = $user->hasCapabilityOnModule($capability, $domain, $module) || ($module->isAdminModule() && $user->canAdmin($domain, $module));
0 ignored issues
show
The method hasCapabilityOnModule() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $isUserAllowed = $user->/** @scrutinizer ignore-call */ hasCapabilityOnModule($capability, $domain, $module) || ($module->isAdminModule() && $user->canAdmin($domain, $module));
Loading history...
The method canAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $isUserAllowed = $user->hasCapabilityOnModule($capability, $domain, $module) || ($module->isAdminModule() && $user->/** @scrutinizer ignore-call */ canAdmin($domain, $module));
Loading history...
37
38
        if (!$isModuleActive) {
39
            return abort(404);
0 ignored issues
show
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
        }
41
42
        if (!$isUserAllowed) {
43
            // Try to redirect to a domain accessible by the user
44
            $domain = uccello()->useMultiDomains() ? uccello()->getLastOrDefaultDomain() : null;
0 ignored issues
show
Are you sure the usage of uccello() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
            if ($domain && $user->canRetrieve($domain, ucmodule('home'))) {
0 ignored issues
show
The method canRetrieve() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            if ($domain && $user->/** @scrutinizer ignore-call */ canRetrieve($domain, ucmodule('home'))) {
Loading history...
46
                return redirect(ucroute('uccello.home', $domain));
47
            }
48
49
            return abort(403);
0 ignored issues
show
Are you sure the usage of abort(403) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
        }
51
52
        return $next($request);
53
    }
54
}
55