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