This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Permission\Models; |
||
4 | |||
5 | use Spatie\Permission\Guard; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Spatie\Permission\Traits\HasPermissions; |
||
8 | use Spatie\Permission\Exceptions\RoleDoesNotExist; |
||
9 | use Spatie\Permission\Exceptions\GuardDoesNotMatch; |
||
10 | use Spatie\Permission\Exceptions\RoleAlreadyExists; |
||
11 | use Spatie\Permission\Contracts\Role as RoleContract; |
||
12 | use Spatie\Permission\Traits\RefreshesPermissionCache; |
||
13 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
14 | |||
15 | class Role extends Model implements RoleContract |
||
16 | { |
||
17 | use HasPermissions; |
||
18 | use RefreshesPermissionCache; |
||
19 | |||
20 | protected $guarded = ['id']; |
||
21 | |||
22 | public function __construct(array $attributes = []) |
||
23 | { |
||
24 | $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard'); |
||
25 | |||
26 | parent::__construct($attributes); |
||
27 | } |
||
28 | |||
29 | public function getTable() |
||
30 | { |
||
31 | return config('permission.table_names.roles', parent::getTable()); |
||
32 | } |
||
33 | |||
34 | public static function create(array $attributes = []) |
||
35 | { |
||
36 | $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); |
||
37 | |||
38 | if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { |
||
39 | throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']); |
||
40 | } |
||
41 | |||
42 | return static::query()->create($attributes); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * A role may be given various permissions. |
||
47 | */ |
||
48 | public function permissions(): BelongsToMany |
||
49 | { |
||
50 | return $this->belongsToMany( |
||
51 | config('permission.models.permission'), |
||
52 | config('permission.table_names.role_has_permissions'), |
||
53 | 'role_id', |
||
54 | 'permission_id' |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * A role belongs to some users of the model associated with its guard. |
||
60 | */ |
||
61 | View Code Duplication | public function users(): BelongsToMany |
|
0 ignored issues
–
show
|
|||
62 | { |
||
63 | return $this->morphedByMany( |
||
64 | getModelForGuard($this->attributes['guard_name']), |
||
65 | 'model', |
||
66 | config('permission.table_names.model_has_roles'), |
||
67 | 'role_id', |
||
68 | config('permission.column_names.model_morph_key') |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Find a role by its name and guard name. |
||
74 | * |
||
75 | * @param string $name |
||
76 | * @param string|null $guardName |
||
77 | * |
||
78 | * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role |
||
79 | * |
||
80 | * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist |
||
81 | */ |
||
82 | View Code Duplication | public static function findByName(string $name, $guardName = null): RoleContract |
|
0 ignored issues
–
show
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. ![]() |
|||
83 | { |
||
84 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
85 | |||
86 | $role = static::where('name', $name)->where('guard_name', $guardName)->first(); |
||
87 | |||
88 | if (! $role) { |
||
89 | throw RoleDoesNotExist::named($name); |
||
90 | } |
||
91 | |||
92 | return $role; |
||
93 | } |
||
94 | |||
95 | View Code Duplication | public static function findById(int $id, $guardName = null): RoleContract |
|
0 ignored issues
–
show
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. ![]() |
|||
96 | { |
||
97 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
98 | |||
99 | $role = static::where('id', $id)->where('guard_name', $guardName)->first(); |
||
100 | |||
101 | if (! $role) { |
||
102 | throw RoleDoesNotExist::withId($id); |
||
103 | } |
||
104 | |||
105 | return $role; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Find or create role by its name (and optionally guardName). |
||
110 | * |
||
111 | * @param string $name |
||
112 | * @param string|null $guardName |
||
113 | * |
||
114 | * @return \Spatie\Permission\Contracts\Role |
||
115 | */ |
||
116 | View Code Duplication | public static function findOrCreate(string $name, $guardName = null): RoleContract |
|
0 ignored issues
–
show
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. ![]() |
|||
117 | { |
||
118 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
119 | |||
120 | $role = static::where('name', $name)->where('guard_name', $guardName)->first(); |
||
121 | |||
122 | if (! $role) { |
||
123 | return static::query()->create(['name' => $name, 'guard_name' => $guardName]); |
||
124 | } |
||
125 | |||
126 | return $role; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Determine if the user may perform the given permission. |
||
131 | * |
||
132 | * @param string|Permission $permission |
||
133 | * |
||
134 | * @return bool |
||
135 | * |
||
136 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
137 | */ |
||
138 | public function hasPermissionTo($permission): bool |
||
139 | { |
||
140 | if (config('permission.enable_wildcard_permission', false)) { |
||
141 | return $this->hasWildcardPermission($permission, $this->getDefaultGuardName()); |
||
142 | } |
||
143 | |||
144 | $permissionClass = $this->getPermissionClass(); |
||
145 | |||
146 | if (is_string($permission)) { |
||
147 | $permission = $permissionClass->findByName($permission, $this->getDefaultGuardName()); |
||
148 | } |
||
149 | |||
150 | if (is_int($permission)) { |
||
151 | $permission = $permissionClass->findById($permission, $this->getDefaultGuardName()); |
||
152 | } |
||
153 | |||
154 | if (! $this->getGuardNames()->contains($permission->guard_name)) { |
||
155 | throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames()); |
||
156 | } |
||
157 | |||
158 | return $this->permissions->contains('id', $permission->id); |
||
159 | } |
||
160 | } |
||
161 |
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.