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