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