]> BookStack Code Mirror - bookstack/blob - app/Role.php
Rolled out new permissions system throughout application
[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      * Get all related entity permissions.
22      * @return \Illuminate\Database\Eloquent\Relations\HasMany
23      */
24     public function entityPermissions()
25     {
26         return $this->hasMany(EntityPermission::class);
27     }
28
29     /**
30      * The permissions that belong to the role.
31      */
32     public function permissions()
33     {
34         return $this->belongsToMany('BookStack\Permission');
35     }
36
37     /**
38      * Check if this role has a permission.
39      * @param $permission
40      */
41     public function hasPermission($permission)
42     {
43         return $this->permissions->pluck('name')->contains($permission);
44     }
45
46     /**
47      * Add a permission to this role.
48      * @param Permission $permission
49      */
50     public function attachPermission(Permission $permission)
51     {
52         $this->permissions()->attach($permission->id);
53     }
54
55     /**
56      * Detach a single permission from this role.
57      * @param Permission $permission
58      */
59     public function detachPermission(Permission $permission)
60     {
61         $this->permissions()->detach($permission->id);
62     }
63
64     /**
65      * Get the role object for the specified role.
66      * @param $roleName
67      * @return mixed
68      */
69     public static function getRole($roleName)
70     {
71         return static::where('name', '=', $roleName)->first();
72     }
73 }