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