bristol-su /
assign-roles
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace BristolSU\Module\AssignRoles\Rules; |
||
| 4 | |||
| 5 | use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository; |
||
| 6 | use BristolSU\Support\Authentication\Contracts\Authentication; |
||
| 7 | use Illuminate\Contracts\Validation\Rule; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Test if a role belongs to a group |
||
| 11 | * |
||
| 12 | * @package BristolSU\Module\AssignRoles\Rules |
||
| 13 | */ |
||
|
0 ignored issues
–
show
|
|||
| 14 | class RoleBelongsToGroup implements Rule |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
|
0 ignored issues
–
show
|
|||
| 18 | * @var RoleRepository |
||
| 19 | */ |
||
| 20 | private $roleRepository; |
||
|
0 ignored issues
–
show
|
|||
| 21 | /** |
||
|
0 ignored issues
–
show
|
|||
| 22 | * @var Authentication |
||
| 23 | */ |
||
| 24 | private $authentication; |
||
|
0 ignored issues
–
show
|
|||
| 25 | |||
| 26 | 24 | public function __construct(RoleRepository $roleRepository, Authentication $authentication) |
|
|
0 ignored issues
–
show
|
|||
| 27 | { |
||
| 28 | 24 | $this->roleRepository = $roleRepository; |
|
| 29 | 24 | $this->authentication = $authentication; |
|
| 30 | 24 | } |
|
| 31 | |||
| 32 | /** |
||
|
0 ignored issues
–
show
|
|||
| 33 | * @inheritDoc |
||
| 34 | */ |
||
|
0 ignored issues
–
show
|
|||
| 35 | 24 | public function passes($attribute, $value) |
|
| 36 | { |
||
| 37 | 24 | $role = $this->roleRepository->getById($value); |
|
| 38 | 24 | return $this->group()->id() === $role->groupId(); |
|
| 39 | } |
||
| 40 | |||
| 41 | 24 | protected function group() |
|
|
0 ignored issues
–
show
|
|||
| 42 | { |
||
| 43 | 24 | return $this->authentication->getGroup(); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
|
0 ignored issues
–
show
|
|||
| 47 | * @inheritDoc |
||
| 48 | */ |
||
|
0 ignored issues
–
show
|
|||
| 49 | 4 | public function message() |
|
| 50 | { |
||
| 51 | 4 | return 'The role does not belong to your group'; |
|
| 52 | } |
||
| 53 | } |