Skip to content

Package: GroupServiceImpl

GroupServiceImpl

nameinstructionbranchcomplexitylinemethod
GroupServiceImpl()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
count(KapuaQuery)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(GroupCreator)
M: 58 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
delete(KapuaId, KapuaId)
M: 34 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
deleteGroupByAccountId(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: 23 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$5(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(GroupCreator, 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$2(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$3(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$query$4(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$1(Group, 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%
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: 20 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: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
update(Group)
M: 87 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 15 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: *******************************************************************************/
13: package org.eclipse.kapua.service.authorization.group.shiro;
14:
15: import org.eclipse.kapua.KapuaDuplicateNameException;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
19: import org.eclipse.kapua.commons.util.ArgumentValidator;
20: import org.eclipse.kapua.event.ServiceEvent;
21: import org.eclipse.kapua.locator.KapuaLocator;
22: import org.eclipse.kapua.locator.KapuaProvider;
23: import org.eclipse.kapua.model.domain.Actions;
24: import org.eclipse.kapua.model.id.KapuaId;
25: import org.eclipse.kapua.model.query.KapuaQuery;
26: import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
27: import org.eclipse.kapua.service.authorization.AuthorizationDomains;
28: import org.eclipse.kapua.service.authorization.AuthorizationService;
29: import org.eclipse.kapua.service.authorization.group.Group;
30: import org.eclipse.kapua.service.authorization.group.GroupAttributes;
31: import org.eclipse.kapua.service.authorization.group.GroupCreator;
32: import org.eclipse.kapua.service.authorization.group.GroupFactory;
33: import org.eclipse.kapua.service.authorization.group.GroupListResult;
34: import org.eclipse.kapua.service.authorization.group.GroupQuery;
35: import org.eclipse.kapua.service.authorization.group.GroupService;
36: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
37: import org.eclipse.kapua.service.authorization.shiro.AuthorizationEntityManagerFactory;
38: import org.slf4j.Logger;
39: import org.slf4j.LoggerFactory;
40:
41: import javax.inject.Inject;
42:
43: /**
44: * {@link GroupService} implementation.
45: *
46: * @since 1.0.0
47: */
48: @KapuaProvider
49: public class GroupServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<Group, GroupCreator, GroupService, GroupListResult, GroupQuery, GroupFactory> implements GroupService {
50:
51: private static final Logger LOG = LoggerFactory.getLogger(GroupServiceImpl.class);
52:
53: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
54: @Inject
55: private AuthorizationService authorizationService;
56: @Inject
57: private PermissionFactory permissionFactory;
58:
59: public GroupServiceImpl() {
60: super(GroupService.class.getName(), AuthorizationDomains.GROUP_DOMAIN, AuthorizationEntityManagerFactory.getInstance(), GroupService.class, GroupFactory.class);
61: }
62:
63: @Override
64: public Group create(GroupCreator groupCreator) throws KapuaException {
65: //
66: // Argument validation
67: ArgumentValidator.notNull(groupCreator, "groupCreator");
68: ArgumentValidator.notNull(groupCreator.getScopeId(), "roleCreator.scopeId");
69: ArgumentValidator.validateEntityName(groupCreator.getName(), "groupCreator.name");
70:
71: //
72: // Check Access
73: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.write, groupCreator.getScopeId()));
74:
75: //
76: // Check entity limit
77: checkAllowedEntities(groupCreator.getScopeId(), "Groups");
78:
79: //
80: // Check duplicate name
81: GroupQuery query = new GroupQueryImpl(groupCreator.getScopeId());
82: query.setPredicate(query.attributePredicate(GroupAttributes.NAME, groupCreator.getName()));
83:
84:• if (count(query) > 0) {
85: throw new KapuaDuplicateNameException(groupCreator.getName());
86: }
87:
88: //
89: // Do create
90: return entityManagerSession.doTransactedAction(em -> GroupDAO.create(em, groupCreator));
91: }
92:
93: @Override
94: public Group update(Group group) throws KapuaException {
95: //
96: // Argument validator
97: ArgumentValidator.notNull(group, "group");
98: ArgumentValidator.notNull(group.getId(), "group.id");
99: ArgumentValidator.notNull(group.getScopeId(), "group.scopeId");
100: ArgumentValidator.validateEntityName(group.getName(), "group.name");
101:
102: //
103: // Check Access
104: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.write, group.getScopeId()));
105:
106: //
107: // Check existence
108:• if (find(group.getScopeId(), group.getId()) == null) {
109: throw new KapuaEntityNotFoundException(Group.TYPE, group.getId());
110: }
111:
112: //
113: // Check duplicate name
114: GroupQuery query = new GroupQueryImpl(group.getScopeId());
115: query.setPredicate(
116: query.andPredicate(
117: query.attributePredicate(GroupAttributes.NAME, group.getName()),
118: query.attributePredicate(GroupAttributes.ENTITY_ID, group.getId(), Operator.NOT_EQUAL)
119: )
120: );
121:
122:• if (count(query) > 0) {
123: throw new KapuaDuplicateNameException(group.getName());
124: }
125:
126: //
127: // Do update
128: return entityManagerSession.doTransactedAction(em -> GroupDAO.update(em, group));
129: }
130:
131: @Override
132: public void delete(KapuaId scopeId, KapuaId groupId) throws KapuaException {
133: //
134: // Argument validation
135: ArgumentValidator.notNull(scopeId, "scopeId");
136: ArgumentValidator.notNull(groupId, "groupId");
137:
138: //
139: // Check Access
140: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.delete, scopeId));
141:
142: //
143: // Check existence
144:• if (find(scopeId, groupId) == null) {
145: throw new KapuaEntityNotFoundException(Group.TYPE, groupId);
146: }
147:
148: //
149: // Do delete
150: entityManagerSession.doTransactedAction(em -> GroupDAO.delete(em, scopeId, groupId));
151: }
152:
153: @Override
154: public Group find(KapuaId scopeId, KapuaId groupId) throws KapuaException {
155: //
156: // Argument validation
157: ArgumentValidator.notNull(scopeId, "scopeId");
158: ArgumentValidator.notNull(groupId, "groupId");
159:
160: //
161: // Check Access
162: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, scopeId));
163:
164: //
165: // Do find
166: return entityManagerSession.doAction(em -> GroupDAO.find(em, scopeId, groupId));
167: }
168:
169: @Override
170: public GroupListResult query(KapuaQuery query) throws KapuaException {
171: //
172: // Argument validation
173: ArgumentValidator.notNull(query, "query");
174:
175: //
176: // Check Access
177: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, query.getScopeId()));
178:
179: //
180: // Do query
181: return entityManagerSession.doAction(em -> GroupDAO.query(em, query));
182: }
183:
184: @Override
185: public long count(KapuaQuery query) throws KapuaException {
186: //
187: // Argument validation
188: ArgumentValidator.notNull(query, "query");
189:
190: //
191: // Check Access
192: authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, query.getScopeId()));
193:
194: //
195: // Do count
196: return entityManagerSession.doAction(em -> GroupDAO.count(em, query));
197: }
198:
199: //@ListenServiceEvent(fromAddress="account")
200: public void onKapuaEvent(ServiceEvent kapuaEvent) throws KapuaException {
201:• if (kapuaEvent == null) {
202: //service bus error. Throw some exception?
203: }
204:
205: LOG.info("GroupService: received kapua event from {}, operation {}", kapuaEvent.getService(), kapuaEvent.getOperation());
206:• if ("account".equals(kapuaEvent.getService()) && "delete".equals(kapuaEvent.getOperation())) {
207: deleteGroupByAccountId(kapuaEvent.getScopeId(), kapuaEvent.getEntityId());
208: }
209: }
210:
211: private void deleteGroupByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
212: GroupQuery query = new GroupQueryImpl(accountId);
213:
214: GroupListResult groupsToDelete = query(query);
215:
216:• for (Group g : groupsToDelete.getItems()) {
217: delete(g.getScopeId(), g.getId());
218: }
219: }
220: }