Roberto Sánchez
2014-01-22 79121484b7e6f721f5435a102018152a164ed655
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -1,8 +1,10 @@
11 package net.curisit.securis.services;
22
3
+import java.security.Principal;
34 import java.util.Date;
45 import java.util.List;
56
7
+import javax.annotation.security.RolesAllowed;
68 import javax.inject.Inject;
79 import javax.inject.Provider;
810 import javax.persistence.EntityManager;
....@@ -23,10 +25,10 @@
2325 import javax.ws.rs.core.Response.Status;
2426
2527 import net.curisit.integrity.commons.Utils;
26
-import net.curisit.integrity.exception.CurisException;
2728 import net.curisit.securis.DefaultExceptionHandler;
2829 import net.curisit.securis.db.Pack;
29
-import net.curisit.securis.db.User;
30
+import net.curisit.securis.security.BasicSecurityContext;
31
+import net.curisit.securis.security.Securable;
3032 import net.curisit.securis.utils.TokenHelper;
3133
3234 import org.slf4j.Logger;
....@@ -59,17 +61,35 @@
5961 */
6062 @GET
6163 @Path("/")
64
+ @Securable
6265 @Produces(
6366 { MediaType.APPLICATION_JSON })
64
- public Response index() {
67
+ public Response index(@Context BasicSecurityContext bsc) {
6568 log.info("Getting packs list ");
6669
6770 EntityManager em = emProvider.get();
68
- TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
71
+ // TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
72
+
73
+ TypedQuery<Pack> q;
74
+ if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
75
+ log.info("Getting all packs for user: " + bsc.getUserPrincipal());
76
+ q = em.createNamedQuery("list-packs", Pack.class);
77
+ } else {
78
+ q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
79
+ if (bsc.getOrganizationsIds() == null)
80
+ Response.ok().build();
81
+ // log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
82
+ q.setParameter("list_ids", bsc.getOrganizationsIds());
83
+ }
6984
7085 List<Pack> list = q.getResultList();
7186
7287 return Response.ok(list).build();
88
+ }
89
+
90
+ private Response generateErrorUnathorizedAccess(Pack pack, Principal user) {
91
+ log.error("Pack with id {} not accesible by user {}", pack, user);
92
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build();
7393 }
7494
7595 /**
....@@ -78,9 +98,10 @@
7898 */
7999 @GET
80100 @Path("/{packId}")
101
+ @Securable
81102 @Produces(
82103 { MediaType.APPLICATION_JSON })
83
- public Response get(@PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
104
+ public Response get(@PathParam("packId") String packId, @Context BasicSecurityContext bsc) {
84105 log.info("Getting pack data for id: {}: ", packId);
85106 if (packId == null || packId.equals("")) {
86107 log.error("Pack ID is mandatory");
....@@ -88,16 +109,23 @@
88109 }
89110
90111 EntityManager em = emProvider.get();
91
- Pack lt = em.find(Pack.class, Integer.parseInt(packId));
92
- if (lt == null) {
112
+ Pack pack = em.find(Pack.class, Integer.parseInt(packId));
113
+ if (pack == null) {
93114 log.error("Pack with id {} not found in DB", packId);
94115 return Response.status(Status.NOT_FOUND).build();
95116 }
96
- return Response.ok(lt).build();
117
+ if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) {
118
+ if (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId())) {
119
+ return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal());
120
+ }
121
+ }
122
+ return Response.ok(pack).build();
97123 }
98124
99125 @POST
100126 @Path("/")
127
+ @Securable
128
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
101129 @Consumes(MediaType.APPLICATION_JSON)
102130 @Produces(
103131 { MediaType.APPLICATION_JSON })
....@@ -112,21 +140,12 @@
112140 return Response.ok(pack).build();
113141 }
114142
115
- private User getUser(String username, EntityManager em) throws CurisException {
116
- User user = null;
117
- if (username != null) {
118
- user = em.find(User.class, username);
119
- if (user == null) {
120
- throw new CurisException("User not found");
121
- }
122
- }
123
- return user;
124
- }
125
-
126143 @PUT
127144 @POST
128145 @Path("/{packId}")
129146 @Transactional
147
+ @Securable
148
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
130149 @Consumes(MediaType.APPLICATION_JSON)
131150 @Produces(
132151 { MediaType.APPLICATION_JSON })
....@@ -141,6 +160,8 @@
141160
142161 @DELETE
143162 @Path("/{packId}")
163
+ @Securable
164
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
144165 @Transactional
145166 @Produces(
146167 { MediaType.APPLICATION_JSON })