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