Skip to content

Package: RolesPermissions

RolesPermissions

nameinstructionbranchcomplexitylinemethod
RolesPermissions()
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%
count(ScopeId, EntityId, RolePermissionQuery)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(ScopeId, EntityId, RolePermissionCreator)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
deleteRolePermission(ScopeId, EntityId, EntityId)
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%
find(ScopeId, EntityId, EntityId)
M: 54 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, EntityId, RolePermissionQuery)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, EntityId, String, Actions, String, SortOrder, int, int)
M: 60 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 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.app.api.resources.v1.resources;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
19: import org.eclipse.kapua.app.api.core.model.CountResult;
20: import org.eclipse.kapua.app.api.core.model.EntityId;
21: import org.eclipse.kapua.app.api.core.model.ScopeId;
22: import org.eclipse.kapua.locator.KapuaLocator;
23: import org.eclipse.kapua.model.KapuaEntityAttributes;
24: import org.eclipse.kapua.model.domain.Actions;
25: import org.eclipse.kapua.model.query.SortOrder;
26: import org.eclipse.kapua.model.query.predicate.AndPredicate;
27: import org.eclipse.kapua.service.KapuaService;
28: import org.eclipse.kapua.service.authorization.role.Role;
29: import org.eclipse.kapua.service.authorization.role.RolePermission;
30: import org.eclipse.kapua.service.authorization.role.RolePermissionAttributes;
31: import org.eclipse.kapua.service.authorization.role.RolePermissionCreator;
32: import org.eclipse.kapua.service.authorization.role.RolePermissionFactory;
33: import org.eclipse.kapua.service.authorization.role.RolePermissionListResult;
34: import org.eclipse.kapua.service.authorization.role.RolePermissionQuery;
35: import org.eclipse.kapua.service.authorization.role.RolePermissionService;
36:
37: import javax.ws.rs.Consumes;
38: import javax.ws.rs.DELETE;
39: import javax.ws.rs.DefaultValue;
40: import javax.ws.rs.GET;
41: import javax.ws.rs.POST;
42: import javax.ws.rs.Path;
43: import javax.ws.rs.PathParam;
44: import javax.ws.rs.Produces;
45: import javax.ws.rs.QueryParam;
46: import javax.ws.rs.core.MediaType;
47: import javax.ws.rs.core.Response;
48:
49: @Path("{scopeId}/roles/{roleId}/permissions")
50: public class RolesPermissions extends AbstractKapuaResource {
51:
52: private final KapuaLocator locator = KapuaLocator.getInstance();
53: private final RolePermissionService rolePermissionService = locator.getService(RolePermissionService.class);
54: private final RolePermissionFactory rolePermissionFactory = locator.getFactory(RolePermissionFactory.class);
55:
56: /**
57: * Gets the {@link RolePermission} list in the scope.
58: *
59: * @param scopeId The {@link ScopeId} in which to search results.
60: * @param roleId The id of the {@link Role} in which to search results.
61: * @param domain The domain name to filter results.
62: * @param action The action to filter results.
63: * @param sortParam The name of the parameter that will be used as a sorting key
64: * @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
65: * @param offset The result set offset.
66: * @param limit The result set limit.
67: * @return The {@link RolePermissionListResult} of all the rolePermissions associated to the current selected scope.
68: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
69: * @since 1.0.0
70: */
71: @GET
72: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
73: public RolePermissionListResult simpleQuery(
74: @PathParam("scopeId") ScopeId scopeId,
75: @PathParam("roleId") EntityId roleId,
76: @QueryParam("name") String domain,
77: @QueryParam("action") Actions action,
78: @QueryParam("sortParam") String sortParam,
79: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
80: @QueryParam("offset") @DefaultValue("0") int offset,
81: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
82: RolePermissionQuery query = rolePermissionFactory.newQuery(scopeId);
83:
84: AndPredicate andPredicate = query.andPredicate();
85: query.setPredicate(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
86:• if (!Strings.isNullOrEmpty(domain)) {
87: andPredicate.and(query.attributePredicate(RolePermissionAttributes.PERMISSION_DOMAIN, domain));
88: }
89:• if (action != null) {
90: andPredicate.and(query.attributePredicate(RolePermissionAttributes.PERMISSION_ACTION, action));
91: }
92:• if (!Strings.isNullOrEmpty(sortParam)) {
93: query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
94: }
95: query.setPredicate(andPredicate);
96:
97: query.setOffset(offset);
98: query.setLimit(limit);
99:
100: return query(scopeId, roleId, query);
101: }
102:
103: /**
104: * Queries the results with the given {@link RolePermissionQuery} parameter.
105: *
106: * @param scopeId The {@link ScopeId} in which to search results.
107: * @param roleId The {@link Role} id in which to search results.
108: * @param query The {@link RolePermissionQuery} to use to filter results.
109: * @return The {@link RolePermissionListResult} of all the result matching the given {@link RolePermissionQuery} parameter.
110: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
111: * @since 1.0.0
112: */
113: @POST
114: @Path("_query")
115: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
116: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
117: public RolePermissionListResult query(
118: @PathParam("scopeId") ScopeId scopeId,
119: @PathParam("roleId") EntityId roleId,
120: RolePermissionQuery query) throws KapuaException {
121: query.setScopeId(scopeId);
122:
123: AndPredicate andPredicate = query.andPredicate();
124: andPredicate.and(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
125:• if (query.getPredicate() != null) {
126: andPredicate.and(query.getPredicate());
127: }
128: query.setPredicate(andPredicate);
129:
130: return rolePermissionService.query(query);
131: }
132:
133: /**
134: * Counts the results with the given {@link RolePermissionQuery} parameter.
135: *
136: * @param scopeId The {@link ScopeId} in which to count results.
137: * @param roleId The {@link Role} id in which to count results.
138: * @param query The {@link RolePermissionQuery} to use to filter results.
139: * @return The count of all the result matching the given {@link RolePermissionQuery} parameter.
140: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
141: * @since 1.0.0
142: */
143: @POST
144: @Path("_count")
145: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
146: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
147: public CountResult count(
148: @PathParam("scopeId") ScopeId scopeId,
149: @PathParam("roleId") EntityId roleId,
150: RolePermissionQuery query) throws KapuaException {
151: query.setScopeId(scopeId);
152: query.setPredicate(query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId));
153:
154: return new CountResult(rolePermissionService.count(query));
155: }
156:
157: /**
158: * Creates a new RolePermission based on the information provided in RolePermissionCreator
159: * parameter.
160: *
161: * @param scopeId The {@link ScopeId} in which to create the {@link RolePermission}
162: * @param roleId The {@link Role} id in which to create the RolePermission.
163: * @param rolePermissionCreator Provides the information for the new RolePermission to be created.
164: * @return The newly created RolePermission object.
165: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
166: * @since 1.0.0
167: */
168: @POST
169: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
170: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
171: public Response create(
172: @PathParam("scopeId") ScopeId scopeId,
173: @PathParam("roleId") EntityId roleId,
174: RolePermissionCreator rolePermissionCreator) throws KapuaException {
175: rolePermissionCreator.setScopeId(scopeId);
176: rolePermissionCreator.setRoleId(roleId);
177:
178: return returnCreated(rolePermissionService.create(rolePermissionCreator));
179: }
180:
181: /**
182: * Returns the RolePermission specified by the "rolePermissionId" path parameter.
183: *
184: * @param scopeId The {@link ScopeId} of the requested {@link RolePermission}.
185: * @param roleId The {@link Role} id of the requested {@link RolePermission}.
186: * @param rolePermissionId The id of the requested RolePermission.
187: * @return The requested RolePermission object.
188: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
189: * @since 1.0.0
190: */
191: @GET
192: @Path("{rolePermissionId}")
193: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
194: public RolePermission find(
195: @PathParam("scopeId") ScopeId scopeId,
196: @PathParam("roleId") EntityId roleId,
197: @PathParam("rolePermissionId") EntityId rolePermissionId) throws KapuaException {
198: RolePermissionQuery query = rolePermissionFactory.newQuery(scopeId);
199:
200: AndPredicate andPredicate = query.andPredicate(
201: query.attributePredicate(RolePermissionAttributes.ROLE_ID, roleId),
202: query.attributePredicate(KapuaEntityAttributes.ENTITY_ID, rolePermissionId)
203: );
204:
205: query.setPredicate(andPredicate);
206: query.setOffset(0);
207: query.setLimit(1);
208:
209: RolePermissionListResult results = rolePermissionService.query(query);
210:
211:• if (results.isEmpty()) {
212: throw new KapuaEntityNotFoundException(RolePermission.TYPE, rolePermissionId);
213: }
214:
215: return results.getFirstItem();
216: }
217:
218: /**
219: * Deletes the RolePermission specified by the "rolePermissionId" path parameter.
220: *
221: * @param scopeId The {@link ScopeId} of the {@link RolePermission} to delete.
222: * @param roleId The {@link Role} id of the {@link RolePermission} to delete.
223: * @param rolePermissionId The id of the RolePermission to be deleted.
224: * @return HTTP 200 if operation has completed successfully.
225: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
226: * @since 1.0.0
227: */
228: @DELETE
229: @Path("{rolePermissionId}")
230: public Response deleteRolePermission(
231: @PathParam("scopeId") ScopeId scopeId,
232: @PathParam("roleId") EntityId roleId,
233: @PathParam("rolePermissionId") EntityId rolePermissionId) throws KapuaException {
234: rolePermissionService.delete(scopeId, rolePermissionId);
235:
236: return returnNoContent();
237: }
238: }