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