Passed
Pull Request — master (#53)
by yasin
05:03
created

hasPermission()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 7
c 1
b 0
f 1
nc 4
nop 4
dl 0
loc 12
rs 9.6111
1
<?php
2
3
use EasyPanel\Contracts\CRUDComponent;
4
5
if(! function_exists('getRouteName')) {
6
    function getRouteName(){
7
        $routeName = config('easy_panel.route_prefix');
8
        $routeName = trim($routeName, '/');
9
        $routeName = str_replace('/', '.', $routeName);
10
        return $routeName;
11
    }
12
}
13
14
if(! function_exists('getCrudConfig')) {
15
    function getCrudConfig($name){
16
        $namespace = "\\App\\CRUD\\{$name}Component";
17
18
        if (!file_exists(app_path("/CRUD/{$name}Component.php")) or !class_exists($namespace)){
19
            abort(403, "Class with {$namespace} namespace doesn't exist");
20
        }
21
22
        $instance = app()->make($namespace);
23
24
        if (!$instance instanceof CRUDComponent){
25
            abort(403, "{$namespace} should implement CRUDComponent interface");
26
        }
27
28
        return $instance;
29
    }
30
}
31
32
if(! function_exists('crud')) {
33
    function crud($name){
34
        return \EasyPanel\Models\CRUD::query()->where('name', $name)->first();
35
    }
36
}
37
38
if (! function_exists('hasPermission')) {
39
    function hasPermission($routeName, $withAcl, $withPolicy = false, $entity = []) {
40
        $showButton = true;
41
42
        if ($withAcl) {
43
            if (!auth()->user()->hasPermission($routeName)) {
44
                $showButton = false;
45
            } else if ($withPolicy && !auth()->user()->hasPermission($routeName, $entity)) {
46
                $showButton = false;
47
            }
48
        }
49
50
        return $showButton;
51
    }
52
}
53