| .. | .. |
|---|
| 26 | 26 | |
|---|
| 27 | 27 | import net.curisit.integrity.commons.Utils; |
|---|
| 28 | 28 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 29 | +import net.curisit.securis.SeCurisException; |
|---|
| 29 | 30 | import net.curisit.securis.db.Organization; |
|---|
| 30 | 31 | import net.curisit.securis.db.User; |
|---|
| 31 | 32 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| .. | .. |
|---|
| 46 | 47 | @Path("/organization") |
|---|
| 47 | 48 | public class OrganizationResource { |
|---|
| 48 | 49 | |
|---|
| 49 | | - private static final Logger log = LogManager.getLogger(OrganizationResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 50 | 51 | |
|---|
| 51 | 52 | @Inject |
|---|
| 52 | 53 | private Provider<EntityManager> emProvider; |
|---|
| .. | .. |
|---|
| 65 | 66 | @Securable |
|---|
| 66 | 67 | // @RolesAllowed(SecurityContextWrapper.ROL_ADVANCE) |
|---|
| 67 | 68 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 68 | | - log.info("Getting organizations list "); |
|---|
| 69 | + LOG.info("Getting organizations list "); |
|---|
| 69 | 70 | |
|---|
| 70 | | - // log.info("User orgs: {}", request.getAttribute("oser_orgs")); |
|---|
| 71 | + // LOG.info("User orgs: {}", request.getAttribute("oser_orgs")); |
|---|
| 71 | 72 | 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)); |
|---|
| 76 | 77 | EntityManager em = emProvider.get(); |
|---|
| 77 | 78 | TypedQuery<Organization> q; |
|---|
| 78 | 79 | 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()); |
|---|
| 80 | 81 | q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 81 | 82 | } else { |
|---|
| 82 | 83 | q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 83 | 84 | // if (securityContext.getOrganizationsIds() == null) |
|---|
| 84 | 85 | // 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()); |
|---|
| 86 | 87 | // q = em.createNamedQuery("list-organizations-by-ids", Organization.class); |
|---|
| 87 | 88 | // q.setParameter("list_ids", securityContext.getOrganizationsIds()); |
|---|
| 88 | 89 | } |
|---|
| .. | .. |
|---|
| 102 | 103 | { MediaType.APPLICATION_JSON }) |
|---|
| 103 | 104 | @Securable |
|---|
| 104 | 105 | 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); |
|---|
| 106 | 107 | if (orgid == null || orgid.equals("")) { |
|---|
| 107 | | - log.error("Organization ID is mandatory"); |
|---|
| 108 | + LOG.error("Organization ID is mandatory"); |
|---|
| 108 | 109 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 109 | 110 | } |
|---|
| 110 | 111 | // 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()); |
|---|
| 112 | 113 | // return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 113 | 114 | // } |
|---|
| 114 | 115 | |
|---|
| 115 | 116 | EntityManager em = emProvider.get(); |
|---|
| 116 | 117 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 117 | 118 | 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); |
|---|
| 119 | 120 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid).build(); |
|---|
| 120 | 121 | } |
|---|
| 121 | 122 | return Response.ok(org).build(); |
|---|
| .. | .. |
|---|
| 139 | 140 | @Securable |
|---|
| 140 | 141 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 141 | 142 | 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"); |
|---|
| 143 | 144 | 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(); |
|---|
| 151 | 150 | } |
|---|
| 151 | + |
|---|
| 152 | 152 | List<User> users = null; |
|---|
| 153 | 153 | List<String> usersIds = org.getUsersIds(); |
|---|
| 154 | 154 | if (usersIds != null && usersIds.size() > 0) { |
|---|
| .. | .. |
|---|
| 156 | 156 | for (String username : usersIds) { |
|---|
| 157 | 157 | User user = em.find(User.class, username); |
|---|
| 158 | 158 | 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); |
|---|
| 160 | 160 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build(); |
|---|
| 161 | 161 | } |
|---|
| 162 | 162 | users.add(user); |
|---|
| .. | .. |
|---|
| 164 | 164 | } |
|---|
| 165 | 165 | |
|---|
| 166 | 166 | org.setUsers(users); |
|---|
| 167 | | - org.setParentOrganization(parentOrg); |
|---|
| 168 | 167 | org.setCreationTimestamp(new Date()); |
|---|
| 169 | 168 | em.persist(org); |
|---|
| 170 | 169 | |
|---|
| 171 | 170 | 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); |
|---|
| 172 | 201 | } |
|---|
| 173 | 202 | |
|---|
| 174 | 203 | @PUT |
|---|
| .. | .. |
|---|
| 181 | 210 | @Securable |
|---|
| 182 | 211 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 183 | 212 | 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); |
|---|
| 185 | 214 | EntityManager em = emProvider.get(); |
|---|
| 186 | 215 | Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 187 | 216 | 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); |
|---|
| 189 | 218 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid).build(); |
|---|
| 190 | 219 | } |
|---|
| 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(); |
|---|
| 201 | 229 | } |
|---|
| 202 | 230 | } |
|---|
| 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(); |
|---|
| 216 | 236 | } |
|---|
| 217 | 237 | |
|---|
| 218 | | - currentOrg.setUsers(users); |
|---|
| 219 | | - currentOrg.setParentOrganization(parentOrg); |
|---|
| 220 | 238 | currentOrg.setCode(org.getCode()); |
|---|
| 221 | 239 | currentOrg.setName(org.getName()); |
|---|
| 222 | 240 | currentOrg.setDescription(org.getDescription()); |
|---|
| .. | .. |
|---|
| 233 | 251 | @Securable |
|---|
| 234 | 252 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 235 | 253 | 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); |
|---|
| 237 | 255 | EntityManager em = emProvider.get(); |
|---|
| 238 | 256 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 239 | 257 | 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); |
|---|
| 241 | 259 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid).build(); |
|---|
| 242 | 260 | } |
|---|
| 243 | 261 | 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); |
|---|
| 245 | 263 | return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build(); |
|---|
| 246 | 264 | } |
|---|
| 247 | 265 | |
|---|