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