Skip to content

Package: UserServiceImpl

UserServiceImpl

nameinstructionbranchcomplexitylinemethod
UserServiceImpl()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
checkReadAccess(User)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
count(KapuaQuery)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(UserCreator)
M: 181 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 35 C: 0
0%
M: 1 C: 0
0%
delete(KapuaId, KapuaId)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
delete(User)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
deleteUserByAccountId(KapuaId, KapuaId)
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
find(KapuaId, KapuaId)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
findByExternalId(String)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
findByExternalUsername(String)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
findByName(String)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lambda$count$21(KapuaQuery, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$create$0(UserCreator)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$create$1(UserCreator)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$create$2(UserCreator)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$create$3(UserCreator, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$delete$8(KapuaId, KapuaId, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$delete$9(KapuaId, KapuaId, User)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$find$10(KapuaId, KapuaId, EntityManager)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$find$11(KapuaId, KapuaId)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$find$12(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByExternalId$16(String, EntityManager)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByExternalId$17(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByExternalUsername$18(String, EntityManager)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByExternalUsername$19(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByName$13(String, EntityManager)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByName$14(String)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findByName$15(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$query$20(KapuaQuery, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$4(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$5(User)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$6(User, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$update$7(User)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
onKapuaEvent(ServiceEvent)
M: 26 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
query(KapuaQuery)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
update(User)
M: 227 C: 0
0%
M: 32 C: 0
0%
M: 17 C: 0
0%
M: 40 C: 0
0%
M: 1 C: 0
0%
validateSelf(User)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
validateSystemUser(String)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.service.user.internal;
15:
16: import org.eclipse.kapua.KapuaDuplicateExternalIdException;
17: import org.eclipse.kapua.KapuaDuplicateNameException;
18: import org.eclipse.kapua.KapuaDuplicateNameInAnotherAccountError;
19: import org.eclipse.kapua.KapuaDuplicateExternalUsernameException;
20: import org.eclipse.kapua.KapuaEntityNotFoundException;
21: import org.eclipse.kapua.KapuaException;
22: import org.eclipse.kapua.KapuaIllegalArgumentException;
23: import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
24: import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
25: import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
26: import org.eclipse.kapua.commons.service.internal.cache.NamedEntityCache;
27: import org.eclipse.kapua.commons.setting.system.SystemSetting;
28: import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
29: import org.eclipse.kapua.commons.util.ArgumentValidator;
30: import org.eclipse.kapua.commons.util.CommonsValidationRegex;
31: import org.eclipse.kapua.event.ServiceEvent;
32: import org.eclipse.kapua.locator.KapuaProvider;
33: import org.eclipse.kapua.model.domain.Actions;
34: import org.eclipse.kapua.model.id.KapuaId;
35: import org.eclipse.kapua.model.query.KapuaQuery;
36: import org.eclipse.kapua.service.authorization.AuthorizationService;
37: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
38: import org.eclipse.kapua.service.user.User;
39: import org.eclipse.kapua.service.user.UserAttributes;
40: import org.eclipse.kapua.service.user.UserCreator;
41: import org.eclipse.kapua.service.user.UserDomains;
42: import org.eclipse.kapua.service.user.UserFactory;
43: import org.eclipse.kapua.service.user.UserListResult;
44: import org.eclipse.kapua.service.user.UserQuery;
45: import org.eclipse.kapua.service.user.UserService;
46: import org.eclipse.kapua.service.user.UserStatus;
47: import org.eclipse.kapua.service.user.UserType;
48: import org.slf4j.Logger;
49: import org.slf4j.LoggerFactory;
50:
51: import javax.inject.Inject;
52: import java.util.Objects;
53:
54: /**
55: * {@link UserService} implementation.
56: *
57: * @since 1.0.0
58: */
59: @KapuaProvider
60: public class UserServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<User, UserCreator, UserService, UserListResult, UserQuery, UserFactory> implements UserService {
61:
62: private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
63:
64: @Inject
65: private AuthorizationService authorizationService;
66:
67: @Inject
68: private PermissionFactory permissionFactory;
69:
70: /**
71: * Constructor.
72: *
73: * @since 1.0.0
74: */
75: public UserServiceImpl() {
76: super(UserService.class.getName(),
77: UserDomains.USER_DOMAIN,
78: UserEntityManagerFactory.getInstance(),
79: UserCacheFactory.getInstance(),
80: UserService.class,
81: UserFactory.class);
82: }
83:
84: @Override
85: public User create(UserCreator userCreator) throws KapuaException {
86: //
87: // Argument Validation
88: ArgumentValidator.notNull(userCreator.getScopeId().getId(), "userCreator.scopeId");
89: ArgumentValidator.notEmptyOrNull(userCreator.getName(), "userCreator.name");
90: ArgumentValidator.match(userCreator.getName(), CommonsValidationRegex.NAME_REGEXP, "userCreator.name");
91: ArgumentValidator.lengthRange(userCreator.getName(), 3, 255, "userCreator.name");
92: ArgumentValidator.match(userCreator.getEmail(), CommonsValidationRegex.EMAIL_REGEXP, "userCreator.email");
93: ArgumentValidator.notNull(userCreator.getStatus(), "userCreator.status");
94:
95: ArgumentValidator.notNull(userCreator.getUserType(), "userCreator.userType");
96:• if (userCreator.getUserType() == UserType.EXTERNAL) {
97:• if (userCreator.getExternalId() != null) {
98: ArgumentValidator.notEmptyOrNull(userCreator.getExternalId(), "userCreator.externalId");
99: ArgumentValidator.lengthRange(userCreator.getExternalId(), 3, 255, "userCreator.externalId");
100: } else {
101: ArgumentValidator.notEmptyOrNull(userCreator.getExternalUsername(), "userCreator.externalUsername");
102: ArgumentValidator.lengthRange(userCreator.getExternalUsername(), 3, 255, "userCreator.externalUsername");
103: }
104:• } else if (userCreator.getUserType() == UserType.INTERNAL) {
105: ArgumentValidator.isEmptyOrNull(userCreator.getExternalId(), "userCreator.externalId");
106: ArgumentValidator.isEmptyOrNull(userCreator.getExternalUsername(), "userCreator.externalUsername");
107: }
108:
109: //
110: // Check Access
111: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.write, userCreator.getScopeId()));
112:
113: //
114: // Check entity limit
115: checkAllowedEntities(userCreator.getScopeId(), "Users");
116:
117: //
118: // Check duplicate name
119: UserQuery query = new UserQueryImpl(userCreator.getScopeId());
120: query.setPredicate(query.attributePredicate(UserAttributes.NAME, userCreator.getName()));
121:• if (count(query) > 0) {
122: throw new KapuaDuplicateNameException(userCreator.getName());
123: }
124:
125: User userByName = KapuaSecurityUtils.doPrivileged(() -> findByName(userCreator.getName()));
126:• if (userByName != null) {
127: throw new KapuaDuplicateNameInAnotherAccountError(userCreator.getName());
128: }
129:
130:• if (userCreator.getUserType() == UserType.EXTERNAL) {
131: // Check duplicate externalId
132:• if (userCreator.getExternalId() != null) {
133: User userByExternalId = KapuaSecurityUtils.doPrivileged(() -> findByExternalId(userCreator.getExternalId()));
134:• if (userByExternalId != null) {
135: throw new KapuaDuplicateExternalIdException(userCreator.getExternalId());
136: }
137: }
138:
139: // Check duplicate externalUsername
140:• if (userCreator.getExternalUsername() != null) {
141: User userByExternalPreferredUserame = KapuaSecurityUtils.doPrivileged(() -> findByExternalId(userCreator.getExternalUsername()));
142:• if (userByExternalPreferredUserame != null) {
143: throw new KapuaDuplicateExternalUsernameException(userCreator.getExternalUsername());
144: }
145: }
146: }
147:
148: //
149: // Do create
150: return entityManagerSession.doTransactedAction(EntityManagerContainer.<User>create().onResultHandler(em -> UserDAO.create(em, userCreator)));
151: }
152:
153: @Override
154: //@RaiseServiceEvent
155: public User update(User user) throws KapuaException {
156: //
157: // Argument validation
158: ArgumentValidator.notNull(user.getId(), "user.id");
159: ArgumentValidator.notNull(user.getScopeId(), "user.scopeId");
160: ArgumentValidator.notEmptyOrNull(user.getName(), "user.name");
161: ArgumentValidator.match(user.getName(), CommonsValidationRegex.NAME_REGEXP, "user.name");
162: ArgumentValidator.lengthRange(user.getName(), 3, 255, "user.name");
163: ArgumentValidator.match(user.getEmail(), CommonsValidationRegex.EMAIL_REGEXP, "user.email");
164: ArgumentValidator.notNull(user.getStatus(), "user.status");
165: ArgumentValidator.notNull(user.getUserType(), "user.userType");
166:
167:• if (user.getUserType() == UserType.EXTERNAL) {
168:• if (user.getExternalId() != null) {
169: ArgumentValidator.notEmptyOrNull(user.getExternalId(), "user.externalId");
170: ArgumentValidator.lengthRange(user.getExternalId(), 3, 255, "user.externalId");
171: } else {
172: ArgumentValidator.notEmptyOrNull(user.getExternalUsername(), "user.externalUsername");
173: ArgumentValidator.lengthRange(user.getExternalUsername(), 3, 255, "user.externalUsername");
174: }
175:• } else if (user.getUserType() == UserType.INTERNAL) {
176: ArgumentValidator.isEmptyOrNull(user.getExternalId(), "user.externalId");
177: ArgumentValidator.isEmptyOrNull(user.getExternalUsername(), "user.externalUsername");
178: }
179:
180: //
181: // Check Access
182: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.write, user.getScopeId()));
183:
184: //
185: // Check existence
186: User currentUser = find(user.getScopeId(), user.getId());
187:• if (currentUser == null) {
188: throw new KapuaEntityNotFoundException(User.TYPE, user.getId());
189: }
190:
191: //
192: // Check action on Sys admin user
193:• if (user.getExpirationDate() != null || !currentUser.getName().equals(user.getName())) {
194: //
195: // Check not deleting environment admin
196: validateSystemUser(user.getName());
197: }
198:
199: //
200: // Check disabling on logged user
201:• if (user.getId().equals(KapuaSecurityUtils.getSession().getUserId())) {
202:• if (user.getStatus().equals(UserStatus.DISABLED)) {
203: throw new KapuaIllegalArgumentException("user.status", user.getStatus().name());
204: }
205: }
206:
207: //
208: // Check not updatable fields
209:
210: // User.userType
211:• if (!Objects.equals(currentUser.getUserType(), user.getUserType())) {
212: throw new KapuaIllegalArgumentException("user.userType", user.getUserType().toString());
213: }
214:
215: // User.name
216:• if (!Objects.equals(currentUser.getName(), user.getName())) {
217: throw new KapuaIllegalArgumentException("user.name", user.getName());
218: }
219:
220: //
221: // Check duplicates
222:
223: // User.externalId
224:• if (user.getExternalId() != null) {
225: User userByExternalId = KapuaSecurityUtils.doPrivileged(() -> findByExternalId(user.getExternalId()));
226:• if (userByExternalId != null && !userByExternalId.getId().equals(user.getId())) {
227: throw new KapuaDuplicateExternalIdException(user.getExternalId());
228: }
229: }
230:
231: // User.externalUsername
232:• if (user.getExternalUsername() != null) {
233: User userByExternalPreferredUsername = KapuaSecurityUtils.doPrivileged(() -> findByExternalId(user.getExternalUsername()));
234:• if (userByExternalPreferredUsername != null && !userByExternalPreferredUsername.getId().equals(user.getId())) {
235: throw new KapuaDuplicateExternalUsernameException(user.getExternalUsername());
236: }
237: }
238:
239: //
240: // Do update
241: return entityManagerSession.doTransactedAction(EntityManagerContainer.<User>create().onResultHandler(em -> UserDAO.update(em, user))
242: .onBeforeHandler(() -> {
243: entityCache.remove(null, user);
244: return null;
245: }));
246: }
247:
248: @Override
249: public void delete(User user) throws KapuaException {
250: //
251: // Argument Validation
252: ArgumentValidator.notNull(user, "user");
253:
254: //
255: // Do delete
256: delete(user.getScopeId(), user.getId());
257: }
258:
259: @Override
260: //@RaiseServiceEvent
261: public void delete(KapuaId scopeId, KapuaId userId) throws KapuaException {
262: //
263: // Argument validation
264: ArgumentValidator.notNull(userId.getId(), "user.id");
265: ArgumentValidator.notNull(scopeId.getId(), "user.scopeId");
266:
267: //
268: // Check Access
269: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.delete, scopeId));
270:
271: //
272: // Check existence
273: User user = find(scopeId, userId);
274:• if (user == null) {
275: throw new KapuaEntityNotFoundException(User.TYPE, userId);
276: }
277:
278: //
279: // Check not deleting environment admin
280: validateSystemUser(user.getName());
281:
282: //
283: // Check not deleting self
284: validateSelf(user);
285:
286: //
287: // Do delete
288: entityManagerSession.doTransactedAction(EntityManagerContainer.<User>create().onResultHandler(em -> UserDAO.delete(em, scopeId, userId))
289: .onAfterHandler((emptyParam) -> entityCache.remove(scopeId, userId)));
290: }
291:
292: @Override
293: public User find(KapuaId scopeId, KapuaId userId)
294: throws KapuaException {
295: // Validation of the fields
296: ArgumentValidator.notNull(scopeId, "scopeId");
297: ArgumentValidator.notNull(userId, "userId");
298:
299: //
300: // Check Access
301: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.read, scopeId));
302:
303: // Do the find
304: return entityManagerSession.doAction(EntityManagerContainer.<User>create().onResultHandler(em -> UserDAO.find(em, scopeId, userId))
305: .onBeforeHandler(() -> (User) entityCache.get(scopeId, userId))
306: .onAfterHandler((entity) -> entityCache.put(entity))
307: );
308: }
309:
310: @Override
311: public User findByName(String name) throws KapuaException {
312: //
313: // Validation of the fields
314: ArgumentValidator.notEmptyOrNull(name, "name");
315:
316: //
317: // Do the find
318: return entityManagerSession.doAction(EntityManagerContainer.<User>create().onResultHandler(em -> checkReadAccess(UserDAO.findByName(em, name)))
319: .onBeforeHandler(() -> checkReadAccess((User) ((NamedEntityCache) entityCache).get(null, name)))
320: .onAfterHandler((entity) -> entityCache.put(entity)));
321: }
322:
323: @Override
324: public User findByExternalId(String externalId) throws KapuaException {
325: //
326: // Validation of the fields
327: ArgumentValidator.notEmptyOrNull(externalId, "externalId");
328:
329: //
330: // Do the find
331: return entityManagerSession.doAction(EntityManagerContainer.<User>create().onResultHandler(em -> checkReadAccess(UserDAO.findByExternalId(em, externalId)))
332: .onAfterHandler((entity) -> entityCache.put(entity)));
333: }
334:
335: @Override
336: public User findByExternalUsername(String externalUsername) throws KapuaException {
337: //
338: // Validation of the fields
339: ArgumentValidator.notEmptyOrNull(externalUsername, "externalUsername");
340:
341: //
342: // Do the find
343: return entityManagerSession.doAction(EntityManagerContainer.<User>create().onResultHandler(em -> checkReadAccess(UserDAO.findByExternalUsername(em, externalUsername)))
344: .onAfterHandler((entity) -> entityCache.put(entity)));
345: }
346:
347: @Override
348: public UserListResult query(KapuaQuery query)
349: throws KapuaException {
350: //
351: // Argument Validation
352: ArgumentValidator.notNull(query, "query");
353:
354: //
355: // Check Access
356: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.read, query.getScopeId()));
357:
358: //
359: // Do query
360: return entityManagerSession.doAction(EntityManagerContainer.<UserListResult>create().onResultHandler(em -> UserDAO.query(em, query)));
361: }
362:
363: @Override
364: public long count(KapuaQuery query)
365: throws KapuaException {
366: //
367: // Argument Validator
368: ArgumentValidator.notNull(query, "query");
369:
370: //
371: // Check Access
372: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.read, query.getScopeId()));
373:
374: //
375: // Do count
376: return entityManagerSession.doAction(EntityManagerContainer.<Long>create().onResultHandler(em -> UserDAO.count(em, query)));
377: }
378:
379: // -----------------------------------------------------------------------------------------
380: //
381: // Private Methods
382: //
383: // -----------------------------------------------------------------------------------------
384:
385: private User checkReadAccess(User user) throws KapuaException {
386:• if (user != null) {
387: authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.read, user.getScopeId()));
388: }
389: return user;
390: }
391:
392: private void validateSystemUser(String name) throws KapuaException {
393: String adminUsername = SystemSetting.getInstance().getString(SystemSettingKey.SYS_ADMIN_USERNAME);
394:
395:• if (adminUsername.equals(name)) {
396: throw new KapuaIllegalArgumentException("name", adminUsername);
397: }
398: }
399:
400: private void validateSelf(User user) throws KapuaException {
401:• if (user.getId().equals(KapuaSecurityUtils.getSession().getUserId())) {
402: throw new KapuaIllegalArgumentException("name", user.getName());
403: }
404: }
405:
406: //@ListenServiceEvent(fromAddress = "account")
407: public void onKapuaEvent(ServiceEvent kapuaEvent) throws KapuaException {
408:• if (kapuaEvent == null) {
409: // service bus error. Throw some exception?
410: }
411: LOGGER.info("UserService: received kapua event from {}, operation {}", kapuaEvent.getService(), kapuaEvent.getOperation());
412:• if ("account".equals(kapuaEvent.getService()) && "delete".equals(kapuaEvent.getOperation())) {
413: deleteUserByAccountId(kapuaEvent.getScopeId(), kapuaEvent.getEntityId());
414: }
415: }
416:
417: private void deleteUserByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
418: UserQuery query = new UserQueryImpl(accountId);
419: UserListResult usersToDelete = query(query);
420:
421:• for (User u : usersToDelete.getItems()) {
422: delete(u.getScopeId(), u.getId());
423: }
424: }
425:
426: }