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