]> BookStack Code Mirror - bookstack/blob - app/Role.php
4e14db181e62fe3f4a9a7a9fe665e7572b61d4b4
[bookstack] / app / Role.php
1 <?php
2
3 namespace BookStack;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 class Role extends Model
8 {
9
10     protected $fillable = ['display_name', 'description'];
11
12     /**
13      * The roles that belong to the role.
14      */
15     public function users()
16     {
17         return $this->belongsToMany('BookStack\User');
18     }
19
20     /**
21      * The permissions that belong to the role.
22      */
23     public function permissions()
24     {
25         return $this->belongsToMany('BookStack\Permission');
26     }
27
28     /**
29      * Check if this role has a permission.
30      * @param $permission
31      */
32     public function hasPermission($permission)
33     {
34         return $this->permissions->pluck('name')->contains($permission);
35     }
36
37     /**
38      * Add a permission to this role.
39      * @param Permission $permission
40      */
41     public function attachPermission(Permission $permission)
42     {
43         $this->permissions()->attach($permission->id);
44     }
45
46     /**
47      * Detach a single permission from this role.
48      * @param Permission $permission
49      */
50     public function detachPermission(Permission $permission)
51     {
52         $this->permissions()->detach($permission->id);
53     }
54
55     /**
56      * Get the role object for the specified role.
57      * @param $roleName
58      * @return mixed
59      */
60     public static function getRole($roleName)
61     {
62         return static::where('name', '=', $roleName)->first();
63     }
64 }