-
Notifications
You must be signed in to change notification settings - Fork 160
/
RoleServiceImpl.java
259 lines (216 loc) · 10.3 KB
/
RoleServiceImpl.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
/*******************************************************************************
* 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.role.shiro;
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaErrorCodes;
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.permission.Permission;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import org.eclipse.kapua.service.authorization.permission.shiro.PermissionValidator;
import org.eclipse.kapua.service.authorization.role.Role;
import org.eclipse.kapua.service.authorization.role.RoleAttributes;
import org.eclipse.kapua.service.authorization.role.RoleCreator;
import org.eclipse.kapua.service.authorization.role.RoleFactory;
import org.eclipse.kapua.service.authorization.role.RoleListResult;
import org.eclipse.kapua.service.authorization.role.RolePermissionCreator;
import org.eclipse.kapua.service.authorization.role.RolePermissionFactory;
import org.eclipse.kapua.service.authorization.role.RoleQuery;
import org.eclipse.kapua.service.authorization.role.RoleService;
import org.eclipse.kapua.service.authorization.shiro.AuthorizationEntityManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link RoleService} implementation.
*
* @since 1.0.0
*/
@KapuaProvider
public class RoleServiceImpl extends AbstractKapuaConfigurableResourceLimitedService<Role, RoleCreator, RoleService, RoleListResult, RoleQuery, RoleFactory> implements RoleService {
private static final Logger LOG = LoggerFactory.getLogger(RoleServiceImpl.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);
private static final RolePermissionFactory ROLE_PERMISSION_FACTORY = LOCATOR.getFactory(RolePermissionFactory.class);
public RoleServiceImpl() {
super(RoleService.class.getName(), AuthorizationDomains.ROLE_DOMAIN, AuthorizationEntityManagerFactory.getInstance(), RoleService.class, RoleFactory.class);
}
@Override
public Role create(RoleCreator roleCreator) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(roleCreator, "roleCreator");
ArgumentValidator.notNull(roleCreator.getScopeId(), "roleCreator.scopeId");
ArgumentValidator.validateEntityName(roleCreator.getName(), "roleCreator.name");
ArgumentValidator.notNull(roleCreator.getPermissions(), "roleCreator.permissions");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.write, roleCreator.getScopeId()));
//
// Check limits
if (allowedChildEntities(roleCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Roles");
}
//
// Check duplicate name
RoleQuery query = new RoleQueryImpl(roleCreator.getScopeId());
query.setPredicate(query.attributePredicate(RoleAttributes.NAME, roleCreator.getName()));
if (count(query) > 0) {
throw new KapuaDuplicateNameException(roleCreator.getName());
}
//
// If permission are created out of the role scope, check that the current user has the permission on the external scopeId.
if (roleCreator.getPermissions() != null) {
for (Permission p : roleCreator.getPermissions()) {
if (p.getTargetScopeId() == null || !p.getTargetScopeId().equals(roleCreator.getScopeId())) {
AUTHORIZATION_SERVICE.checkPermission(p);
}
}
}
//
// Check that the given permission matches the definition of the Domains.
PermissionValidator.validatePermissions(roleCreator.getPermissions());
//
// Do create
return entityManagerSession.onTransactedInsert(em -> {
Role role = RoleDAO.create(em, roleCreator);
if (!roleCreator.getPermissions().isEmpty()) {
for (Permission p : roleCreator.getPermissions()) {
RolePermissionCreator rolePermissionCreator = ROLE_PERMISSION_FACTORY.newCreator(roleCreator.getScopeId());
rolePermissionCreator.setRoleId(role.getId());
rolePermissionCreator.setPermission(p);
RolePermissionDAO.create(em, rolePermissionCreator);
}
}
return role;
});
}
@Override
public Role update(Role role) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(role, "role");
ArgumentValidator.notNull(role.getId(), "role.id");
ArgumentValidator.notNull(role.getScopeId(), "role.scopeId");
ArgumentValidator.validateEntityName(role.getName(), "role.name");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.write, role.getScopeId()));
//
// Check existence
if (find(role.getScopeId(), role.getId()) == null) {
throw new KapuaEntityNotFoundException(Role.TYPE, role.getId());
}
//
// Check duplicate name
RoleQuery query = new RoleQueryImpl(role.getScopeId());
query.setPredicate(
query.andPredicate(
query.attributePredicate(RoleAttributes.NAME, role.getName()),
query.attributePredicate(RoleAttributes.ENTITY_ID, role.getId(), Operator.NOT_EQUAL)
)
);
if (count(query) > 0) {
throw new KapuaDuplicateNameException(role.getName());
}
//
// Do update
return entityManagerSession.onTransactedResult(em -> RoleDAO.update(em, role));
}
@Override
public void delete(KapuaId scopeId, KapuaId roleId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(roleId, "roleId");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.delete, scopeId));
//
// Check existence
if (find(scopeId, roleId) == null) {
throw new KapuaEntityNotFoundException(Role.TYPE, roleId);
}
if (roleId.equals(KapuaId.ONE)) {
throw new KapuaException(KapuaErrorCodes.ADMIN_ROLE_DELETED_ERROR);
}
//
// Do delete
entityManagerSession.onTransactedAction(em -> RoleDAO.delete(em, scopeId, roleId));
}
@Override
public Role find(KapuaId scopeId, KapuaId roleId) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(roleId, "roleId");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.read, scopeId));
//
// Do find
return entityManagerSession.onResult(em -> RoleDAO.find(em, scopeId, roleId));
}
@Override
public RoleListResult query(KapuaQuery<Role> query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.read, query.getScopeId()));
//
// Do query
return entityManagerSession.onResult(em -> RoleDAO.query(em, query));
}
@Override
public long count(KapuaQuery<Role> query) throws KapuaException {
//
// Argument validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.read, query.getScopeId()));
//
// Do count
return entityManagerSession.onResult(em -> RoleDAO.count(em, query));
}
//@ListenServiceEvent(fromAddress="account")
public void onKapuaEvent(ServiceEvent kapuaEvent) throws KapuaException {
if (kapuaEvent == null) {
//service bus error. Throw some exception?
}
LOG.info("RoleService: received kapua event from {}, operation {}", kapuaEvent.getService(), kapuaEvent.getOperation());
if ("account".equals(kapuaEvent.getService()) && "delete".equals(kapuaEvent.getOperation())) {
deleteRoleByAccountId(kapuaEvent.getScopeId(), kapuaEvent.getEntityId());
}
}
private void deleteRoleByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
RoleQuery query = new RoleQueryImpl(accountId);
RoleListResult rolesToDelete = query(query);
for (Role r : rolesToDelete.getItems()) {
delete(r.getScopeId(), r.getId());
}
}
}