1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
7
|
|
|
use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
8
|
|
|
use Spatie\Permission\Exceptions\RoleDoesNotExist; |
9
|
|
|
use Spatie\Permission\Traits\HasPermissions; |
10
|
|
|
use Spatie\Permission\Traits\RefreshesPermissionCache; |
11
|
|
|
|
12
|
|
|
class Role extends Model implements RoleContract |
13
|
|
|
{ |
14
|
|
|
use HasPermissions; |
15
|
|
|
use RefreshesPermissionCache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The attributes that aren't mass assignable. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
public $guarded = ['id']; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new Eloquent model instance. |
26
|
|
|
* |
27
|
|
|
* @param array $attributes |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $attributes = []) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($attributes); |
32
|
|
|
|
33
|
|
|
$this->setTable(config('laravel-permission.table_names.roles')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A role may be given various permissions. |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
40
|
|
|
*/ |
41
|
|
|
public function permissions() |
42
|
|
|
{ |
43
|
|
|
return $this->belongsToMany( |
44
|
|
|
config('laravel-permission.models.permission'), |
45
|
|
|
config('laravel-permission.table_names.role_has_permissions') |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* A role may be assigned to various users. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
53
|
|
|
*/ |
54
|
|
|
public function users() |
55
|
|
|
{ |
56
|
|
|
return $this->belongsToMany( |
57
|
|
|
config('auth.model') ?: config('auth.providers.users.model'), |
58
|
|
|
config('laravel-permission.table_names.user_has_roles') |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Find a role by its name. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return Role |
68
|
|
|
* |
69
|
|
|
* @throws RoleDoesNotExist |
70
|
|
|
*/ |
71
|
|
|
public static function findByName($name) |
72
|
|
|
{ |
73
|
|
|
$role = static::where('name', $name)->first(); |
74
|
|
|
|
75
|
|
|
if (!$role) { |
76
|
|
|
throw new RoleDoesNotExist(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $role; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Determine if the user may perform the given permission. |
84
|
|
|
* |
85
|
|
|
* @param string|Permission $permission |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function hasPermissionTo($permission) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
if (is_string($permission)) { |
92
|
|
|
try { |
93
|
|
|
$permission = app(Permission::class)->findByName($permission); |
94
|
|
|
} catch (PermissionDoesNotExist $e) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->permissions->contains('id', $permission->id); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.