Passed
Push — master ( 347935...cc7747 )
by Mihail
05:42
created

Role::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 11
loc 11
rs 9.4285
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
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class Role. Active model for user roles with RBAC permissions.
13
 * @package Apps\ActiveRecord
14
 * @property int $id
15
 * @property string $name
16
 * @property string $permissions
17
 * @property string $created_at
18
 * @property string $updated_at
19
 */
20
class Role extends ActiveModel
21
{
22
    /**
23
     * Get all table data as object
24
     * @param array $columns
25
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
26
     */
27 View Code Duplication
    public static function all($columns = ['*'])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $cacheName = 'activerecords.role.all.' . implode('.', $columns);
30
        $records = MemoryObject::instance()->get($cacheName);
31
        if ($records === null) {
32
            $records = parent::all($columns);
33
            MemoryObject::instance()->set($cacheName, $records);
34
        }
35
36
        return $records;
37
    }
38
39
    /**
40
     * Get role object via id
41
     * @param int $role_id
42
     * @return object|null
43
     */
44
    public static function get($role_id)
45
    {
46
        $role = MainApp::$Memory->get('user.role.cache.' . $role_id);
47
48
        // not founded in cache
49
        if ($role === null) {
50
            $role = self::find($role_id);
51
            MainApp::$Memory->set('user.role.cache.' . $role_id, $role);
52
        }
53
        return $role;
54
    }
55
56
    /**
57
     * @deprecated
58
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
59
     */
60
    public static function getAll()
61
    {
62
        return self::all();
63
    }
64
65
    /**
66
     * Get all roles as array [id=>name]
67
     * @return null|array
68
     */
69
    public static function getIdNameAll()
70
    {
71
        $all = self::all();
72
73
        $output = null;
74
        foreach ($all as $row) {
75
            $output[$row->id] = $row->name;
76
        }
77
        return $output;
78
    }
79
80
    /**
81
     * Check if user role contains permission
82
     * @param string $permission
83
     * @return bool
84
     */
85
    public function can($permission)
86
    {
87
88
        // Role::get(id) is not initialized
89
        if ($this->permissions === null) {
90
            return false;
91
        }
92
93
        // global admin
94
        $permArray = explode(';', $this->permissions);
95
96
        if (count($permArray) < 1) {
97
            return false;
98
        }
99
100
        // admin can all :)
101
        if (Arr::in('global/all', $permArray)) {
102
            return true;
103
        }
104
105
        // check if current permission in user permission role
106
        if (Arr::in($permission, $permArray)) {
107
            return true;
108
        }
109
110
        return false;
111
    }
112
}