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