1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Organizations\Controller\Plugin; |
12
|
|
|
|
13
|
|
|
use Auth\AuthenticationService; |
14
|
|
|
use Organizations\Repository\Organization as OrganizationRepository; |
15
|
|
|
use Auth\Repository\User as UserRepository; |
16
|
|
|
use Core\Exception\MissingDependencyException; |
17
|
|
|
use Organizations\Entity\EmployeeInterface; |
18
|
|
|
use Zend\Mvc\Controller\Plugin\AbstractPlugin; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ${CARET} |
22
|
|
|
* |
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
24
|
|
|
* @todo write test |
25
|
|
|
*/ |
26
|
|
|
class AcceptInvitationHandler extends AbstractPlugin |
27
|
|
|
{ |
28
|
|
|
const ERROR_ORGANIZATION_NOT_FOUND = 'ErrorOrganizationNotFound'; |
29
|
|
|
const ERROR_TOKEN_INVALID = 'ErrorTokenInvalid'; |
30
|
|
|
const OK_SET_PW = 'OK_SetPw'; |
31
|
|
|
const OK = 'OK'; |
32
|
|
|
|
33
|
|
|
protected $organizationRepository; |
34
|
|
|
protected $userRepository; |
35
|
|
|
protected $authenticationService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets the authentication service. |
39
|
|
|
* |
40
|
|
|
* @param AuthenticationService $authenticationService |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
6 |
|
public function setAuthenticationService(AuthenticationService $authenticationService) |
45
|
|
|
{ |
46
|
6 |
|
$this->authenticationService = $authenticationService; |
47
|
|
|
|
48
|
6 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the Authentication Service. |
53
|
|
|
* |
54
|
|
|
* @return AuthenticationService |
55
|
|
|
* @throws MissingDependencyException |
56
|
|
|
*/ |
57
|
4 |
|
public function getAuthenticationService() |
58
|
|
|
{ |
59
|
4 |
|
if (!$this->authenticationService) { |
60
|
|
|
throw new MissingDependencyException('\Auth\AuthenticationService', $this); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return $this->authenticationService; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the organizations repository. |
68
|
|
|
* |
69
|
|
|
* @param OrganizationRepository $organizationRepository |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
6 |
|
public function setOrganizationRepository(OrganizationRepository $organizationRepository) |
74
|
|
|
{ |
75
|
6 |
|
$this->organizationRepository = $organizationRepository; |
76
|
|
|
|
77
|
6 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the organization repository |
82
|
|
|
* |
83
|
|
|
* @return OrganizationRepository |
84
|
|
|
* @throws MissingDependencyException |
85
|
|
|
*/ |
86
|
6 |
|
public function getOrganizationRepository() |
87
|
|
|
{ |
88
|
6 |
|
if (!$this->organizationRepository) { |
89
|
|
|
throw new MissingDependencyException('\Organizations\Repository\Organization', $this); |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
return $this->organizationRepository; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the user repository. |
97
|
|
|
* |
98
|
|
|
* @param UserRepository $userRepository |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
5 |
|
public function setUserRepository(UserRepository $userRepository) |
103
|
|
|
{ |
104
|
5 |
|
$this->userRepository = $userRepository; |
105
|
|
|
|
106
|
5 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets the user repository. |
111
|
|
|
* |
112
|
|
|
* @return UserRepository |
113
|
|
|
* @throws MissingDependencyException |
114
|
|
|
*/ |
115
|
5 |
|
public function getUserRepository() |
116
|
|
|
{ |
117
|
5 |
|
if (!$this->userRepository) { |
118
|
|
|
throw new MissingDependencyException('\Auth\Repository\User', $this); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
return $this->userRepository; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
5 |
|
public function process($token, $organizationId) |
128
|
|
|
{ |
129
|
5 |
|
$organizationRepository = $this->getOrganizationRepository(); |
130
|
5 |
|
$organization = $organizationRepository->find($organizationId); /* @var $organization \Organizations\Entity\OrganizationInterface */ |
131
|
|
|
|
132
|
5 |
|
if (!$organization) { |
|
|
|
|
133
|
1 |
|
return self::ERROR_ORGANIZATION_NOT_FOUND; |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
$userRepository = $this->getUserRepository(); |
137
|
4 |
|
$user = $userRepository->findByToken($token); /* @var $user \Auth\Entity\User */ |
138
|
|
|
|
139
|
4 |
|
if (!$user) { |
|
|
|
|
140
|
1 |
|
return self::ERROR_TOKEN_INVALID; |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
if ($user->isDraft()) { |
144
|
1 |
|
$user->setIsDraft(false); |
145
|
1 |
|
$user->getInfo()->setEmailVerified(true); |
146
|
1 |
|
$mustSetPassword = true; |
147
|
|
|
} else { |
148
|
2 |
|
$mustSetPassword = false; |
149
|
2 |
|
$userOrg = $user->getOrganization(); /* @var $userOrg \Organizations\Entity\OrganizationReference */ |
150
|
2 |
|
if ($userOrg->hasAssociation()) { |
151
|
1 |
|
$userEmp = $userOrg->getEmployee($user->getId()); |
152
|
1 |
|
$userEmp->setStatus(EmployeeInterface::STATUS_UNASSIGNED); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
$employee = $organization->getEmployee($user->getId()); |
157
|
3 |
|
$employee->setStatus(EmployeeInterface::STATUS_ASSIGNED); |
158
|
|
|
|
159
|
|
|
|
160
|
3 |
|
foreach ($organizationRepository->findPendingOrganizationsByEmployee($user->getId()) as $pendingOrg) { |
161
|
|
|
/* @var $pendingOrg \Organizations\Entity\OrganizationInterface */ |
162
|
3 |
|
if ($pendingOrg->getId() == $organization->getId()) { |
163
|
3 |
|
continue; |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
$pendingOrgEmp = $pendingOrg->getEmployee($user->getId()); |
167
|
3 |
|
if (!$pendingOrgEmp->isUnassigned(/*strict*/ true)) { |
|
|
|
|
168
|
3 |
|
$pendingOrgEmp->setStatus(EmployeeInterface::STATUS_REJECTED); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
$this->getAuthenticationService()->getStorage()->write($user->getId()); |
173
|
3 |
|
return $mustSetPassword ? self::OK_SET_PW : self::OK; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|