]> BookStack Code Mirror - bookstack/blob - app/Auth/Role.php
d6c4a09519ea9b8717009709e5e6ba0e1ec5fbc5
[bookstack] / app / Auth / Role.php
1 <?php
2
3 namespace BookStack\Auth;
4
5 use BookStack\Auth\Permissions\EntityPermission;
6 use BookStack\Auth\Permissions\JointPermission;
7 use BookStack\Auth\Permissions\RolePermission;
8 use BookStack\Interfaces\Loggable;
9 use BookStack\Model;
10 use Illuminate\Database\Eloquent\Collection;
11 use Illuminate\Database\Eloquent\Factories\HasFactory;
12 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13 use Illuminate\Database\Eloquent\Relations\HasMany;
14
15 /**
16  * Class Role.
17  *
18  * @property int        $id
19  * @property string     $display_name
20  * @property string     $description
21  * @property string     $external_auth_id
22  * @property string     $system_name
23  * @property bool       $mfa_enforced
24  * @property Collection $users
25  */
26 class Role extends Model implements Loggable
27 {
28     use HasFactory;
29
30     protected $fillable = ['display_name', 'description', 'external_auth_id', 'mfa_enforced'];
31
32     protected $hidden = ['pivot'];
33
34     protected $casts = [
35         'mfa_enforced' => 'boolean',
36     ];
37
38     /**
39      * The roles that belong to the role.
40      */
41     public function users(): BelongsToMany
42     {
43         return $this->belongsToMany(User::class)->orderBy('name', 'asc');
44     }
45
46     /**
47      * Get all related JointPermissions.
48      */
49     public function jointPermissions(): HasMany
50     {
51         return $this->hasMany(JointPermission::class);
52     }
53
54     /**
55      * The RolePermissions that belong to the role.
56      */
57     public function permissions(): BelongsToMany
58     {
59         return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
60     }
61
62     /**
63      * Get the entity permissions assigned to this role.
64      */
65     public function entityPermissions(): HasMany
66     {
67         return $this->hasMany(EntityPermission::class);
68     }
69
70     /**
71      * Check if this role has a permission.
72      */
73     public function hasPermission(string $permissionName): bool
74     {
75         $permissions = $this->getRelationValue('permissions');
76         foreach ($permissions as $permission) {
77             if ($permission->getRawAttribute('name') === $permissionName) {
78                 return true;
79             }
80         }
81
82         return false;
83     }
84
85     /**
86      * Add a permission to this role.
87      */
88     public function attachPermission(RolePermission $permission)
89     {
90         $this->permissions()->attach($permission->id);
91     }
92
93     /**
94      * Detach a single permission from this role.
95      */
96     public function detachPermission(RolePermission $permission)
97     {
98         $this->permissions()->detach([$permission->id]);
99     }
100
101     /**
102      * Get the role of the specified display name.
103      */
104     public static function getRole(string $displayName): ?self
105     {
106         return static::query()->where('display_name', '=', $displayName)->first();
107     }
108
109     /**
110      * Get the role object for the specified system role.
111      */
112     public static function getSystemRole(string $systemName): ?self
113     {
114         return static::query()->where('system_name', '=', $systemName)->first();
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function logDescriptor(): string
121     {
122         return "({$this->id}) {$this->display_name}";
123     }
124 }