thinkstudeo /
laravel-rakshak
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Thinkstudeo\Rakshak\Traits; |
||
| 4 | |||
| 5 | use Thinkstudeo\Rakshak\Ability; |
||
| 6 | use Thinkstudeo\Rakshak\Role; |
||
| 7 | |||
| 8 | trait HasRoles |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Get all the roles assigned to the user. |
||
| 12 | * |
||
| 13 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 14 | */ |
||
| 15 | public function roles() |
||
| 16 | { |
||
| 17 | return $this->belongsToMany(Role::class); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Assign a Role to the current User. |
||
| 22 | * |
||
| 23 | * @param $role |
||
| 24 | * @return $this |
||
| 25 | */ |
||
| 26 | public function assignRole($role) |
||
| 27 | { |
||
| 28 | $model = $role instanceof \Thinkstudeo\Rakshak\Role ? $role : Role::whereName($role)->firstOrFail(); |
||
| 29 | |||
| 30 | $this->roles()->save($model); |
||
| 31 | |||
| 32 | return $this; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Retract a Role to the current User. |
||
| 37 | * |
||
| 38 | * @param $role |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | public function retractRole($role) |
||
| 42 | { |
||
| 43 | $model = $role instanceof \Thinkstudeo\Rakshak\Role ? $role : Role::whereName($role)->firstOrFail(); |
||
| 44 | |||
| 45 | $this->roles()->detach($model->id); |
||
| 46 | |||
| 47 | return $this; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Determine if the user has the given role. |
||
| 52 | * |
||
| 53 | * @param $role |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function hasRole($role) |
||
| 57 | { |
||
| 58 | return is_string($role) |
||
| 59 | ? (bool) $this->roles->contains('name', $role) |
||
| 60 | : (bool) $this->roles->intersect([$role])->count(); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Determine if the user has any of the given roles. |
||
| 65 | * |
||
| 66 | * @param array $roles |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | public function hasAnyRole(array $roles) |
||
| 70 | { |
||
| 71 | return is_string($roles[0]) |
||
| 72 | ? (bool) $this->roles->intersect(Role::whereIn('name', $roles)->get())->count() |
||
| 73 | : (bool) $this->roles->intersect($roles)->count(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Determine if the user has any of the given abilities. |
||
| 78 | * |
||
| 79 | * @param $task |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function hasAbility($task) |
||
| 83 | { |
||
| 84 | $ability = is_string($task) ? Ability::whereName($task)->first() : $task; |
||
| 85 | |||
| 86 | return $ability ? (bool) $ability->roles->intersect($this->roles)->count() : false; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Determine if the user has any of the given abilities. |
||
| 91 | * |
||
| 92 | * @param array $abilities |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function hasAnyAbility(array $tasks) |
||
| 96 | { |
||
| 97 | $count = 0; |
||
| 98 | foreach ($tasks as $task) { |
||
| 99 | $ability = is_string($task) ? Ability::whereName($task)->first() : $task; |
||
| 100 | |||
| 101 | $count += $ability ? $ability->roles->intersect($this->roles)->count() : 0; |
||
| 102 | } |
||
| 103 | |||
| 104 | return (bool) $count; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Determine if the user is the super user. |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isSuperUser() |
||
| 113 | { |
||
| 114 | return (bool) $this->hasRole('super'); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |