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 = ['*']) |
|
|
|
|
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
|
|
|
} |
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.