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