-
Notifications
You must be signed in to change notification settings - Fork 160
/
RolesPermissions.java
237 lines (214 loc) · 13.5 KB
/
RolesPermissions.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
/*******************************************************************************
* 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.app.api.resources.v1.resources;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.app.api.resources.v1.resources.model.CountResult;
import org.eclipse.kapua.app.api.resources.v1.resources.model.EntityId;
import org.eclipse.kapua.app.api.resources.v1.resources.model.ScopeId;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.query.predicate.AndPredicate;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.authorization.role.Role;
import org.eclipse.kapua.service.authorization.role.RolePermission;
import org.eclipse.kapua.service.authorization.role.RolePermissionAttributes;
import org.eclipse.kapua.service.authorization.role.RolePermissionCreator;
import org.eclipse.kapua.service.authorization.role.RolePermissionFactory;
import org.eclipse.kapua.service.authorization.role.RolePermissionListResult;
import org.eclipse.kapua.service.authorization.role.RolePermissionQuery;
import org.eclipse.kapua.service.authorization.role.RolePermissionService;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Api(value = "Roles", authorizations = {@Authorization(value = "kapuaAccessToken")})
@Path("{scopeId}/roles/{roleId}/permissions")
public class RolesPermissions extends AbstractKapuaResource {
private final KapuaLocator locator = KapuaLocator.getInstance();
private final RolePermissionService rolePermissionService = locator.getService(RolePermissionService.class);
private final RolePermissionFactory rolePermissionFactory = locator.getFactory(RolePermissionFactory.class);
/**
* Gets the {@link RolePermission} list in the scope.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param roleId The id of the {@link Role} in which to search results.
* @param domain The domain name to filter results.
* @param action The action to filter results.
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link RolePermissionListResult} of all the rolePermissions associated to the current selected scope.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionSimpleQuery", value = "Gets the RolePermission list in the scope", notes = "Returns the list of all the rolePermissions associated to the current selected scope.", response = RolePermissionListResult.class)
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public RolePermissionListResult simpleQuery(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The id of the Role to filter results.") @PathParam("roleId") EntityId roleId,
@ApiParam(value = "The domain name to filter results.") @QueryParam("name") String domain,
@ApiParam(value = "The action to filter results.") @QueryParam("action") Actions action,
@ApiParam(value = "The result set offset.", defaultValue = "0") @QueryParam("offset") @DefaultValue("0") int offset,
@ApiParam(value = "The result set limit.", defaultValue = "50") @QueryParam("limit") @DefaultValue("50") int limit) throws Exception {
RolePermissionQuery query = rolePermissionFactory.newQuery(scopeId);
AndPredicate andPredicate = query.andPredicate();
query.setPredicate(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
if (!Strings.isNullOrEmpty(domain)) {
andPredicate.and(query.attributePredicate(RolePermissionAttributes.PERMISSION_DOMAIN, domain));
}
if (action != null) {
andPredicate.and(query.attributePredicate(RolePermissionAttributes.PERMISSION_ACTION, action));
}
query.setPredicate(andPredicate);
query.setOffset(offset);
query.setLimit(limit);
return query(scopeId, roleId, query);
}
/**
* Queries the results with the given {@link RolePermissionQuery} parameter.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param roleId The {@link Role} id in which to search results.
* @param query The {@link RolePermissionQuery} to use to filter results.
* @return The {@link RolePermissionListResult} of all the result matching the given {@link RolePermissionQuery} parameter.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionQuery", value = "Queries the RolePermissions", notes = "Queries the RolePermissions with the given RolePermissionQuery parameter returning all matching RolePermissions", response = RolePermissionListResult.class)
@POST
@Path("_query")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public RolePermissionListResult query(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The Role id in which to search results.") @PathParam("roleId") EntityId roleId,
@ApiParam(value = "The RolePermissionQuery to use to filter results.", required = true) RolePermissionQuery query) throws Exception {
query.setScopeId(scopeId);
AndPredicate andPredicate = query.andPredicate();
andPredicate.and(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
if (query.getPredicate() != null) {
andPredicate.and(query.getPredicate());
}
query.setPredicate(andPredicate);
return rolePermissionService.query(query);
}
/**
* Counts the results with the given {@link RolePermissionQuery} parameter.
*
* @param scopeId The {@link ScopeId} in which to count results.
* @param roleId The {@link Role} id in which to count results.
* @param query The {@link RolePermissionQuery} to use to filter results.
* @return The count of all the result matching the given {@link RolePermissionQuery} parameter.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionCount", value = "Counts the RolePermissions", notes = "Counts the RolePermissions with the given RolePermissionQuery parameter returning the number of matching RolePermissions", response = CountResult.class)
@POST
@Path("_count")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public CountResult count(
@ApiParam(value = "The ScopeId in which to count results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The Role id in which to count results.") @PathParam("roleId") EntityId roleId,
@ApiParam(value = "The RolePermissionQuery to use to filter results.", required = true) RolePermissionQuery query) throws Exception {
query.setScopeId(scopeId);
query.setPredicate(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
return new CountResult(rolePermissionService.count(query));
}
/**
* Creates a new RolePermission based on the information provided in RolePermissionCreator
* parameter.
*
* @param scopeId The {@link ScopeId} in which to create the {@link RolePermission}
* @param roleId The {@link Role} id in which to create the RolePermission.
* @param rolePermissionCreator Provides the information for the new RolePermission to be created.
* @return The newly created RolePermission object.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionCreate", value = "Create a RolePermission", notes = "Creates a new RolePermission based on the information provided in RolePermissionCreator parameter.", response = RolePermission.class)
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public RolePermission create(
@ApiParam(value = "The ScopeId in which to create the RolePermission", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The Role id in which to create the RolePermission.", required = true) @PathParam("roleId") EntityId roleId,
@ApiParam(value = "Provides the information for the new RolePermission to be created", required = true) RolePermissionCreator rolePermissionCreator) throws Exception {
rolePermissionCreator.setScopeId(scopeId);
rolePermissionCreator.setRoleId(roleId);
return rolePermissionService.create(rolePermissionCreator);
}
/**
* Returns the RolePermission specified by the "rolePermissionId" path parameter.
*
* @param scopeId The {@link ScopeId} of the requested {@link RolePermission}.
* @param roleId The {@link Role} id of the requested {@link RolePermission}.
* @param rolePermissionId The id of the requested RolePermission.
* @return The requested RolePermission object.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionFind", value = "Get a RolePermission", notes = "Returns the RolePermission specified by the \"rolePermissionId\" path parameter.", response = RolePermission.class)
@GET
@Path("{rolePermissionId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public RolePermission find(
@ApiParam(value = "The ScopeId of the requested RolePermission.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "Specifies the RoleId for the requested RolePermission", required = true) @PathParam("roleId") EntityId roleId,
@ApiParam(value = "The id of the requested RolePermission", required = true) @PathParam("rolePermissionId") EntityId rolePermissionId) throws Exception {
RolePermissionQuery query = rolePermissionFactory.newQuery(scopeId);
AndPredicate andPredicate = query.andPredicate(
query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId),
query.attributePredicate(RolePermissionAttributes.ENTITY_ID, rolePermissionId)
);
query.setPredicate(andPredicate);
query.setOffset(0);
query.setLimit(1);
RolePermissionListResult results = rolePermissionService.query(query);
if (results.isEmpty()) {
throw new KapuaEntityNotFoundException(RolePermission.TYPE, rolePermissionId);
}
return results.getFirstItem();
}
/**
* Deletes the RolePermission specified by the "rolePermissionId" path parameter.
*
* @param scopeId The {@link ScopeId} of the {@link RolePermission} to delete.
* @param roleId The {@link Role} id of the {@link RolePermission} to delete.
* @param rolePermissionId The id of the RolePermission to be deleted.
* @return HTTP 200 if operation has completed successfully.
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@ApiOperation(nickname = "rolePermissionDelete", value = "Delete an RolePermission", notes = "Deletes the RolePermission specified by the \"rolePermissionId\" path parameter.")
@DELETE
@Path("{rolePermissionId}")
public Response deleteRolePermission(
@ApiParam(value = "The ScopeId of the RolePermission to delete.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "Specifies the Role Id for the requested RolePermission", required = true) @PathParam("roleId") EntityId roleId,
@ApiParam(value = "The id of the RolePermission to be deleted", required = true) @PathParam("rolePermissionId") EntityId rolePermissionId) throws Exception {
rolePermissionService.delete(scopeId, rolePermissionId);
return returnOk();
}
}