-
Notifications
You must be signed in to change notification settings - Fork 160
/
AccountServiceImpl.java
455 lines (397 loc) · 20.6 KB
/
AccountServiceImpl.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
/*******************************************************************************
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.service.account.internal;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaDuplicateNameInAnotherAccountError;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalAccessException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.service.internal.cache.NamedEntityCache;
import org.eclipse.kapua.commons.setting.system.SystemSetting;
import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.commons.util.CommonsValidationRegex;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.domain.Domain;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.account.AccountAttributes;
import org.eclipse.kapua.service.account.AccountCreator;
import org.eclipse.kapua.service.account.AccountDomains;
import org.eclipse.kapua.service.account.AccountFactory;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.account.AccountQuery;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.account.internal.exception.KapuaAccountErrorCodes;
import org.eclipse.kapua.service.account.internal.exception.KapuaAccountException;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import java.util.Map;
import java.util.Objects;
/**
* {@link AccountService} implementation.
*
* @since 1.0.0
*/
@KapuaProvider
public class AccountServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<Account, AccountCreator, AccountService, AccountListResult, AccountQuery, AccountFactory>
implements AccountService {
@Inject
private AuthorizationService authorizationService;
@Inject
private PermissionFactory permissionFactory;
private static final String NO_EXPIRATION_DATE_SET = "no expiration date set";
/**
* Constructor.
*
* @since 1.0.0
*/
public AccountServiceImpl() {
super(AccountService.class.getName(), AccountDomains.ACCOUNT_DOMAIN, AccountEntityManagerFactory.getInstance(), AccountCacheFactory.getInstance(), AccountService.class, AccountFactory.class);
}
@Override
//@RaiseServiceEvent
public Account create(AccountCreator accountCreator) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(accountCreator, "accountCreator");
ArgumentValidator.notNull(accountCreator.getScopeId(), "accountCreator.scopeId");
ArgumentValidator.notEmptyOrNull(accountCreator.getName(), "accountCreator.name");
ArgumentValidator.match(accountCreator.getName(), CommonsValidationRegex.NAME_REGEXP, "accountCreator.name");
ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationName(), "accountCreator.organizationName");
ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationEmail(), "accountCreator.organizationEmail");
ArgumentValidator.match(accountCreator.getOrganizationEmail(), CommonsValidationRegex.EMAIL_REGEXP, "accountCreator.organizationEmail");
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.write, accountCreator.getScopeId()));
//
// Check child account policy
if (allowedChildEntities(accountCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Accounts");
}
//
// Check if the parent account exists
if (findById(accountCreator.getScopeId()) == null) {
throw new KapuaIllegalArgumentException(KapuaEntityAttributes.SCOPE_ID, "parent account does not exist: " + accountCreator.getScopeId() + "::");
}
//
// check if the account collides with the SystemSettingKey#COMMONS_CONTROL_TOPIC_CLASSIFIER
if (!StringUtils.isEmpty(SystemSetting.getInstance().getMessageClassifier())) {
if (SystemSetting.getInstance().getMessageClassifier().equals(accountCreator.getName())) {
throw new KapuaIllegalArgumentException("name", "Reserved account name");// obfuscate this message? or change to something more clear like "the account name collides with some system
// configuration parameter"?
}
}
//
// Check duplicate name
AccountQuery query = new AccountQueryImpl(accountCreator.getScopeId());
query.setPredicate(query.attributePredicate(KapuaNamedEntityAttributes.NAME, accountCreator.getName()));
if (count(query) > 0) {
throw new KapuaDuplicateNameException(accountCreator.getName());
}
if (findByName(accountCreator.getName()) != null) {
throw new KapuaDuplicateNameInAnotherAccountError(accountCreator.getName());
}
// check that expiration date is no later than parent expiration date
Account parentAccount = KapuaSecurityUtils.doPrivileged(() -> find(accountCreator.getScopeId()));
if (parentAccount != null && parentAccount.getExpirationDate() != null) {
// parent account never expires no check is needed
if (accountCreator.getExpirationDate() == null || parentAccount.getExpirationDate().before(accountCreator.getExpirationDate())) {
// if current account expiration date is null it will be obviously after parent expiration date
throw new KapuaIllegalArgumentException(AccountAttributes.EXPIRATION_DATE, accountCreator.getExpirationDate() != null ? accountCreator.getExpirationDate().toString() : NO_EXPIRATION_DATE_SET);
}
}
return entityManagerSession.doTransactedAction(EntityManagerContainer.<Account>create().onResultHandler(em -> {
Account account = AccountDAO.create(em, accountCreator);
em.persist(account);
// Set the parent account path
String parentAccountPath = AccountDAO.find(em, null, accountCreator.getScopeId()).getParentAccountPath() + "/" + account.getId();
account.setParentAccountPath(parentAccountPath);
return AccountDAO.update(em, account);
}));
}
@Override
//@RaiseServiceEvent
public Account update(Account account) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(account.getId(), "account.id");
ArgumentValidator.notEmptyOrNull(account.getName(), "account.name");
ArgumentValidator.match(account.getName(), CommonsValidationRegex.NAME_REGEXP, "account.name");
ArgumentValidator.notNull(account.getOrganization(), "account.organization");
ArgumentValidator.match(account.getOrganization().getEmail(), CommonsValidationRegex.EMAIL_REGEXP, "account.organization.email");
//
// Check Access
if (KapuaSecurityUtils.getSession().getScopeId().equals(account.getId())) {
// Editing self
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.write, account.getId()));
} else {
// Editing child
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.write, account.getScopeId()));
}
//
// Check existence
Account oldAccount = find(account.getId());
if (oldAccount == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, account.getId());
}
// check that expiration date is no later than parent expiration date
Account parentAccount = null;
if (oldAccount.getScopeId() != null) {
parentAccount = KapuaSecurityUtils.doPrivileged(() -> find(oldAccount.getScopeId()));
}
if (parentAccount != null && parentAccount.getExpirationDate() != null) {
// if parent account never expires no check is needed
if (account.getExpirationDate() == null || parentAccount.getExpirationDate().before(account.getExpirationDate())) {
// if current account expiration date is null it will be obviously after parent expiration date
throw new KapuaIllegalArgumentException(AccountAttributes.EXPIRATION_DATE, account.getExpirationDate() != null ? account.getExpirationDate().toString() : NO_EXPIRATION_DATE_SET);
}
}
if (account.getExpirationDate() != null) {
SystemSetting setting = SystemSetting.getInstance();
//check if the updated account is an admin account
if (setting.getString(SystemSettingKey.SYS_ADMIN_ACCOUNT).equals(account.getName())) {
//throw exception if trying to set an expiration date for an admin account
throw new KapuaIllegalArgumentException("notAllowedExpirationDate", account.getExpirationDate().toString());
}
// check that expiration date is after all the children account
// if expiration date is null it means the account never expires, so it will be obviously later its children
AccountListResult childrenAccounts = findChildrenRecursively(account.getId());
// if child account expiration date is null it will be obviously after current account expiration date
if (childrenAccounts.getItems().stream().anyMatch(childAccount -> childAccount.getExpirationDate() == null || childAccount.getExpirationDate().after(account.getExpirationDate()))) {
throw new KapuaIllegalArgumentException(AccountAttributes.EXPIRATION_DATE, account.getExpirationDate() != null ? account.getExpirationDate().toString() : NO_EXPIRATION_DATE_SET);
}
}
//
// Verify unchanged parent account ID and parent account path
if (!Objects.equals(oldAccount.getScopeId(), account.getScopeId())) {
throw new KapuaIllegalArgumentException("account.scopeId", account.getScopeId().toStringId());
}
if (!oldAccount.getParentAccountPath().equals(account.getParentAccountPath())) {
throw new KapuaIllegalArgumentException("account.parentAccountPath", account.getParentAccountPath());
}
if (!oldAccount.getName().equals(account.getName())) {
throw new KapuaIllegalArgumentException("account.name", account.getName());
}
//
// Do update
return entityManagerSession.doTransactedAction(EntityManagerContainer.<Account>create().onResultHandler(em -> AccountDAO.update(em, account))
.onBeforeHandler(() -> {
entityCache.remove(null, account);
return null;
}));
}
@Override
//@RaiseServiceEvent
public void delete(KapuaId scopeId, KapuaId accountId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
ArgumentValidator.notNull(accountId, KapuaEntityAttributes.ENTITY_ID);
//
// Check Access
Actions action = Actions.delete;
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, action, scopeId));
//
// Check if it has children
if (!findChildAccountsTrusted(accountId).isEmpty()) {
throw new KapuaAccountException(KapuaAccountErrorCodes.OPERATION_NOT_ALLOWED, null, "This account cannot be deleted. Delete its child first.");
}
//
// Do delete
entityManagerSession.doTransactedAction(EntityManagerContainer.<Account>create().onResultHandler(em -> {
// Entity needs to be loaded in the context of the same EntityManger to be able to delete it afterwards
Account account = AccountDAO.find(em, scopeId, accountId);
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, accountId);
}
// do not allow deletion of the kapua admin account
SystemSetting settings = SystemSetting.getInstance();
if (settings.getString(SystemSettingKey.SYS_PROVISION_ACCOUNT_NAME).equals(account.getName())) {
throw new KapuaIllegalAccessException(action.name());
}
if (settings.getString(SystemSettingKey.SYS_ADMIN_USERNAME).equals(account.getName())) {
throw new KapuaIllegalAccessException(action.name());
}
return AccountDAO.delete(em, scopeId, accountId);
}).onAfterHandler((emptyParam) -> entityCache.remove(scopeId, accountId)));
}
@Override
public Account find(KapuaId scopeId, KapuaId accountId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
ArgumentValidator.notNull(accountId, KapuaEntityAttributes.ENTITY_ID);
//
// Check Access
checkAccountPermission(scopeId, accountId, AccountDomains.ACCOUNT_DOMAIN, Actions.read);
//
// Do find
return findById(accountId);
}
@Override
public Account find(KapuaId accountId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(accountId, KapuaEntityAttributes.ENTITY_ID);
Account account = findById(accountId);
//
// Check Access
if (account != null) {
checkAccountPermission(account.getScopeId(), account.getId(), AccountDomains.ACCOUNT_DOMAIN, Actions.read);
}
return account;
}
@Override
public Account findByName(String name) throws KapuaException {
//
// Argument validation
ArgumentValidator.notEmptyOrNull(name, "name");
//
// Do find
return entityManagerSession.doAction(EntityManagerContainer.<Account>create().onResultHandler(em -> {
Account account = AccountDAO.findByName(em, name);
if (account != null) {
checkAccountPermission(account.getScopeId(), account.getId(), AccountDomains.ACCOUNT_DOMAIN, Actions.read);
}
return account;
}
).onBeforeHandler(() -> {
Account account = (Account) ((NamedEntityCache) entityCache).get(null, name);
if (account != null) { // TODO: can this be put in the onAfterResultHandler ?
checkAccountPermission(account.getScopeId(), account.getId(), AccountDomains.ACCOUNT_DOMAIN, Actions.read);
}
return account;
}).onAfterHandler((entity) -> entityCache.put(entity)));
}
@Override
public AccountListResult findChildrenRecursively(KapuaId scopeId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, KapuaEntityAttributes.SCOPE_ID);
//
// Make sure account exists
Account account = findById(scopeId);
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, scopeId);
}
//
// Check Access
checkAccountPermission(account.getScopeId(), account.getId(), AccountDomains.ACCOUNT_DOMAIN, Actions.read, true);
return entityManagerSession.doAction(EntityManagerContainer.<AccountListResult>create().onResultHandler(em -> {
AccountListResult result = null;
TypedQuery<Account> q;
q = em.createNamedQuery("Account.findChildAccountsRecursive", Account.class);
q.setParameter("parentAccountPath", "\\" + account.getParentAccountPath() + "/%");
result = new AccountListResultImpl();
result.addItems(q.getResultList());
return result;
}));
}
@Override
public AccountListResult query(KapuaQuery query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.read, query.getScopeId()));
//
// Do query
return entityManagerSession.doAction(
EntityManagerContainer.<AccountListResult>create().onResultHandler(em -> AccountDAO.query(em, query))
);
}
@Override
public long count(KapuaQuery query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.read, query.getScopeId()));
//
// Do count
return entityManagerSession.doAction(
EntityManagerContainer.<Long>create().onResultHandler(em -> AccountDAO.count(em, query))
);
}
/**
* Find an {@link Account} without authorization checks.
*
* @param accountId
* @return
* @throws KapuaException
* @since 1.0.0
*/
private Account findById(KapuaId accountId) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(accountId, KapuaEntityAttributes.ENTITY_ID);
//
// Do find
return entityManagerSession.doAction(
EntityManagerContainer.<Account>create().onResultHandler(em -> AccountDAO.find(em, null, accountId))
.onBeforeHandler(() -> (Account) entityCache.get(null, accountId))
.onAfterHandler((entity) -> entityCache.put(entity))
);
}
private AccountListResult findChildAccountsTrusted(KapuaId accountId)
throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(accountId, KapuaEntityAttributes.ENTITY_ID);
ArgumentValidator.notNull(accountId.getId(), "accountId.id");
//
// Do find
return entityManagerSession.doAction(
EntityManagerContainer.<AccountListResult>create().onResultHandler(em -> AccountDAO.query(em,
new AccountQueryImpl(accountId)))
);
}
@Override
protected Map<String, Object> getConfigValues(Account entity) throws KapuaException {
return super.getConfigValues(entity.getId());
}
private void checkAccountPermission(KapuaId scopeId, KapuaId accountId, Domain domain, Actions action) throws KapuaException {
checkAccountPermission(scopeId, accountId, domain, action, false);
}
/**
* Checks if the current session can retrieve the {@link Account}, by both having an explicit permission or because
* it's looking for its own {@link Account}
*
* @param accountId The {@link KapuaId} of the {@link Account} to look for
*/
private void checkAccountPermission(KapuaId scopeId, KapuaId accountId, Domain domain, Actions action, boolean forwardable) throws KapuaException {
if (KapuaSecurityUtils.getSession().getScopeId().equals(accountId)) {
// I'm looking for myself, so let's check if I have the correct permission
authorizationService.checkPermission(permissionFactory.newPermission(domain, action, accountId, null, forwardable));
} else {
// I'm looking for another account, so I need to check the permission on the account scope
authorizationService.checkPermission(permissionFactory.newPermission(domain, action, scopeId, null, forwardable));
}
}
}