]> BookStack Code Mirror - bookstack/blob - app/Auth/Role.php
Update settings.php
[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
7 /**
8  * Class Role
9  * @property string $display_name
10  * @property string $description
11  * @property string $external_auth_id
12  * @package BookStack\Auth
13  */
14 class Role extends Model
15 {
16
17     protected $fillable = ['display_name', 'description', 'external_auth_id'];
18
19     /**
20      * The roles that belong to the role.
21      */
22     public function users()
23     {
24         return $this->belongsToMany(User::class)->orderBy('name', 'asc');
25     }
26
27     /**
28      * Get all related JointPermissions.
29      * @return \Illuminate\Database\Eloquent\Relations\HasMany
30      */
31     public function jointPermissions()
32     {
33         return $this->hasMany(JointPermission::class);
34     }
35
36     /**
37      * The RolePermissions that belong to the role.
38      */
39     public function permissions()
40     {
41         return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
42     }
43
44     /**
45      * Check if this role has a permission.
46      * @param $permissionName
47      * @return bool
48      */
49     public function hasPermission($permissionName)
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      * @param RolePermission $permission
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      * @param RolePermission $permission
72      */
73     public function detachPermission(RolePermission $permission)
74     {
75         $this->permissions()->detach([$permission->id]);
76     }
77
78     /**
79      * Get the role object for the specified role.
80      * @param $roleName
81      * @return Role
82      */
83     public static function getRole($roleName)
84     {
85         return static::query()->where('name', '=', $roleName)->first();
86     }
87
88     /**
89      * Get the role object for the specified system role.
90      * @param $roleName
91      * @return Role
92      */
93     public static function getSystemRole($roleName)
94     {
95         return static::query()->where('system_name', '=', $roleName)->first();
96     }
97
98     /**
99      * Get all visible roles
100      * @return mixed
101      */
102     public static function visible()
103     {
104         return static::query()->where('hidden', '=', false)->orderBy('name')->get();
105     }
106
107     /**
108      * Get the roles that can be restricted.
109      * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
110      */
111     public static function restrictable()
112     {
113         return static::query()->where('system_name', '!=', 'admin')->get();
114     }
115 }