Passed
Push — master ( c57047...e9f946 )
by Mihail
06:06
created

Role   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 10 2
A get() 0 10 2
A can() 0 25 5
A getIdNameAll() 0 9 2
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\App as MainApp;
6
use Ffcms\Core\Arch\ActiveModel;
7
use Ffcms\Core\Cache\MemoryObject;
8
use Ffcms\Core\Helper\Type\Arr;
9
10
/**
11
 * Class Role. Active model for user roles with RBAC permissions.
12
 * @package Apps\ActiveRecord
13
 * @property int $id
14
 * @property string $name
15
 * @property string $permissions
16
 * @property string $color
17
 * @property string $created_at
18
 * @property string $updated_at
19
 */
20
class Role extends ActiveModel
21
{
22
    protected $casts = [
23
        'id' => 'integer',
24
        'name' => 'string',
25
        'permissions' => 'string',
26
        'color' => 'string'
27
    ];
28
29
    /**
30
     * Get all table data as object
31
     * @param array $columns
32
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
33
     */
34
    public static function all($columns = ['*'])
35
    {
36
        $cacheName = 'activerecords.role.all.' . implode('.', $columns);
37
        $records = MemoryObject::instance()->get($cacheName);
38
        if ($records === null) {
39
            $records = parent::all($columns);
40
            MemoryObject::instance()->set($cacheName, $records);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * Get role object via id
48
     * @param int $roleId
49
     * @return object|null
50
     */
51
    public static function get($roleId)
52
    {
53
        $role = MainApp::$Memory->get('user.role.cache.' . $roleId);
54
55
        // not founded in cache
56
        if ($role === null) {
57
            $role = self::find($roleId);
58
            MainApp::$Memory->set('user.role.cache.' . $roleId, $role);
59
        }
60
        return $role;
61
    }
62
63
    /**
64
     * Get all roles as array [id=>name]
65
     * @return null|array
66
     */
67
    public static function getIdNameAll(): ?array
68
    {
69
        $all = self::all();
70
71
        $output = null;
72
        foreach ($all as $row) {
73
            $output[$row->id] = $row->name;
74
        }
75
        return $output;
76
    }
77
78
    /**
79
     * Check if user role contains permission
80
     * @param string $permission
81
     * @return bool
82
     */
83
    public function can($permission): bool
84
    {
85
        // Role::get(id) is not initialized
86
        if (!$this->permissions) {
87
            return false;
88
        }
89
90
        // global admin
91
        $permArray = explode(';', $this->permissions);
92
93
        if (count($permArray) < 1) {
94
            return false;
95
        }
96
97
        // admin can all :)
98
        if (Arr::in('global/all', $permArray)) {
99
            return true;
100
        }
101
102
        // check if current permission in user permission role
103
        if (Arr::in($permission, $permArray)) {
104
            return true;
105
        }
106
107
        return false;
108
    }
109
}
110