]> BookStack Code Mirror - bookstack/blob - app/Role.php
Finished initial implementation of custom role system
[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      * Sets the default role name for newly registered users.
13      * @var string
14      */
15     protected static $default = 'viewer';
16
17     /**
18      * The roles that belong to the role.
19      */
20     public function users()
21     {
22         return $this->belongsToMany('BookStack\User');
23     }
24
25     /**
26      * The permissions that belong to the role.
27      */
28     public function permissions()
29     {
30         return $this->belongsToMany('BookStack\Permission');
31     }
32
33     /**
34      * Check if this role has a permission.
35      * @param $permission
36      */
37     public function hasPermission($permission)
38     {
39         return $this->permissions->pluck('name')->contains($permission);
40     }
41
42     /**
43      * Add a permission to this role.
44      * @param Permission $permission
45      */
46     public function attachPermission(Permission $permission)
47     {
48         $this->permissions()->attach($permission->id);
49     }
50
51     /**
52      * Get an instance of the default role.
53      * @return Role
54      */
55     public static function getDefault()
56     {
57         return static::getRole(static::$default);
58     }
59
60     /**
61      * Get the role object for the specified role.
62      * @param $roleName
63      * @return mixed
64      */
65     public static function getRole($roleName)
66     {
67         return static::where('name', '=', $roleName)->first();
68     }
69 }