Roberto Sánchez
2014-01-23 cbfe9207ad7c9bba96b39c550d250d12097fd06f
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -7,16 +7,15 @@
77 import javax.inject.Provider;
88 import javax.persistence.EntityManager;
99 import javax.persistence.TypedQuery;
10
-import javax.servlet.http.HttpServletRequest;
1110 import javax.ws.rs.Consumes;
1211 import javax.ws.rs.DELETE;
1312 import javax.ws.rs.GET;
14
-import javax.ws.rs.HeaderParam;
1513 import javax.ws.rs.POST;
1614 import javax.ws.rs.PUT;
1715 import javax.ws.rs.Path;
1816 import javax.ws.rs.PathParam;
1917 import javax.ws.rs.Produces;
18
+import javax.ws.rs.QueryParam;
2019 import javax.ws.rs.core.Context;
2120 import javax.ws.rs.core.MediaType;
2221 import javax.ws.rs.core.Response;
....@@ -28,6 +27,8 @@
2827 import net.curisit.securis.db.License;
2928 import net.curisit.securis.db.Pack;
3029 import net.curisit.securis.db.User;
30
+import net.curisit.securis.security.BasicSecurityContext;
31
+import net.curisit.securis.security.Securable;
3132 import net.curisit.securis.utils.TokenHelper;
3233
3334 import org.slf4j.Logger;
....@@ -40,7 +41,7 @@
4041 *
4142 * @author roberto <roberto.sanchez@curisit.net>
4243 */
43
-@Path("/organization")
44
+@Path("/license")
4445 public class LicenseResource {
4546
4647 private static final Logger log = LoggerFactory.getLogger(LicenseResource.class);
....@@ -60,14 +61,25 @@
6061 */
6162 @GET
6263 @Path("/")
64
+ @Securable
6365 @Produces(
6466 { MediaType.APPLICATION_JSON })
65
- public Response index() {
67
+ public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
6668 log.info("Getting licenses list ");
6769
6870 EntityManager em = emProvider.get();
69
- TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class);
7071
72
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
73
+ Pack pack = em.find(Pack.class, packId);
74
+ if (pack == null)
75
+ return Response.ok().build();
76
+ if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
77
+ log.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal());
78
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build();
79
+ }
80
+ }
81
+ TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class);
82
+ q.setParameter("packId", packId);
7183 List<License> list = q.getResultList();
7284
7385 return Response.ok(list).build();
....@@ -79,9 +91,10 @@
7991 */
8092 @GET
8193 @Path("/{licId}")
94
+ @Securable
8295 @Produces(
8396 { MediaType.APPLICATION_JSON })
84
- public Response get(@PathParam("licId") String licId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
97
+ public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
8598 log.info("Getting organization data for id: {}: ", licId);
8699 if (licId == null || licId.equals("")) {
87100 log.error("License ID is mandatory");
....@@ -89,21 +102,28 @@
89102 }
90103
91104 EntityManager em = emProvider.get();
92
- License lt = em.find(License.class, Integer.parseInt(licId));
93
- if (lt == null) {
105
+ License lic = em.find(License.class, licId);
106
+ if (lic == null) {
94107 log.error("License with id {} not found in DB", licId);
95
- return Response.status(Status.NOT_FOUND).build();
108
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
96109 }
97
- return Response.ok(lt).build();
110
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
111
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
112
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
113
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
114
+ }
115
+ }
116
+ return Response.ok(lic).build();
98117 }
99118
100119 @POST
101120 @Path("/")
102121 @Consumes(MediaType.APPLICATION_JSON)
122
+ @Securable
103123 @Produces(
104124 { MediaType.APPLICATION_JSON })
105125 @Transactional
106
- public Response create(License lic, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
126
+ public Response create(License lic, @Context BasicSecurityContext bsc) {
107127 log.info("Creating new organization");
108128 EntityManager em = emProvider.get();
109129 Pack pack = null;
....@@ -112,6 +132,13 @@
112132 if (pack == null) {
113133 log.error("License pack with id {} not found in DB", lic.getPackId());
114134 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build();
135
+ } else {
136
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
137
+ if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
138
+ log.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal());
139
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build();
140
+ }
141
+ }
115142 }
116143 }
117144
....@@ -124,16 +151,11 @@
124151 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's created by user not found with ID: " + createdByUsername).build();
125152 }
126153
127
- try {
128
- User canceledBy = getUser(lic.getCanceledById(), em);
129
- lic.setCanceledBy(canceledBy);
130
- } catch (CurisException ex) {
131
- String canceledByUsername = lic.getCreatedById();
132
- log.error("License canceled by user with id {} not found in DB", canceledByUsername);
133
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's canceled by user not found with ID: " + canceledByUsername).build();
134
- }
135
-
154
+ // ODO: Manage status if request data is set
155
+ lic.setCanceledBy(null);
156
+ lic.setStatus(License.Status.CREATED);
136157 lic.setCreationTimestamp(new Date());
158
+ lic.setModificationTimestamp(lic.getCreationTimestamp());
137159 em.persist(lic);
138160
139161 return Response.ok(lic).build();
....@@ -153,22 +175,30 @@
153175 @PUT
154176 @POST
155177 @Path("/{licId}")
178
+ @Securable
156179 @Transactional
157180 @Consumes(MediaType.APPLICATION_JSON)
158181 @Produces(
159182 { MediaType.APPLICATION_JSON })
160
- public Response modify(License lic, @PathParam("licId") String licId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
183
+ public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
161184 log.info("Modifying organization with id: {}", licId);
162185 EntityManager em = emProvider.get();
163186
164
- Pack pack = null;
165
- if (lic.getPackId() != null) {
166
- pack = em.find(Pack.class, lic.getPackId());
167
- if (pack == null) {
168
- log.error("License pack with id {} not found in DB", lic.getPackId());
169
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build();
170
- }
171
- }
187
+ // Pack pack = null;
188
+ // if (lic.getPackId() != null) {
189
+ // pack = em.find(Pack.class, lic.getPackId());
190
+ // if (pack == null) {
191
+ // log.error("License pack with id {} not found in DB", lic.getPackId());
192
+ // return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build();
193
+ // } else {
194
+ // if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
195
+ // if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
196
+ // log.error("License for pack with id {} can not be modified by user {}", pack.getId(), bsc.getUserPrincipal());
197
+ // return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build();
198
+ // }
199
+ // }
200
+ // }
201
+ // }
172202 User createdBy = null;
173203 try {
174204 createdBy = getUser(lic.getCreatedById(), em);
....@@ -186,30 +216,59 @@
186216 log.error("License canceled by user with id {} not found in DB", canceledByUsername);
187217 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's canceled by user not found with ID: " + canceledByUsername).build();
188218 }
219
+ License currentLicense = em.find(License.class, lic.getId());
220
+ if (currentLicense == null) {
221
+ log.error("License with id {} not found in DB", licId);
222
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
223
+ }
224
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
225
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
226
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
227
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
228
+ }
229
+ }
230
+ // TODO: set status based in current one and dates ? use custom actions ?
231
+ currentLicense.setCreatedBy(createdBy);
232
+ currentLicense.setCanceledBy(canceledBy);
233
+ // currentLicense.setPack(pack);
234
+ currentLicense.setCode(lic.getCode());
235
+ currentLicense.setFullName(lic.getFullName());
236
+ currentLicense.setEmail(lic.getEmail());
237
+ currentLicense.setRequestData(lic.getRequestData());
238
+ currentLicense.setModificationTimestamp(new Date());
239
+ em.persist(currentLicense);
189240
190
- lic.setCreatedBy(createdBy);
191
- lic.setCanceledBy(canceledBy);
192
- lic.setPack(pack);
193
- em.persist(lic);
194
-
195
- return Response.ok(lic).build();
241
+ return Response.ok(currentLicense).build();
196242 }
197243
198244 @DELETE
199245 @Path("/{licId}")
200246 @Transactional
247
+ @Securable
201248 @Produces(
202249 { MediaType.APPLICATION_JSON })
203
- public Response delete(@PathParam("licId") String licId, @Context HttpServletRequest request) {
250
+ public Response delete(@PathParam("licId") String licId, @Context BasicSecurityContext bsc) {
204251 log.info("Deleting license with id: {}", licId);
205252 EntityManager em = emProvider.get();
206
- License org = em.find(License.class, Integer.parseInt(licId));
207
- if (org == null) {
253
+ License lic = em.find(License.class, Integer.parseInt(licId));
254
+ if (lic == null) {
208255 log.error("License with id {} can not be deleted, It was not found in DB", licId);
209256 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License was not found, ID: " + licId).build();
210257 }
211258
212
- em.remove(org);
259
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
260
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
261
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
262
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
263
+ }
264
+ }
265
+
266
+ if (lic.getStatus() != License.Status.CANCELED || lic.getStatus() != License.Status.CREATED) {
267
+ log.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
268
+ return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License can not be deleted in current status").build();
269
+ }
270
+
271
+ em.remove(lic);
213272 return Response.ok(Utils.createMap("success", true, "id", licId)).build();
214273 }
215274