-
Notifications
You must be signed in to change notification settings - Fork 160
/
GroupServiceImpl.java
219 lines (183 loc) · 8.6 KB
/
GroupServiceImpl.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
/*******************************************************************************
* Copyright (c) 2016, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.authorization.group.shiro;
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.event.ServiceEvent;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
import org.eclipse.kapua.service.authorization.AuthorizationDomains;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.group.Group;
import org.eclipse.kapua.service.authorization.group.GroupAttributes;
import org.eclipse.kapua.service.authorization.group.GroupCreator;
import org.eclipse.kapua.service.authorization.group.GroupFactory;
import org.eclipse.kapua.service.authorization.group.GroupListResult;
import org.eclipse.kapua.service.authorization.group.GroupQuery;
import org.eclipse.kapua.service.authorization.group.GroupService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import org.eclipse.kapua.service.authorization.shiro.AuthorizationEntityManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link GroupService} implementation.
*
* @since 1.0.0
*/
@KapuaProvider
public class GroupServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<Group, GroupCreator, GroupService, GroupListResult, GroupQuery, GroupFactory> implements GroupService {
private static final Logger LOG = LoggerFactory.getLogger(GroupServiceImpl.class);
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final AuthorizationService AUTHORIZATION_SERVICE = LOCATOR.getService(AuthorizationService.class);
private static final PermissionFactory PERMISSION_FACTORY = LOCATOR.getFactory(PermissionFactory.class);
public GroupServiceImpl() {
super(GroupService.class.getName(), AuthorizationDomains.GROUP_DOMAIN, AuthorizationEntityManagerFactory.getInstance(), GroupService.class, GroupFactory.class);
}
@Override
public Group create(GroupCreator groupCreator) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(groupCreator, "groupCreator");
ArgumentValidator.notNull(groupCreator.getScopeId(), "roleCreator.scopeId");
ArgumentValidator.validateEntityName(groupCreator.getName(), "groupCreator.name");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.write, groupCreator.getScopeId()));
//
// Check limits
if (allowedChildEntities(groupCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Groups");
}
//
// Check duplicate name
GroupQuery query = new GroupQueryImpl(groupCreator.getScopeId());
query.setPredicate(query.attributePredicate(GroupAttributes.NAME, groupCreator.getName()));
if (count(query) > 0) {
throw new KapuaDuplicateNameException(groupCreator.getName());
}
//
// Do create
return entityManagerSession.onTransactedInsert(em -> GroupDAO.create(em, groupCreator));
}
@Override
public Group update(Group group) throws KapuaException {
//
// Argument validator
ArgumentValidator.notNull(group, "group");
ArgumentValidator.notNull(group.getId(), "group.id");
ArgumentValidator.notNull(group.getScopeId(), "group.scopeId");
ArgumentValidator.validateEntityName(group.getName(), "group.name");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.write, group.getScopeId()));
//
// Check existence
if (find(group.getScopeId(), group.getId()) == null) {
throw new KapuaEntityNotFoundException(Group.TYPE, group.getId());
}
//
// Check duplicate name
GroupQuery query = new GroupQueryImpl(group.getScopeId());
query.setPredicate(
query.andPredicate(
query.attributePredicate(GroupAttributes.NAME, group.getName()),
query.attributePredicate(GroupAttributes.ENTITY_ID, group.getId(), Operator.NOT_EQUAL)
)
);
if (count(query) > 0) {
throw new KapuaDuplicateNameException(group.getName());
}
//
// Do update
return entityManagerSession.onTransactedResult(em -> GroupDAO.update(em, group));
}
@Override
public void delete(KapuaId scopeId, KapuaId groupId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(groupId, "groupId");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.delete, scopeId));
//
// Check existence
if (find(scopeId, groupId) == null) {
throw new KapuaEntityNotFoundException(Group.TYPE, groupId);
}
//
// Do delete
entityManagerSession.onTransactedAction(em -> GroupDAO.delete(em, scopeId, groupId));
}
@Override
public Group find(KapuaId scopeId, KapuaId groupId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(groupId, "groupId");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, scopeId));
//
// Do find
return entityManagerSession.onResult(em -> GroupDAO.find(em, scopeId, groupId));
}
@Override
public GroupListResult query(KapuaQuery<Group> query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, query.getScopeId()));
//
// Do query
return entityManagerSession.onResult(em -> GroupDAO.query(em, query));
}
@Override
public long count(KapuaQuery<Group> query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.read, query.getScopeId()));
//
// Do count
return entityManagerSession.onResult(em -> GroupDAO.count(em, query));
}
//@ListenServiceEvent(fromAddress="account")
public void onKapuaEvent(ServiceEvent kapuaEvent) throws KapuaException {
if (kapuaEvent == null) {
//service bus error. Throw some exception?
}
LOG.info("GroupService: received kapua event from {}, operation {}", kapuaEvent.getService(), kapuaEvent.getOperation());
if ("account".equals(kapuaEvent.getService()) && "delete".equals(kapuaEvent.getOperation())) {
deleteGroupByAccountId(kapuaEvent.getScopeId(), kapuaEvent.getEntityId());
}
}
private void deleteGroupByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
GroupQuery query = new GroupQueryImpl(accountId);
GroupListResult groupsToDelete = query(query);
for (Group g : groupsToDelete.getItems()) {
delete(g.getScopeId(), g.getId());
}
}
}