| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Maklad\Permission\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use MongoDB\Laravel\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Maklad\Permission\Contracts\PermissionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Maklad\Permission\Contracts\RoleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Maklad\Permission\Exceptions\GuardDoesNotMatch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Maklad\Permission\Exceptions\RoleAlreadyExists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Maklad\Permission\Exceptions\RoleDoesNotExist; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Maklad\Permission\Guard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Maklad\Permission\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Maklad\Permission\Traits\HasPermissions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Maklad\Permission\Traits\RefreshesPermissionCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use ReflectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class Role | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property string $_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @package Maklad\Permission\Models | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class Role extends Model implements RoleInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     use HasPermissions; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     use RefreshesPermissionCache; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public $guarded = ['id']; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     protected Helpers $helpers; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Role constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 123 |  |      * @param array $attributes | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 123 |  |      * @throws ReflectionException | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 123 |  |     public function __construct(array $attributes = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 123 |  |         $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 123 |  |         parent::__construct($attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 123 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->helpers = new Helpers(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->setTable(config('permission.collection_names.roles')); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @param array $attributes | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @return Builder|\Illuminate\Database\Eloquent\Model|RoleInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 123 |  |      * @throws RoleAlreadyExists | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @throws ReflectionException | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 123 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 123 |  |     public static function create(array $attributes = []): \Illuminate\Database\Eloquent\Model|RoleInterface|Builder | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 123 |  |         $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |         $helpers = new Helpers(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |         if (static::query()->where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $name = (string)$attributes['name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $guardName = (string)$attributes['guard_name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 123 |  |             throw new RoleAlreadyExists($helpers->getRoleAlreadyExistsMessage($name, $guardName)); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         return static::query()->create($attributes); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 123 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * Find or create role by its name (and optionally guardName). | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param string|null $guardName | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @return RoleInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @throws RoleAlreadyExists | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @throws ReflectionException | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public static function findOrCreate(string $name, string $guardName = null): Role | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $role = static::query() | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             ->where('name', $name) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |             ->where('guard_name', $guardName) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |             ->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |         if (!$role) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |             $role = static::create(['name' => $name, 'guard_name' => $guardName]); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |         return $role; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * Find a role by its name and guard name. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @param string|null $guardName | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return RoleInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @throws RoleDoesNotExist | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @throws ReflectionException | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 49 |  |     public static function findByName(string $name, string $guardName = null): RoleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 49 |  |         $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 49 |  |         $role = static::query() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 49 |  |             ->where('name', $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 49 |  |             ->where('guard_name', $guardName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             ->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 49 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |         if (!$role) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |             $helpers = new Helpers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             throw new RoleDoesNotExist($helpers->getRoleDoesNotExistMessage($name, $guardName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 47 |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |         return $role; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * A permission belongs to some users of the model associated with its guard. | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     public function usersQuery(): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name'])); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |         return $usersClass->query()->where('role_ids', 'all', [$this->_id]); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 | 12 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 136 | 12 |  |      * A permission belongs to some users of the model associated with its guard. | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 9 |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function getUsersAttribute(): mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 11 |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 | 1 |  |         return $this->usersQuery()->get(); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 142 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 1 |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * Determine if the user may perform the given permission. | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 10 |  |      * @param string|Permission $permission | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @throws GuardDoesNotMatch | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * @throws ReflectionException | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function hasPermissionTo(string|PermissionInterface $permission): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if (is_string($permission)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $permission = $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if (!$this->getGuardNames()->contains($permission->guard_name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $expected = $this->getGuardNames(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             $given = $permission->guard_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             throw new GuardDoesNotMatch($this->helpers->getGuardDoesNotMatchMessage($expected, $given)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |         return in_array($permission->_id, $this->permission_ids ?? [], true); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  |