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