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