Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Auth/Permission.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Auth;
4
5
use Encore\Admin\Facades\Admin;
6
use Encore\Admin\Middleware\Pjax;
7
use Illuminate\Support\Facades\Auth;
8
9
class Permission
10
{
11
    /**
12
     * Check permission.
13
     *
14
     * @param $permission
15
     *
16
     * @return true
17
     */
18
    public static function check($permission)
19
    {
20
        if (static::isAdministrator()) {
21
            return true;
22
        }
23
24
        if (is_array($permission)) {
25
            collect($permission)->each(function ($permission) {
26
                call_user_func([Permission::class, 'check'], $permission);
27
            });
28
29
            return;
30
        }
31
32
        if (Auth::guard('admin')->user()->cannot($permission)) {
33
            static::error();
34
        }
35
    }
36
37
    /**
38
     * Roles allowed to access.
39
     *
40
     * @param $roles
41
     *
42
     * @return true
43
     */
44
    public static function allow($roles)
45
    {
46
        if (static::isAdministrator()) {
47
            return true;
48
        }
49
50
        if (!Auth::guard('admin')->user()->inRoles($roles)) {
51
            static::error();
52
        }
53
    }
54
55
    /**
56
     * Don't check permission.
57
     *
58
     * @return bool
59
     */
60
    public static function free()
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * Roles denied to access.
67
     *
68
     * @param $roles
69
     *
70
     * @return true
71
     */
72
    public static function deny($roles)
73
    {
74
        if (static::isAdministrator()) {
75
            return true;
76
        }
77
78
        if (Auth::guard('admin')->user()->inRoles($roles)) {
79
            static::error();
80
        }
81
    }
82
83
    /**
84
     * Send error response page.
85
     */
86
    public static function error()
87
    {
88
        $response = response(Admin::content()->withError(trans('admin.deny')));
89
90
        if (!request()->pjax() && request()->ajax()) {
91
            abort(403, trans('admin.deny'));
92
        }
93
94
        Pjax::respond($response);
0 ignored issues
show
It seems like $response defined by response(\Encore\Admin\F...r(trans('admin.deny'))) on line 88 can also be of type object<Illuminate\Contra...outing\ResponseFactory>; however, Encore\Admin\Middleware\Pjax::respond() does only seem to accept object<Symfony\Component\HttpFoundation\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
    }
96
97
    /**
98
     * If current user is administrator.
99
     *
100
     * @return mixed
101
     */
102
    public static function isAdministrator()
103
    {
104
        return Auth::guard('admin')->user()->isRole('administrator');
105
    }
106
}
107