1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\Rules; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\User; |
7
|
|
|
use BristolSU\Module\AssignRoles\Support\PositionSettingRetrieval; |
8
|
|
|
use BristolSU\Support\Authentication\Contracts\Authentication; |
9
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
10
|
|
|
use BristolSU\Support\Logic\Facade\LogicTester; |
11
|
|
|
use Illuminate\Contracts\Validation\Rule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Is the user available to be assigned? |
15
|
|
|
* |
16
|
|
|
* If the user belongs to a user, which has a position that is 'user only has one user', they will not be available |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
class UserIsAvailableToBeAssigned implements Rule |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @var PositionSettingRetrieval |
23
|
|
|
*/ |
24
|
|
|
private $positionSettingRetrieval; |
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var Authentication |
27
|
|
|
*/ |
28
|
|
|
private $authentication; |
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var User |
31
|
|
|
*/ |
32
|
|
|
private $userRepository; |
|
|
|
|
33
|
|
|
|
34
|
7 |
|
public function __construct(Authentication $authentication, PositionSettingRetrieval $positionSettingRetrieval, User $userRepository) |
|
|
|
|
35
|
|
|
{ |
36
|
7 |
|
$this->positionSettingRetrieval = $positionSettingRetrieval; |
37
|
7 |
|
$this->authentication = $authentication; |
38
|
7 |
|
$this->userRepository = $userRepository; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
|
|
|
|
44
|
7 |
|
public function passes($attribute, $value) |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
7 |
|
$settings = $this->positionSettingRetrieval->getSettings($this->group()); |
|
|
|
|
48
|
|
|
} catch (\Exception $e) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
7 |
|
$user = $this->userRepository->getById($value); |
52
|
|
|
|
53
|
|
|
return $user->roles()->filter(function(Role $role) use ($settings, $user) { |
|
|
|
|
54
|
1 |
|
return in_array($role->id(), $settings['user_only_has_one_role']); |
55
|
7 |
|
})->count() === 0; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
protected function group() |
|
|
|
|
59
|
|
|
{ |
60
|
7 |
|
return $this->authentication->getGroup(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
|
|
|
|
66
|
1 |
|
public function message() |
67
|
|
|
{ |
68
|
1 |
|
return 'The user is already in a role'; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|