Roberto Sánchez
2014-09-18 52ce72b22ef8d92a1f35b4993bcddaaa66d67350
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
....@@ -26,6 +26,7 @@
2626
2727 import net.curisit.integrity.commons.Utils;
2828 import net.curisit.securis.DefaultExceptionHandler;
29
+import net.curisit.securis.SeCurisException;
2930 import net.curisit.securis.db.Organization;
3031 import net.curisit.securis.db.User;
3132 import net.curisit.securis.security.BasicSecurityContext;
....@@ -46,7 +47,7 @@
4647 @Path("/organization")
4748 public class OrganizationResource {
4849
49
- private static final Logger log = LogManager.getLogger(OrganizationResource.class);
50
+ private static final Logger LOG = LogManager.getLogger(OrganizationResource.class);
5051
5152 @Inject
5253 private Provider<EntityManager> emProvider;
....@@ -65,24 +66,24 @@
6566 @Securable
6667 // @RolesAllowed(SecurityContextWrapper.ROL_ADVANCE)
6768 public Response index(@Context BasicSecurityContext bsc) {
68
- log.info("Getting organizations list ");
69
+ LOG.info("Getting organizations list ");
6970
70
- // log.info("User orgs: {}", request.getAttribute("oser_orgs"));
71
+ // LOG.info("User orgs: {}", request.getAttribute("oser_orgs"));
7172 BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class);
72
- log.info("bsc: {}", bsc);
73
- log.info("bsc2: {}", bsc2);
74
- // log.info("securityContext: {}", scw);
75
- log.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN));
73
+ LOG.info("bsc: {}", bsc);
74
+ LOG.info("bsc2: {}", bsc2);
75
+ // LOG.info("securityContext: {}", scw);
76
+ LOG.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN));
7677 EntityManager em = emProvider.get();
7778 TypedQuery<Organization> q;
7879 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
79
- log.info("GEtting all orgs for user: " + bsc.getUserPrincipal());
80
+ LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal());
8081 q = em.createNamedQuery("list-organizations", Organization.class);
8182 } else {
8283 q = em.createNamedQuery("list-organizations", Organization.class);
8384 // if (securityContext.getOrganizationsIds() == null)
8485 // Response.ok().build();
85
- // log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
86
+ // LOG.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
8687 // q = em.createNamedQuery("list-organizations-by-ids", Organization.class);
8788 // q.setParameter("list_ids", securityContext.getOrganizationsIds());
8889 }
....@@ -102,20 +103,20 @@
102103 { MediaType.APPLICATION_JSON })
103104 @Securable
104105 public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
105
- log.info("Getting organization data for id: {}: ", orgid);
106
+ LOG.info("Getting organization data for id: {}: ", orgid);
106107 if (orgid == null || orgid.equals("")) {
107
- log.error("Organization ID is mandatory");
108
+ LOG.error("Organization ID is mandatory");
108109 return Response.status(Status.NOT_FOUND).build();
109110 }
110111 // if (!securityContext.isOrgAccesible(Integer.parseInt(orgid))) {
111
- // log.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal());
112
+ // LOG.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal());
112113 // return Response.status(Status.UNAUTHORIZED).build();
113114 // }
114115
115116 EntityManager em = emProvider.get();
116117 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
117118 if (org == null) {
118
- log.error("Organization with id {} not found in DB", orgid);
119
+ LOG.error("Organization with id {} not found in DB", orgid);
119120 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid).build();
120121 }
121122 return Response.ok(org).build();
....@@ -139,16 +140,15 @@
139140 @Securable
140141 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
141142 public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
142
- log.info("Creating new organization");
143
+ LOG.info("Creating new organization");
143144 EntityManager em = emProvider.get();
144
- Organization parentOrg = null;
145
- if (org.getParentOrgId() != null) {
146
- parentOrg = em.find(Organization.class, org.getParentOrgId());
147
- if (parentOrg == null) {
148
- log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
149
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
150
- }
145
+
146
+ try {
147
+ this.setParentOrg(org, org.getParentOrgId(), em);
148
+ } catch (SeCurisException e) {
149
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
151150 }
151
+
152152 List<User> users = null;
153153 List<String> usersIds = org.getUsersIds();
154154 if (usersIds != null && usersIds.size() > 0) {
....@@ -156,7 +156,7 @@
156156 for (String username : usersIds) {
157157 User user = em.find(User.class, username);
158158 if (user == null) {
159
- log.error("Organization user with id {} not found in DB", username);
159
+ LOG.error("Organization user with id {} not found in DB", username);
160160 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build();
161161 }
162162 users.add(user);
....@@ -164,11 +164,40 @@
164164 }
165165
166166 org.setUsers(users);
167
- org.setParentOrganization(parentOrg);
168167 org.setCreationTimestamp(new Date());
169168 em.persist(org);
170169
171170 return Response.ok(org).build();
171
+ }
172
+
173
+ private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException {
174
+ Organization parentOrg = null;
175
+ if (parentOrgId != null) {
176
+ parentOrg = em.find(Organization.class, parentOrgId);
177
+ if (parentOrg == null) {
178
+ LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId());
179
+ throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId());
180
+ }
181
+ }
182
+
183
+ org.setParentOrganization(parentOrg);
184
+ }
185
+
186
+ private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException {
187
+ List<User> users = null;
188
+ if (usersIds != null && usersIds.size() > 0) {
189
+ users = new ArrayList<>();
190
+ for (String username : usersIds) {
191
+ User user = em.find(User.class, username);
192
+ if (user == null) {
193
+ LOG.error("Organization user with id '{}' not found in DB", username);
194
+ throw new SecurityException("Organization's user not found with ID: " + username);
195
+ }
196
+ users.add(user);
197
+ }
198
+ }
199
+
200
+ org.setUsers(users);
172201 }
173202
174203 @PUT
....@@ -181,42 +210,31 @@
181210 @Securable
182211 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
183212 public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
184
- log.info("Modifying organization with id: {}", orgid);
213
+ LOG.info("Modifying organization with id: {}", orgid);
185214 EntityManager em = emProvider.get();
186215 Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
187216 if (currentOrg == null) {
188
- log.error("Organization with id {} not found in DB", orgid);
217
+ LOG.error("Organization with id {} not found in DB", orgid);
189218 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid).build();
190219 }
191
- Organization parentOrg = null;
192
- if (org.getParentOrgId() != null) {
193
- parentOrg = em.find(Organization.class, org.getParentOrgId());
194
- if (parentOrg == null) {
195
- log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
196
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
197
- }
198
- if (isCyclicalRelationship(currentOrg.getId(), parentOrg)) {
199
- log.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId());
200
- return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + parentOrg.getName()).build();
220
+ try {
221
+ this.setParentOrg(currentOrg, org.getParentOrgId(), em);
222
+ } catch (SeCurisException e) {
223
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
224
+ }
225
+ if (org.getParentOrganization() != null) {
226
+ if (isCyclicalRelationship(currentOrg.getId(), org.getParentOrganization())) {
227
+ LOG.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId());
228
+ return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + org.getParentOrganization().getName()).build();
201229 }
202230 }
203
-
204
- List<User> users = null;
205
- List<String> usersIds = org.getUsersIds();
206
- if (usersIds != null && usersIds.size() > 0) {
207
- users = new ArrayList<>();
208
- for (String username : usersIds) {
209
- User user = em.find(User.class, username);
210
- if (user == null) {
211
- log.error("Organization user with id '{}' not found in DB", username);
212
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build();
213
- }
214
- users.add(user);
215
- }
231
+
232
+ try {
233
+ setOrgUsers(currentOrg, org.getUsersIds(), em);
234
+ } catch (SeCurisException e) {
235
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
216236 }
217237
218
- currentOrg.setUsers(users);
219
- currentOrg.setParentOrganization(parentOrg);
220238 currentOrg.setCode(org.getCode());
221239 currentOrg.setName(org.getName());
222240 currentOrg.setDescription(org.getDescription());
....@@ -233,15 +251,15 @@
233251 @Securable
234252 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
235253 public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
236
- log.info("Deleting organization with id: {}", orgid);
254
+ LOG.info("Deleting organization with id: {}", orgid);
237255 EntityManager em = emProvider.get();
238256 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
239257 if (org == null) {
240
- log.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
258
+ LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
241259 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid).build();
242260 }
243261 if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) {
244
- log.error("Organization has children and can not be deleted, ID: " + orgid);
262
+ LOG.error("Organization has children and can not be deleted, ID: " + orgid);
245263 return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build();
246264 }
247265