Permit::hasNeededRole()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php namespace Mascame\Artificer\Permit;
2
3
use Auth;
4
5
abstract class Permit extends Auth
6
{
7
8
    protected static $role = null;
9
10
    /**
11
     * @param $to
12
     */
13
    public static function access($to)
14
    {
15
        return false;
16
    }
17
18
    /**
19
     * @param $action
20
     */
21
    public static function to($action)
22
    {
23
        return false;
24
    }
25
26
    /**
27
     * @param string $role_column
28
     * @return null
29
     */
30
    public static function getRole($role_column = 'role')
31
    {
32
        if (static::$role) {
33
            return static::$role;
34
        }
35
36
        if (isset(Auth::user()->$role_column)) {
37
            return static::$role = Auth::user()->$role_column;
38
        }
39
40
        return null;
41
    }
42
43
    /**
44
     * @param null $permissions
45
     * @return bool
46
     */
47
    public static function hasPermission($permissions = null)
48
    {
49
        if (!$permissions) {
50
            return true;
51
        }
52
53
        if (is_array($permissions) && self::hasNeededRole($permissions)) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * @param $permissions
62
     * @return bool
63
     */
64
    private static function hasNeededRole($permissions)
65
    {
66
        return (in_array(self::getRole(), $permissions) || $permissions[0] == '*');
67
    }
68
}