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