|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Alliance\Lib; |
|
6
|
|
|
|
|
7
|
|
|
use Stu\Component\Alliance\Enum\AllianceJobPermissionEnum; |
|
8
|
|
|
use Stu\Orm\Entity\Alliance; |
|
9
|
|
|
use Stu\Orm\Entity\AllianceJob; |
|
10
|
|
|
use Stu\Orm\Entity\User; |
|
11
|
|
|
use Stu\Orm\Repository\AllianceMemberJobRepositoryInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class AllianceJobManager implements AllianceJobManagerInterface |
|
14
|
|
|
{ |
|
15
|
2 |
|
public function __construct( |
|
16
|
|
|
private AllianceMemberJobRepositoryInterface $allianceMemberJobRepository |
|
17
|
2 |
|
) {} |
|
18
|
|
|
|
|
19
|
|
|
#[\Override] |
|
20
|
|
|
public function assignUserToJob(User $user, AllianceJob $job): void |
|
21
|
|
|
{ |
|
22
|
|
|
$existing = $this->allianceMemberJobRepository->getByUserAndJob($user, $job->getId()); |
|
23
|
|
|
|
|
24
|
|
|
if ($existing !== null) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$assignment = $this->allianceMemberJobRepository->prototype(); |
|
29
|
|
|
$assignment->setUser($user); |
|
30
|
|
|
$assignment->setJob($job); |
|
31
|
|
|
|
|
32
|
|
|
$this->allianceMemberJobRepository->save($assignment); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
#[\Override] |
|
36
|
|
|
public function removeUserFromJob(User $user, AllianceJob $job): void |
|
37
|
|
|
{ |
|
38
|
|
|
$assignment = $this->allianceMemberJobRepository->getByUserAndJob($user, $job->getId()); |
|
39
|
|
|
|
|
40
|
|
|
if ($assignment === null) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->allianceMemberJobRepository->delete($assignment); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
#[\Override] |
|
48
|
|
|
public function removeUserFromAllJobs(User $user, Alliance $alliance): void |
|
49
|
|
|
{ |
|
50
|
|
|
$assignments = $this->allianceMemberJobRepository->getByUser($user->getId()); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($assignments as $assignment) { |
|
53
|
|
|
if ($assignment->getJob()->getAlliance()->getId() === $alliance->getId()) { |
|
54
|
|
|
$this->allianceMemberJobRepository->delete($assignment); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
#[\Override] |
|
60
|
|
|
public function hasUserJob(User $user, Alliance $alliance): bool |
|
61
|
|
|
{ |
|
62
|
|
|
$assignments = $this->allianceMemberJobRepository->getByUser($user->getId()); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($assignments as $assignment) { |
|
65
|
|
|
if ($assignment->getJob()->getAlliance()->getId() === $alliance->getId()) { |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
#[\Override] |
|
74
|
|
|
public function getUserJobs(User $user, Alliance $alliance): array |
|
75
|
|
|
{ |
|
76
|
9 |
|
$assignments = $this->allianceMemberJobRepository->getByUser($user->getId()); |
|
77
|
9 |
|
$jobs = []; |
|
78
|
|
|
|
|
79
|
9 |
|
foreach ($assignments as $assignment) { |
|
80
|
9 |
|
if ($assignment->getJob()->getAlliance()->getId() === $alliance->getId()) { |
|
81
|
9 |
|
$jobs[] = $assignment->getJob(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
return $jobs; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
9 |
|
#[\Override] |
|
89
|
|
|
public function hasUserPermission(User $user, Alliance $alliance, AllianceJobPermissionEnum $permissionType): bool |
|
90
|
|
|
{ |
|
91
|
9 |
|
$jobs = $this->getUserJobs($user, $alliance); |
|
92
|
|
|
|
|
93
|
9 |
|
foreach ($jobs as $job) { |
|
94
|
9 |
|
if ($job->hasPermission(AllianceJobPermissionEnum::FOUNDER->value)) { |
|
95
|
9 |
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($job->hasPermission($permissionType->value)) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$parentPermission = $permissionType->getParentPermission(); |
|
103
|
|
|
if ($parentPermission !== null && $job->hasPermission($parentPermission->value)) { |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|