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\Support\Collection; |
||
7 | use Spatie\Permission\Traits\HasRoles; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | use Spatie\Permission\PermissionRegistrar; |
||
10 | use Spatie\Permission\Traits\RefreshesPermissionCache; |
||
11 | use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
||
12 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
13 | use Spatie\Permission\Exceptions\PermissionAlreadyExists; |
||
14 | use Spatie\Permission\Contracts\Permission as PermissionContract; |
||
15 | |||
16 | class Permission extends Model implements PermissionContract |
||
17 | { |
||
18 | use HasRoles; |
||
19 | use RefreshesPermissionCache; |
||
20 | |||
21 | protected $guarded = ['id']; |
||
22 | |||
23 | public function __construct(array $attributes = []) |
||
24 | { |
||
25 | $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard'); |
||
26 | |||
27 | parent::__construct($attributes); |
||
28 | } |
||
29 | |||
30 | public function getTable() |
||
31 | { |
||
32 | return config('permission.table_names.permissions', parent::getTable()); |
||
33 | } |
||
34 | |||
35 | public static function create(array $attributes = []) |
||
36 | { |
||
37 | $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); |
||
38 | |||
39 | $permission = static::getPermissions(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']])->first(); |
||
40 | |||
41 | if ($permission) { |
||
42 | throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']); |
||
43 | } |
||
44 | |||
45 | return static::query()->create($attributes); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * A permission can be applied to roles. |
||
50 | */ |
||
51 | public function roles(): BelongsToMany |
||
52 | { |
||
53 | return $this->belongsToMany( |
||
54 | config('permission.models.role'), |
||
55 | config('permission.table_names.role_has_permissions'), |
||
56 | 'permission_id', |
||
57 | 'role_id' |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * A permission belongs to some users of the model associated with its guard. |
||
63 | */ |
||
64 | View Code Duplication | public function users(): BelongsToMany |
|
0 ignored issues
–
show
|
|||
65 | { |
||
66 | return $this->morphedByMany( |
||
67 | getModelForGuard($this->attributes['guard_name']), |
||
68 | 'model', |
||
69 | config('permission.table_names.model_has_permissions'), |
||
70 | 'permission_id', |
||
71 | config('permission.column_names.model_morph_key') |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Find a permission by its name (and optionally guardName). |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @param string|null $guardName |
||
80 | * |
||
81 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
82 | * |
||
83 | * @return \Spatie\Permission\Contracts\Permission |
||
84 | */ |
||
85 | View Code Duplication | public static function findByName(string $name, $guardName = null): PermissionContract |
|
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. ![]() |
|||
86 | { |
||
87 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
88 | $permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first(); |
||
89 | if (! $permission) { |
||
90 | throw PermissionDoesNotExist::create($name, $guardName); |
||
91 | } |
||
92 | |||
93 | return $permission; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Find a permission by its id (and optionally guardName). |
||
98 | * |
||
99 | * @param int $id |
||
100 | * @param string|null $guardName |
||
101 | * |
||
102 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
103 | * |
||
104 | * @return \Spatie\Permission\Contracts\Permission |
||
105 | */ |
||
106 | View Code Duplication | public static function findById(int $id, $guardName = null): PermissionContract |
|
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. ![]() |
|||
107 | { |
||
108 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
109 | $permission = static::getPermissions(['id' => $id, 'guard_name' => $guardName])->first(); |
||
110 | |||
111 | if (! $permission) { |
||
112 | throw PermissionDoesNotExist::withId($id, $guardName); |
||
113 | } |
||
114 | |||
115 | return $permission; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Find or create permission by its name (and optionally guardName). |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @param string|null $guardName |
||
123 | * |
||
124 | * @return \Spatie\Permission\Contracts\Permission |
||
125 | */ |
||
126 | View Code Duplication | public static function findOrCreate(string $name, $guardName = null): PermissionContract |
|
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. ![]() |
|||
127 | { |
||
128 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
129 | $permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first(); |
||
130 | |||
131 | if (! $permission) { |
||
132 | return static::query()->create(['name' => $name, 'guard_name' => $guardName]); |
||
133 | } |
||
134 | |||
135 | return $permission; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get the current cached permissions. |
||
140 | */ |
||
141 | protected static function getPermissions(array $params = []): Collection |
||
142 | { |
||
143 | return app(PermissionRegistrar::class) |
||
144 | ->setPermissionClass(static::class) |
||
145 | ->getPermissions($params); |
||
146 | } |
||
147 | } |
||
148 |
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.