| .. | .. |
|---|
| 10 | 10 | |
|---|
| 11 | 11 | import jakarta.annotation.security.RolesAllowed; |
|---|
| 12 | 12 | import jakarta.enterprise.context.RequestScoped; |
|---|
| 13 | +import jakarta.inject.Inject; |
|---|
| 13 | 14 | import jakarta.persistence.EntityManager; |
|---|
| 14 | 15 | import jakarta.persistence.TypedQuery; |
|---|
| 15 | 16 | import jakarta.servlet.http.HttpServletRequest; |
|---|
| .. | .. |
|---|
| 26 | 27 | import jakarta.ws.rs.core.MediaType; |
|---|
| 27 | 28 | import jakarta.ws.rs.core.Response; |
|---|
| 28 | 29 | import jakarta.ws.rs.core.Response.Status; |
|---|
| 30 | +import jakarta.ws.rs.core.SecurityContext; |
|---|
| 29 | 31 | |
|---|
| 30 | 32 | import org.apache.logging.log4j.LogManager; |
|---|
| 31 | 33 | import org.apache.logging.log4j.Logger; |
|---|
| .. | .. |
|---|
| 55 | 57 | |
|---|
| 56 | 58 | private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 57 | 59 | |
|---|
| 58 | | - @Context EntityManager em; |
|---|
| 59 | | - @Context BasicSecurityContext bsc; |
|---|
| 60 | + @Inject EntityManager em; |
|---|
| 60 | 61 | |
|---|
| 61 | 62 | public OrganizationResource() { } |
|---|
| 62 | 63 | |
|---|
| .. | .. |
|---|
| 72 | 73 | @Path("/") |
|---|
| 73 | 74 | @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 74 | 75 | @Securable |
|---|
| 75 | | - public Response index() { |
|---|
| 76 | | - LOG.info("Getting organizations list "); |
|---|
| 77 | | - em.clear(); |
|---|
| 78 | | - TypedQuery<Organization> q; |
|---|
| 79 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 80 | | - LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 81 | | - q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 82 | | - } else { |
|---|
| 83 | | - if (bsc.getOrganizationsIds() == null || bsc.getOrganizationsIds().isEmpty()) { |
|---|
| 84 | | - return Response.ok().build(); |
|---|
| 85 | | - } else { |
|---|
| 86 | | - q = em.createNamedQuery("list-organizations-by-ids", Organization.class); |
|---|
| 87 | | - q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 88 | | - } |
|---|
| 89 | | - } |
|---|
| 90 | | - List<Organization> list = q.getResultList(); |
|---|
| 91 | | - return Response.ok(list).build(); |
|---|
| 76 | + public Response index(@Context SecurityContext securityContext) { |
|---|
| 77 | + BasicSecurityContext bsc = org.jboss.resteasy.core.ResteasyContext.getContextData(BasicSecurityContext.class); |
|---|
| 78 | + if (bsc == null) { |
|---|
| 79 | + LOG.error("BasicSecurityContext not found in ResteasyContext"); |
|---|
| 80 | + return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 81 | + } |
|---|
| 82 | + LOG.info("Getting organizations list "); |
|---|
| 83 | + em.clear(); |
|---|
| 84 | + TypedQuery<Organization> q; |
|---|
| 85 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 86 | + LOG.info("Getting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 87 | + q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 88 | + } else { |
|---|
| 89 | + if (bsc.getOrganizationsIds() == null || bsc.getOrganizationsIds().isEmpty()) { |
|---|
| 90 | + return Response.ok().build(); |
|---|
| 91 | + } else { |
|---|
| 92 | + q = em.createNamedQuery("list-organizations-by-ids", Organization.class); |
|---|
| 93 | + q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 94 | + } |
|---|
| 95 | + } |
|---|
| 96 | + List<Organization> list = q.getResultList(); |
|---|
| 97 | + return Response.ok(list).build(); |
|---|
| 92 | 98 | } |
|---|
| 93 | 99 | |
|---|
| 94 | 100 | /** |
|---|