-
Notifications
You must be signed in to change notification settings - Fork 905
/
IdentityApplicationService.java
463 lines (374 loc) · 17 KB
/
IdentityApplicationService.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright 2012,2013 Vaughn Vernon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.saasovation.identityaccess.application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.saasovation.identityaccess.application.command.ActivateTenantCommand;
import com.saasovation.identityaccess.application.command.AddGroupToGroupCommand;
import com.saasovation.identityaccess.application.command.AddUserToGroupCommand;
import com.saasovation.identityaccess.application.command.AuthenticateUserCommand;
import com.saasovation.identityaccess.application.command.ChangeContactInfoCommand;
import com.saasovation.identityaccess.application.command.ChangeEmailAddressCommand;
import com.saasovation.identityaccess.application.command.ChangePostalAddressCommand;
import com.saasovation.identityaccess.application.command.ChangePrimaryTelephoneCommand;
import com.saasovation.identityaccess.application.command.ChangeSecondaryTelephoneCommand;
import com.saasovation.identityaccess.application.command.ChangeUserPasswordCommand;
import com.saasovation.identityaccess.application.command.ChangeUserPersonalNameCommand;
import com.saasovation.identityaccess.application.command.DeactivateTenantCommand;
import com.saasovation.identityaccess.application.command.DefineUserEnablementCommand;
import com.saasovation.identityaccess.application.command.ProvisionGroupCommand;
import com.saasovation.identityaccess.application.command.ProvisionTenantCommand;
import com.saasovation.identityaccess.application.command.RegisterUserCommand;
import com.saasovation.identityaccess.application.command.RemoveGroupFromGroupCommand;
import com.saasovation.identityaccess.application.command.RemoveUserFromGroupCommand;
import com.saasovation.identityaccess.domain.model.identity.AuthenticationService;
import com.saasovation.identityaccess.domain.model.identity.ContactInformation;
import com.saasovation.identityaccess.domain.model.identity.EmailAddress;
import com.saasovation.identityaccess.domain.model.identity.Enablement;
import com.saasovation.identityaccess.domain.model.identity.FullName;
import com.saasovation.identityaccess.domain.model.identity.Group;
import com.saasovation.identityaccess.domain.model.identity.GroupMemberService;
import com.saasovation.identityaccess.domain.model.identity.GroupRepository;
import com.saasovation.identityaccess.domain.model.identity.Person;
import com.saasovation.identityaccess.domain.model.identity.PostalAddress;
import com.saasovation.identityaccess.domain.model.identity.Telephone;
import com.saasovation.identityaccess.domain.model.identity.Tenant;
import com.saasovation.identityaccess.domain.model.identity.TenantId;
import com.saasovation.identityaccess.domain.model.identity.TenantProvisioningService;
import com.saasovation.identityaccess.domain.model.identity.TenantRepository;
import com.saasovation.identityaccess.domain.model.identity.User;
import com.saasovation.identityaccess.domain.model.identity.UserDescriptor;
import com.saasovation.identityaccess.domain.model.identity.UserRepository;
@Transactional
public class IdentityApplicationService {
@Autowired
private AuthenticationService authenticationService;
@Autowired
private GroupMemberService groupMemberService;
@Autowired
private GroupRepository groupRepository;
@Autowired
private TenantProvisioningService tenantProvisioningService;
@Autowired
private TenantRepository tenantRepository;
@Autowired
private UserRepository userRepository;
public IdentityApplicationService() {
super();
// IdentityAccessEventProcessor.register();
}
@Transactional
public void activateTenant(ActivateTenantCommand aCommand) {
Tenant tenant = this.existingTenant(aCommand.getTenantId());
tenant.activate();
}
@Transactional
public void addGroupToGroup(AddGroupToGroupCommand aCommand) {
Group parentGroup =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getParentGroupName());
Group childGroup =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getChildGroupName());
parentGroup.addGroup(childGroup, this.groupMemberService());
}
@Transactional
public void addUserToGroup(AddUserToGroupCommand aCommand) {
Group group =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getGroupName());
User user =
this.existingUser(
aCommand.getTenantId(),
aCommand.getUsername());
group.addUser(user);
}
@Transactional(readOnly=true)
public UserDescriptor authenticateUser(AuthenticateUserCommand aCommand) {
UserDescriptor userDescriptor =
this.authenticationService()
.authenticate(
new TenantId(aCommand.getTenantId()),
aCommand.getUsername(),
aCommand.getPassword());
return userDescriptor;
}
@Transactional
public void deactivateTenant(DeactivateTenantCommand aCommand) {
Tenant tenant = this.existingTenant(aCommand.getTenantId());
tenant.deactivate();
}
@Transactional
public void changeUserContactInformation(ChangeContactInfoCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
this.internalChangeUserContactInformation(
user,
new ContactInformation(
new EmailAddress(aCommand.getEmailAddress()),
new PostalAddress(
aCommand.getAddressStreetAddress(),
aCommand.getAddressCity(),
aCommand.getAddressStateProvince(),
aCommand.getAddressPostalCode(),
aCommand.getAddressCountryCode()),
new Telephone(aCommand.getPrimaryTelephone()),
new Telephone(aCommand.getSecondaryTelephone())));
}
@Transactional
public void changeUserEmailAddress(ChangeEmailAddressCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
this.internalChangeUserContactInformation(
user,
user.person()
.contactInformation()
.changeEmailAddress(new EmailAddress(aCommand.getEmailAddress())));
}
@Transactional
public void changeUserPostalAddress(ChangePostalAddressCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
this.internalChangeUserContactInformation(
user,
user.person()
.contactInformation()
.changePostalAddress(
new PostalAddress(
aCommand.getAddressStreetAddress(),
aCommand.getAddressCity(),
aCommand.getAddressStateProvince(),
aCommand.getAddressPostalCode(),
aCommand.getAddressCountryCode())));
}
@Transactional
public void changeUserPrimaryTelephone(ChangePrimaryTelephoneCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
this.internalChangeUserContactInformation(
user,
user.person()
.contactInformation()
.changePrimaryTelephone(new Telephone(aCommand.getTelephone())));
}
@Transactional
public void changeUserSecondaryTelephone(ChangeSecondaryTelephoneCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
this.internalChangeUserContactInformation(
user,
user.person()
.contactInformation()
.changeSecondaryTelephone(new Telephone(aCommand.getTelephone())));
}
@Transactional
public void changeUserPassword(ChangeUserPasswordCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
user.changePassword(aCommand.getCurrentPassword(), aCommand.getChangedPassword());
}
@Transactional
public void changeUserPersonalName(ChangeUserPersonalNameCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
user.person().changeName(new FullName(aCommand.getFirstName(), aCommand.getLastName()));
}
@Transactional
public void defineUserEnablement(DefineUserEnablementCommand aCommand) {
User user = this.existingUser(aCommand.getTenantId(), aCommand.getUsername());
user.defineEnablement(
new Enablement(
aCommand.isEnabled(),
aCommand.getStartDate(),
aCommand.getEndDate()));
}
@Transactional(readOnly=true)
public Group group(String aTenantId, String aGroupName) {
Group group =
this.groupRepository()
.groupNamed(new TenantId(aTenantId), aGroupName);
return group;
}
@Transactional(readOnly=true)
public boolean isGroupMember(String aTenantId, String aGroupName, String aUsername) {
Group group =
this.existingGroup(
aTenantId,
aGroupName);
User user =
this.existingUser(
aTenantId,
aUsername);
return group.isMember(user, this.groupMemberService());
}
@Transactional
public Group provisionGroup(ProvisionGroupCommand aCommand) {
Tenant tenant = this.existingTenant(aCommand.getTenantId());
Group group =
tenant.provisionGroup(
aCommand.getGroupName(),
aCommand.getDescription());
this.groupRepository().add(group);
return group;
}
@Transactional
public Tenant provisionTenant(ProvisionTenantCommand aCommand) {
return
this.tenantProvisioningService().provisionTenant(
aCommand.getTenantName(),
aCommand.getTenantDescription(),
new FullName(
aCommand.getAdministorFirstName(),
aCommand.getAdministorLastName()),
new EmailAddress(aCommand.getEmailAddress()),
new PostalAddress(
aCommand.getAddressStateProvince(),
aCommand.getAddressCity(),
aCommand.getAddressStateProvince(),
aCommand.getAddressPostalCode(),
aCommand.getAddressCountryCode()),
new Telephone(aCommand.getPrimaryTelephone()),
new Telephone(aCommand.getSecondaryTelephone()));
}
@Transactional
public User registerUser(RegisterUserCommand aCommand) {
Tenant tenant = this.existingTenant(aCommand.getTenantId());
User user =
tenant.registerUser(
aCommand.getInvitationIdentifier(),
aCommand.getUsername(),
aCommand.getPassword(),
new Enablement(
aCommand.isEnabled(),
aCommand.getStartDate(),
aCommand.getEndDate()),
new Person(
new TenantId(aCommand.getTenantId()),
new FullName(aCommand.getFirstName(), aCommand.getLastName()),
new ContactInformation(
new EmailAddress(aCommand.getEmailAddress()),
new PostalAddress(
aCommand.getAddressStateProvince(),
aCommand.getAddressCity(),
aCommand.getAddressStateProvince(),
aCommand.getAddressPostalCode(),
aCommand.getAddressCountryCode()),
new Telephone(aCommand.getPrimaryTelephone()),
new Telephone(aCommand.getSecondaryTelephone()))));
if (user == null) {
throw new IllegalStateException("User not registered.");
}
this.userRepository().add(user);
return user;
}
@Transactional
public void removeGroupFromGroup(RemoveGroupFromGroupCommand aCommand) {
Group parentGroup =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getParentGroupName());
Group childGroup =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getChildGroupName());
parentGroup.removeGroup(childGroup);
}
@Transactional
public void removeUserFromGroup(RemoveUserFromGroupCommand aCommand) {
Group group =
this.existingGroup(
aCommand.getTenantId(),
aCommand.getGroupName());
User user =
this.existingUser(
aCommand.getTenantId(),
aCommand.getUsername());
group.removeUser(user);
}
@Transactional(readOnly=true)
public Tenant tenant(String aTenantId) {
Tenant tenant =
this.tenantRepository()
.tenantOfId(new TenantId(aTenantId));
return tenant;
}
@Transactional(readOnly=true)
public User user(String aTenantId, String aUsername) {
User user =
this.userRepository()
.userWithUsername(
new TenantId(aTenantId),
aUsername);
return user;
}
@Transactional(readOnly=true)
public UserDescriptor userDescriptor(
String aTenantId,
String aUsername) {
UserDescriptor userDescriptor = null;
User user = this.user(aTenantId, aUsername);
if (user != null) {
userDescriptor = user.userDescriptor();
}
return userDescriptor;
}
private AuthenticationService authenticationService() {
return this.authenticationService;
}
private Group existingGroup(String aTenantId, String aGroupName) {
Group group = this.group(aTenantId, aGroupName);
if (group == null) {
throw new IllegalArgumentException(
"Group does not exist for: "
+ aTenantId + " and: " + aGroupName);
}
return group;
}
private Tenant existingTenant(String aTenantId) {
Tenant tenant = this.tenant(aTenantId);
if (tenant == null) {
throw new IllegalArgumentException(
"Tenant does not exist for: " + aTenantId);
}
return tenant;
}
private User existingUser(String aTenantId, String aUsername) {
User user = this.user(aTenantId, aUsername);
if (user == null) {
throw new IllegalArgumentException(
"User does not exist for: "
+ aTenantId + " and " + aUsername);
}
return user;
}
private GroupMemberService groupMemberService() {
return this.groupMemberService;
}
private GroupRepository groupRepository() {
return this.groupRepository;
}
private void internalChangeUserContactInformation(
User aUser,
ContactInformation aContactInformation) {
if (aUser == null) {
throw new IllegalArgumentException("User must exist.");
}
aUser.person().changeContactInformation(aContactInformation);
}
private TenantProvisioningService tenantProvisioningService() {
return this.tenantProvisioningService;
}
private TenantRepository tenantRepository() {
return this.tenantRepository;
}
private UserRepository userRepository() {
return this.userRepository;
}
}