1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\User\Models\Eloquent; |
12
|
|
|
|
13
|
|
|
use Cache; |
14
|
|
|
use Config; |
15
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
16
|
|
|
use Longman\Platfourm\Database\Eloquent\Model; |
17
|
|
|
use Longman\Platfourm\User\Models\Eloquent\EntrustRoleTrait; |
18
|
|
|
use Zizaco\Entrust\EntrustRole; |
19
|
|
|
|
20
|
|
|
class Role extends Model |
21
|
|
|
{ |
22
|
|
|
use SoftDeletes; |
23
|
|
|
|
24
|
|
|
protected $fillable = ['name', 'display_name', 'description']; |
25
|
|
|
|
26
|
|
|
protected $searchableFields = ['name', 'display_name', 'description']; |
27
|
|
|
|
28
|
|
|
protected $sortableFields = ['name', 'display_name', 'description', 'created_at', 'updated_at']; |
29
|
|
|
|
30
|
|
|
protected $dates = ['deleted_at']; |
31
|
|
|
|
32
|
|
View Code Duplication |
public function cachedPermissions() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$cacheKey = 'entrust_permissions_for_role_' . $this->getKey(); |
35
|
|
|
return Cache::remember($cacheKey, Config::get('cache.ttl'), function () { |
36
|
|
|
return $this->permissions()->get(); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Checks if the role has a permission by its name. |
42
|
|
|
* |
43
|
|
|
* @param string|array $name Permission name or array of permission names. |
44
|
|
|
* @param bool $requireAll All permissions in the array are required. |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function hasPermission($name, $requireAll = false) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if (is_array($name)) { |
50
|
|
|
foreach ($name as $permissionName) { |
51
|
|
|
$hasPermission = $this->hasPermission($permissionName); |
52
|
|
|
|
53
|
|
|
if ($hasPermission && !$requireAll) { |
54
|
|
|
return true; |
55
|
|
|
} elseif (!$hasPermission && $requireAll) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found |
61
|
|
|
// If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found. |
62
|
|
|
// Return the value of $requireAll; |
63
|
|
|
return $requireAll; |
64
|
|
|
} else { |
65
|
|
|
foreach ($this->cachedPermissions() as $permission) { |
66
|
|
|
if ($permission->name == $name) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function users() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$model = User::class; |
78
|
|
|
if (class_exists(\App\Models\User::class)) { |
79
|
|
|
$model = \App\Models\User::class; |
80
|
|
|
} |
81
|
|
|
return $this->hasMany($model); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function permissions() |
85
|
|
|
{ |
86
|
|
|
$model = Permission::class; |
87
|
|
|
if (class_exists(\App\Models\Permission::class)) { |
88
|
|
|
$model = \App\Models\Permission::class; |
89
|
|
|
} |
90
|
|
|
return $this->belongsToMany($model); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
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.