#4479 - upgrade SecurisServer to Java 21
| .. | .. |
|---|
| 27 | 27 | import org.apache.logging.log4j.LogManager; |
|---|
| 28 | 28 | import org.apache.logging.log4j.Logger; |
|---|
| 29 | 29 | |
|---|
| 30 | | -import jakarta.inject.Singleton; |
|---|
| 30 | +import jakarta.enterprise.context.ApplicationScoped; |
|---|
| 31 | 31 | |
|---|
| 32 | 32 | /** |
|---|
| 33 | 33 | * LicenseGenerator |
|---|
| .. | .. |
|---|
| 39 | 39 | * @author JRA |
|---|
| 40 | 40 | * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 41 | 41 | */ |
|---|
| 42 | | -@Singleton |
|---|
| 42 | +@ApplicationScoped |
|---|
| 43 | 43 | public class LicenseGenerator { |
|---|
| 44 | 44 | |
|---|
| 45 | 45 | private static final Logger LOG = LogManager.getLogger(LicenseGenerator.class); |
|---|
| .. | .. |
|---|
| 8 | 8 |
|
|---|
| 9 | 9 | import jakarta.ws.rs.core.Application;
|
|---|
| 10 | 10 |
|
|---|
| 11 | | -/// import net.curisit.securis.ioc.RequestsInterceptor;
|
|---|
| 11 | +import net.curisit.securis.ioc.RequestsInterceptor;
|
|---|
| 12 | 12 | import net.curisit.securis.services.ApiResource;
|
|---|
| 13 | 13 | import net.curisit.securis.services.ApplicationResource;
|
|---|
| 14 | 14 | import net.curisit.securis.services.BasicServices;
|
|---|
| .. | .. |
|---|
| 50 | 50 | classes.add(OrganizationResource.class);
|
|---|
| 51 | 51 | classes.add(LicenseTypeResource.class);
|
|---|
| 52 | 52 | classes.add(PackResource.class);
|
|---|
| 53 | | - /// classes.add(RequestsInterceptor.class);
|
|---|
| 53 | + classes.add(RequestsInterceptor.class);
|
|---|
| 54 | 54 | // classes.add(TransactionsInterceptor.class);
|
|---|
| 55 | 55 | // classes.add(JpaJacksonProvider.class);
|
|---|
| 56 | 56 | // classes.add(JacksonConfig.class);
|
|---|
| .. | .. |
|---|
| 21 | 21 | |
|---|
| 22 | 22 | private static final Logger log = LogManager.getLogger(EntityManagerProducer.class); |
|---|
| 23 | 23 | |
|---|
| 24 | | - @Inject |
|---|
| 25 | | - private EntityManagerProvider emProvider; |
|---|
| 24 | + @Inject private EntityManagerProvider emProvider; |
|---|
| 26 | 25 | |
|---|
| 27 | 26 | @Produces |
|---|
| 28 | 27 | @RequestScoped |
|---|
| .. | .. |
|---|
| 28 | 28 | |
|---|
| 29 | 29 | import org.apache.logging.log4j.LogManager; |
|---|
| 30 | 30 | import org.apache.logging.log4j.Logger; |
|---|
| 31 | | - |
|---|
| 31 | +import org.jboss.resteasy.core.ResteasyContext; |
|---|
| 32 | 32 | import net.curisit.securis.db.User; |
|---|
| 33 | 33 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 34 | 34 | import net.curisit.securis.security.Securable; |
|---|
| .. | .. |
|---|
| 189 | 189 | sc.setOrganizationsIds(getUserOrganizations(username)); |
|---|
| 190 | 190 | sc.setApplicationsIds(getUserApplications(username)); |
|---|
| 191 | 191 | ctx.setSecurityContext(sc); |
|---|
| 192 | + |
|---|
| 193 | + ResteasyContext.pushContext(BasicSecurityContext.class, sc); |
|---|
| 194 | + |
|---|
| 192 | 195 | return true; |
|---|
| 193 | 196 | } |
|---|
| 194 | 197 | |
|---|
| .. | .. |
|---|
| 7 | 7 | import java.util.Date; |
|---|
| 8 | 8 | import java.util.List; |
|---|
| 9 | 9 | |
|---|
| 10 | +import jakarta.enterprise.context.RequestScoped; |
|---|
| 10 | 11 | import jakarta.inject.Inject; |
|---|
| 11 | 12 | import jakarta.persistence.EntityManager; |
|---|
| 12 | 13 | import jakarta.persistence.NoResultException; |
|---|
| .. | .. |
|---|
| 76 | 77 | * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 77 | 78 | */ |
|---|
| 78 | 79 | @Path("/api") |
|---|
| 80 | +@RequestScoped |
|---|
| 79 | 81 | public class ApiResource { |
|---|
| 80 | 82 | |
|---|
| 81 | 83 | private static final Logger LOG = LogManager.getLogger(ApiResource.class); |
|---|
| .. | .. |
|---|
| 10 | 10 | import java.util.Set; |
|---|
| 11 | 11 | |
|---|
| 12 | 12 | import jakarta.annotation.security.RolesAllowed; |
|---|
| 13 | +import jakarta.enterprise.context.RequestScoped; |
|---|
| 13 | 14 | import jakarta.inject.Inject; |
|---|
| 14 | 15 | import jakarta.persistence.EntityManager; |
|---|
| 15 | 16 | import jakarta.persistence.TypedQuery; |
|---|
| .. | .. |
|---|
| 63 | 64 | * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 64 | 65 | */ |
|---|
| 65 | 66 | @Path("/application") |
|---|
| 67 | +@RequestScoped |
|---|
| 66 | 68 | public class ApplicationResource { |
|---|
| 67 | 69 | |
|---|
| 68 | 70 | @Inject TokenHelper tokenHelper; |
|---|
| 69 | 71 | @Inject MetadataHelper metadataHelper; |
|---|
| 70 | 72 | |
|---|
| 71 | | - @Context EntityManager em; |
|---|
| 73 | + @Inject EntityManager em; |
|---|
| 72 | 74 | |
|---|
| 73 | 75 | private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 74 | 76 | |
|---|
| .. | .. |
|---|
| 10 | 10 | import java.util.stream.Collectors; |
|---|
| 11 | 11 | |
|---|
| 12 | 12 | import jakarta.annotation.security.RolesAllowed; |
|---|
| 13 | +import jakarta.enterprise.context.RequestScoped; |
|---|
| 13 | 14 | import jakarta.inject.Inject; |
|---|
| 14 | 15 | import jakarta.persistence.EntityManager; |
|---|
| 15 | 16 | import jakarta.persistence.TypedQuery; |
|---|
| .. | .. |
|---|
| 57 | 58 | * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 58 | 59 | */ |
|---|
| 59 | 60 | @Path("/licensetype") |
|---|
| 61 | +@RequestScoped |
|---|
| 60 | 62 | public class LicenseTypeResource { |
|---|
| 61 | 63 | |
|---|
| 62 | 64 | private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| .. | .. |
|---|
| 64 | 66 | @Inject TokenHelper tokenHelper; |
|---|
| 65 | 67 | @Inject MetadataHelper metadataHelper; |
|---|
| 66 | 68 | |
|---|
| 67 | | - @Context EntityManager em; |
|---|
| 69 | + @Inject EntityManager em; |
|---|
| 68 | 70 | |
|---|
| 69 | 71 | public LicenseTypeResource() { } |
|---|
| 70 | 72 | |
|---|
| .. | .. |
|---|
| 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 | /** |
|---|
| .. | .. |
|---|
| 11 | 11 | import java.util.Set; |
|---|
| 12 | 12 | |
|---|
| 13 | 13 | import jakarta.annotation.security.RolesAllowed; |
|---|
| 14 | +import jakarta.enterprise.context.RequestScoped; |
|---|
| 14 | 15 | import jakarta.inject.Inject; |
|---|
| 15 | 16 | import jakarta.persistence.EntityManager; |
|---|
| 16 | 17 | import jakarta.persistence.TypedQuery; |
|---|
| .. | .. |
|---|
| 66 | 67 | * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 67 | 68 | */ |
|---|
| 68 | 69 | @Path("/pack") |
|---|
| 70 | +@RequestScoped |
|---|
| 69 | 71 | public class PackResource { |
|---|
| 70 | 72 | |
|---|
| 71 | 73 | private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| .. | .. |
|---|
| 407 | 407 | LOG.info("login() called. session={}", request.getSession(false)); |
|---|
| 408 | 408 | LOG.info("login() username='{}'", username); |
|---|
| 409 | 409 | |
|---|
| 410 | | - LOG.info("UserResource instance={}", this); |
|---|
| 411 | | - LOG.info("Injected em={}", em); |
|---|
| 412 | | - LOG.info("Injected tokenHelper={}", tokenHelper); |
|---|
| 413 | | - LOG.info("Injected cache={}", cache); |
|---|
| 414 | | - |
|---|
| 410 | + boolean trazalogin = false; |
|---|
| 411 | + |
|---|
| 412 | + if (trazalogin) { |
|---|
| 413 | + LOG.info("UserResource instance={}", this); |
|---|
| 414 | + LOG.info("Injected em={}", em); |
|---|
| 415 | + LOG.info("Injected tokenHelper={}", tokenHelper); |
|---|
| 416 | + LOG.info("Injected cache={}", cache); |
|---|
| 417 | + } |
|---|
| 418 | + |
|---|
| 415 | 419 | if (username == null || username.trim().isEmpty()) { |
|---|
| 416 | 420 | LOG.error("login() username is null or empty"); |
|---|
| 417 | 421 | throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials"); |
|---|
| .. | .. |
|---|
| 432 | 436 | String securedPassword = Utils.sha256(password); |
|---|
| 433 | 437 | LOG.info("login() hashed password generated? {}", securedPassword != null); |
|---|
| 434 | 438 | |
|---|
| 439 | + if (trazalogin) { |
|---|
| 440 | + LOG.info("login() stored password hash='{}'", user.getPassword()); |
|---|
| 441 | + LOG.info("login() computed password hash='{}'", securedPassword); |
|---|
| 442 | + LOG.info("login() stored length={}, computed length={}", |
|---|
| 443 | + user.getPassword() == null ? null : user.getPassword().length(), |
|---|
| 444 | + securedPassword == null ? null : securedPassword.length()); |
|---|
| 445 | + |
|---|
| 446 | + |
|---|
| 447 | + LOG.info("raw password='{}'", password); |
|---|
| 448 | + LOG.info("sha256(raw)='{}'", Utils.sha256(password)); |
|---|
| 449 | + LOG.info("sha256(trim)='{}'", Utils.sha256(password == null ? null : password.trim())); |
|---|
| 450 | + LOG.info("sha256(lower)='{}'", Utils.sha256(password == null ? null : password.toLowerCase())); |
|---|
| 451 | + LOG.info("sha256(upper)='{}'", Utils.sha256(password == null ? null : password.toUpperCase())); |
|---|
| 452 | + LOG.info("sha256(username+password)='{}'", Utils.sha256(username + password)); |
|---|
| 453 | + LOG.info("sha256(password+username)='{}'", Utils.sha256(password + username)); |
|---|
| 454 | + LOG.info("stored hash='{}'", user.getPassword()); |
|---|
| 455 | + |
|---|
| 456 | + LOG.info("password.equals('admin')={}", "admin".equals(password)); |
|---|
| 457 | + LOG.info("password length={}", password == null ? null : password.length()); |
|---|
| 458 | + } |
|---|
| 459 | + |
|---|
| 435 | 460 | if (securedPassword == null || !securedPassword.equals(user.getPassword())) { |
|---|
| 436 | 461 | LOG.error("Wrong password for user '{}'", username); |
|---|
| 437 | 462 | throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials"); |
|---|
| .. | .. |
|---|
| 1 | 1 | <?xml version="1.0" encoding="UTF-8"?>
|
|---|
| 2 | 2 | <Configuration>
|
|---|
| 3 | | - <Appenders>
|
|---|
| 4 | | -
|
|---|
| 5 | | - <RollingFile name="defaultFile"
|
|---|
| 6 | | - fileName="${sys:catalina.home}/logs/securis-server.log" append="true"
|
|---|
| 7 | | - filePattern="${sys:catalina.home}/logs/securis-server-%d{yyyy-MM-dd-HH}.log.gz">
|
|---|
| 8 | | - <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
|
|---|
| 9 | | - <Policies>
|
|---|
| 10 | | - <TimeBasedTriggeringPolicy interval="1"
|
|---|
| 11 | | - modulate="true" />
|
|---|
| 12 | | - </Policies>
|
|---|
| 13 | | - <DefaultRolloverStrategy max="10" />
|
|---|
| 14 | | - </RollingFile>
|
|---|
| 15 | | - <Async name="defaultFileAsync" includeLocation="true">
|
|---|
| 16 | | - <AppenderRef ref="defaultFile" />
|
|---|
| 17 | | - </Async>
|
|---|
| 18 | | -
|
|---|
| 19 | | - <Console name="stdout" target="SYSTEM_OUT">
|
|---|
| 20 | | - <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
|
|---|
| 21 | | - </Console>
|
|---|
| 22 | | -
|
|---|
| 23 | | - <Console name="console" target="SYSTEM_OUT">
|
|---|
| 24 | | - <PatternLayout pattern="%m%n" />
|
|---|
| 25 | | - </Console>
|
|---|
| 26 | | - </Appenders>
|
|---|
| 27 | | - <Loggers>
|
|---|
| 28 | | -
|
|---|
| 29 | | - <Logger name="net.curisit" level="INFO" additivity="false">
|
|---|
| 30 | | - <AppenderRef ref="defaultFileAsync" />
|
|---|
| 31 | | - <AppenderRef ref="stdout" />
|
|---|
| 32 | | - </Logger>
|
|---|
| 33 | | -
|
|---|
| 34 | | - <Logger name="console" level="INFO" additivity="false">
|
|---|
| 35 | | - <AppenderRef ref="console" />
|
|---|
| 36 | | - </Logger>
|
|---|
| 37 | | -
|
|---|
| 38 | | -<!-- <logger name="org.hibernate.type.descriptor.sql.BasicBinder"
|
|---|
| 39 | | - level="TRACE" additivity="false">
|
|---|
| 40 | | - <appender-ref ref="stdout" />
|
|---|
| 41 | | - </logger> -->
|
|---|
| 42 | | -
|
|---|
| 43 | | - <Root level="INFO">
|
|---|
| 44 | | - <AppenderRef ref="stdout" />
|
|---|
| 45 | | - </Root>
|
|---|
| 46 | | -
|
|---|
| 47 | | - </Loggers>
|
|---|
| 3 | + <Appenders>
|
|---|
| 4 | + <Console name="STDOUT" target="SYSTEM_OUT">
|
|---|
| 5 | + <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
|---|
| 6 | + </Console>
|
|---|
| 7 | + </Appenders>
|
|---|
| 8 | + <Loggers>
|
|---|
| 9 | + <Root level="info">
|
|---|
| 10 | + <AppenderRef ref="STDOUT"/>
|
|---|
| 11 | + </Root>
|
|---|
| 12 | + </Loggers>
|
|---|
| 48 | 13 | </Configuration>
|
|---|
| .. | .. |
|---|
| 36 | 36 | <param-name>resteasy.providers</param-name> |
|---|
| 37 | 37 | <param-value>net.curisit.securis.DefaultExceptionHandler</param-value> |
|---|
| 38 | 38 | </context-param> |
|---|
| 39 | + |
|---|
| 40 | + <context-param> |
|---|
| 41 | + <param-name>resteasy.injector.factory</param-name> |
|---|
| 42 | + <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value> |
|---|
| 43 | + </context-param> |
|---|
| 39 | 44 | |
|---|
| 45 | + |
|---|
| 40 | 46 | <context-param> |
|---|
| 41 | 47 | <param-name>resteasy.servlet.mapping.prefix</param-name> |
|---|
| 42 | 48 | <param-value>/api</param-value> |
|---|
| 43 | 49 | </context-param> |
|---|
| 44 | 50 | |
|---|
| 45 | | - <context-param> |
|---|
| 46 | | - <param-name>resteasy.injector.factory</param-name> |
|---|
| 47 | | - <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value> |
|---|
| 48 | | - </context-param> |
|---|
| 49 | 51 | |
|---|
| 50 | 52 | |
|---|
| 51 | 53 | <filter> |
|---|
| .. | .. |
|---|
| 56 | 58 | </filter> |
|---|
| 57 | 59 | <filter-mapping> |
|---|
| 58 | 60 | <filter-name>DevFilter</filter-name> |
|---|
| 59 | | - <url-pattern>/api/*</url-pattern> |
|---|
| 61 | + <url-pattern>/*</url-pattern> |
|---|
| 60 | 62 | </filter-mapping> |
|---|
| 61 | 63 | |
|---|
| 62 | 64 | <filter> |
|---|
| .. | .. |
|---|
| 17 | 17 | |
|---|
| 18 | 18 | ngOnInit(): void { |
|---|
| 19 | 19 | //TODO Move to service |
|---|
| 20 | | - this.http.get("version", /* workaround to avoid OPTIONS method request*/ new BaseRequestOptions()) |
|---|
| 20 | + this.http.get("api/version", /* workaround to avoid OPTIONS method request*/ new BaseRequestOptions()) |
|---|
| 21 | 21 | .map((res) => <string>res.json().version) |
|---|
| 22 | 22 | .subscribe( |
|---|
| 23 | 23 | version => this.securisVersion = version, |
|---|
| .. | .. |
|---|
| 84 | 84 | this.toaster.success(this.$L.get('{} was sucessfully deleted', this.resourceName.capitalize())); |
|---|
| 85 | 85 | this.goBack(); |
|---|
| 86 | 86 | }, |
|---|
| 87 | | - err => this.toaster.success(err.message, this.$L.get('Error deleting the {}', this.resourceName)) |
|---|
| 87 | + err => this.toaster.error(err.message, this.$L.get('Error deleting the {}', this.resourceName)) |
|---|
| 88 | 88 | ); |
|---|
| 89 | 89 | } |
|---|
| 90 | 90 | }); |
|---|
| .. | .. |
|---|
| 34 | 34 | } |
|---|
| 35 | 35 | |
|---|
| 36 | 36 | loadCombos(): void { |
|---|
| 37 | | - this.http.get('organization') |
|---|
| 37 | + this.http.get('api/organization') |
|---|
| 38 | 38 | .map(response => response.json().map((org : any) => <IComboOption>{id: org.id, label: `(${org.code}) ${org.name}`})) |
|---|
| 39 | 39 | .subscribe( |
|---|
| 40 | 40 | data => super.setViewData(() => this.organizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label))), |
|---|
| .. | .. |
|---|
| 1 | 1 | import { Observable } from 'rxjs/Rx'; |
|---|
| 2 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 3 | import { Http, RequestOptions } from '@angular/http'; |
|---|
| 4 | +import { Locker } from 'angular-safeguard'; |
|---|
| 4 | 5 | import { SeCurisResourceServices } from './base'; |
|---|
| 5 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 6 | 7 | |
|---|
| .. | .. |
|---|
| 18 | 19 | |
|---|
| 19 | 20 | @Injectable() |
|---|
| 20 | 21 | export class ApplicationsService extends SeCurisResourceServices { |
|---|
| 21 | | - constructor(http: Http, $L: LocaleService) { |
|---|
| 22 | | - super($L, http, 'application'); |
|---|
| 22 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 23 | + super($L, http, store, 'application'); |
|---|
| 23 | 24 | } |
|---|
| 24 | 25 | |
|---|
| 25 | 26 | |
|---|
| .. | .. |
|---|
| 1 | 1 | import { LocaleService } from '../common/i18n'; |
|---|
| 2 | 2 | import { BasicService } from '../common/utils'; |
|---|
| 3 | 3 | import { Observable } from 'rxjs/Observable'; |
|---|
| 4 | | -import { Http, RequestOptionsArgs, URLSearchParams } from '@angular/http'; |
|---|
| 4 | +import { Http, RequestOptionsArgs, URLSearchParams, Headers } from '@angular/http'; |
|---|
| 5 | +import { Locker } from 'angular-safeguard'; |
|---|
| 6 | + |
|---|
| 5 | 7 | |
|---|
| 6 | 8 | export class MySearchParams extends URLSearchParams { |
|---|
| 7 | | - constructor(obj : any = {}) { |
|---|
| 8 | | - var searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&'); |
|---|
| 9 | + constructor(obj: any = {}) { |
|---|
| 10 | + const searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&'); |
|---|
| 9 | 11 | super(searchQuery); |
|---|
| 10 | 12 | } |
|---|
| 11 | 13 | } |
|---|
| 12 | 14 | |
|---|
| 13 | 15 | export class SeCurisResourceServices extends BasicService { |
|---|
| 14 | | - constructor($L: LocaleService, |
|---|
| 15 | | - protected http: Http, |
|---|
| 16 | | - protected resource: string) { |
|---|
| 17 | | - super($L); |
|---|
| 18 | | - } |
|---|
| 16 | + constructor($L: LocaleService, |
|---|
| 17 | + protected http: Http, |
|---|
| 18 | + protected store: Locker, |
|---|
| 19 | + protected resource: string) { |
|---|
| 20 | + super($L); |
|---|
| 21 | + this.resource = `api/${resource}`; |
|---|
| 22 | + } |
|---|
| 19 | 23 | |
|---|
| 20 | | - public get(id?: any) : Observable<any> { |
|---|
| 21 | | - let url = `${this.resource}/${id || ''}`; |
|---|
| 22 | | - return this.http.get(url).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 24 | + protected getAuthHeaders(): RequestOptionsArgs { |
|---|
| 25 | + const token = this.store.get('token'); |
|---|
| 26 | + const headers = new Headers(); |
|---|
| 27 | + if (token) { |
|---|
| 28 | + headers.append('X-SECURIS-TOKEN', token); |
|---|
| 23 | 29 | } |
|---|
| 30 | + headers.append('Content-Type', 'application/json'); |
|---|
| 31 | + return { headers: headers }; |
|---|
| 32 | + } |
|---|
| 24 | 33 | |
|---|
| 25 | | - public create(data: any) : Observable<any> { |
|---|
| 26 | | - let url = `${this.resource}`; |
|---|
| 27 | | - return this.http.post(url, JSON.stringify(data)).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 28 | | - } |
|---|
| 34 | + public get(id?: any): Observable<any> { |
|---|
| 35 | + const url = `${this.resource}/${id || ''}`; |
|---|
| 36 | + return this.http.get(url, this.getAuthHeaders()) |
|---|
| 37 | + .map(response => response.json()) |
|---|
| 38 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 39 | + } |
|---|
| 29 | 40 | |
|---|
| 30 | | - public modify(id: any, data: any) : Observable<any> { |
|---|
| 31 | | - let url = `${this.resource}/${id}`; |
|---|
| 32 | | - return this.http.post(url, JSON.stringify(data)).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 33 | | - } |
|---|
| 41 | + public create(data: any): Observable<any> { |
|---|
| 42 | + const url = `${this.resource}`; |
|---|
| 43 | + return this.http.post(url, JSON.stringify(data), this.getAuthHeaders()) |
|---|
| 44 | + .map(response => response.json()) |
|---|
| 45 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 46 | + } |
|---|
| 34 | 47 | |
|---|
| 35 | | - public remove(id: any) : Observable<any> { |
|---|
| 36 | | - let url = `${this.resource}/${id}`; |
|---|
| 37 | | - return this.http.delete(url).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 38 | | - } |
|---|
| 39 | | - |
|---|
| 40 | | - public action(id: any, action: string, method = 'POST', params : any = {}) : Observable<any> { |
|---|
| 41 | | - let url = `${this.resource}/${id}/${action}`; |
|---|
| 42 | | - params.action = action; |
|---|
| 43 | | - var options:RequestOptionsArgs = { |
|---|
| 44 | | - url: url, |
|---|
| 45 | | - method: method, |
|---|
| 46 | | - search: (method == 'GET') && new MySearchParams(params) || undefined, |
|---|
| 47 | | - body: (method == 'POST') && JSON.stringify(params) || undefined |
|---|
| 48 | | - }; |
|---|
| 49 | | - return this.http.request(url, options).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 50 | | - } |
|---|
| 48 | + public modify(id: any, data: any): Observable<any> { |
|---|
| 49 | + const url = `${this.resource}/${id}`; |
|---|
| 50 | + // Se usa POST para modificar (según tu diseño original) |
|---|
| 51 | + return this.http.post(url, JSON.stringify(data), this.getAuthHeaders()) |
|---|
| 52 | + .map(response => response.json()) |
|---|
| 53 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 54 | + } |
|---|
| 51 | 55 | |
|---|
| 56 | + public remove(id: any): Observable<any> { |
|---|
| 57 | + const url = `${this.resource}/${id}`; |
|---|
| 58 | + return this.http.delete(url, this.getAuthHeaders()) |
|---|
| 59 | + .map(response => response.json()) |
|---|
| 60 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 61 | + } |
|---|
| 52 | 62 | |
|---|
| 63 | + public action(id: any, action: string, method: string = 'POST', params: any = {}): Observable<any> { |
|---|
| 64 | + const url = `${this.resource}/${id}/${action}`; |
|---|
| 65 | + params.action = action; |
|---|
| 66 | + const options: RequestOptionsArgs = { |
|---|
| 67 | + url: url, |
|---|
| 68 | + method: method, |
|---|
| 69 | + search: (method === 'GET') ? new MySearchParams(params) : undefined, |
|---|
| 70 | + headers: this.getAuthHeaders().headers, |
|---|
| 71 | + body: (method === 'POST') ? JSON.stringify(params) : undefined |
|---|
| 72 | + }; |
|---|
| 73 | + return this.http.request(url, options) |
|---|
| 74 | + .map(response => response.json()) |
|---|
| 75 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 76 | + } |
|---|
| 53 | 77 | } |
|---|
| .. | .. |
|---|
| 1 | 1 | import { Observable } from 'rxjs/Rx'; |
|---|
| 2 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 3 | import { Http, RequestOptions } from '@angular/http'; |
|---|
| 4 | +import { Locker } from 'angular-safeguard'; |
|---|
| 4 | 5 | import { SeCurisResourceServices } from './base'; |
|---|
| 5 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 6 | 7 | |
|---|
| .. | .. |
|---|
| 15 | 16 | |
|---|
| 16 | 17 | @Injectable() |
|---|
| 17 | 18 | export class LicenseTypesService extends SeCurisResourceServices { |
|---|
| 18 | | - constructor(http: Http, $L: LocaleService) { |
|---|
| 19 | | - super($L, http, 'licensetype'); |
|---|
| 19 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 20 | + super($L, http, store, 'licensetype'); |
|---|
| 20 | 21 | } |
|---|
| 21 | 22 | |
|---|
| 22 | 23 | |
|---|
| .. | .. |
|---|
| 2 | 2 | import { Observable } from 'rxjs/Rx'; |
|---|
| 3 | 3 | import { Injectable } from '@angular/core'; |
|---|
| 4 | 4 | import { Http, RequestOptions, ResponseContentType, Response } from '@angular/http'; |
|---|
| 5 | +import { Locker } from 'angular-safeguard'; |
|---|
| 5 | 6 | import { SeCurisResourceServices } from './base'; |
|---|
| 6 | 7 | import { saveAs } from "file-saver"; |
|---|
| 7 | 8 | |
|---|
| .. | .. |
|---|
| 78 | 79 | |
|---|
| 79 | 80 | @Injectable() |
|---|
| 80 | 81 | export class LicensesService extends SeCurisResourceServices { |
|---|
| 81 | | - constructor(http: Http, |
|---|
| 82 | | - $L: LocaleService) { |
|---|
| 83 | | - super($L, http, 'license'); |
|---|
| 82 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 83 | + super($L, http, store, 'license'); |
|---|
| 84 | 84 | } |
|---|
| 85 | 85 | |
|---|
| 86 | 86 | public getByPack(packId: number) { |
|---|
| 87 | 87 | let url = `${this.resource}?packId=${packId}`; |
|---|
| 88 | | - return this.http.get(url).map(response => response.json()); |
|---|
| 88 | + return this.http.get(url, this.getAuthHeaders()).map(response => response.json()); |
|---|
| 89 | 89 | } |
|---|
| 90 | 90 | |
|---|
| 91 | 91 | public activate(id: number) { |
|---|
| .. | .. |
|---|
| 114 | 114 | |
|---|
| 115 | 115 | public download(id: number) { |
|---|
| 116 | 116 | let url = `${this.resource}/${id}/download`; |
|---|
| 117 | | - return this.http.get(url).map((response : Response) => { |
|---|
| 118 | | - let filename = JSON.parse(response.headers.get('Content-Disposition').match(/".*"$/g)[0]); |
|---|
| 119 | | - let content = JSON.stringify(response.json(), null, 2); |
|---|
| 120 | | - saveAs( new Blob([ content ], { type : 'application/octet-stream' }), filename); |
|---|
| 121 | | - return Observable.of(true); |
|---|
| 122 | | - }).catch(err => super.processErrorResponse(err)); |
|---|
| 117 | + return this.http.get(url, this.getAuthHeaders()) |
|---|
| 118 | + .map((response: Response) => { |
|---|
| 119 | + let filename = JSON.parse(response.headers.get('Content-Disposition').match(/".*"$/g)[0]); |
|---|
| 120 | + let content = JSON.stringify(response.json(), null, 2); |
|---|
| 121 | + saveAs(new Blob([content], { type: 'application/octet-stream' }), filename); |
|---|
| 122 | + return Observable.of(true); |
|---|
| 123 | + }) |
|---|
| 124 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 123 | 125 | } |
|---|
| 124 | 126 | |
|---|
| 125 | 127 | public isActionAvailable(action:string, lic:any) { |
|---|
| .. | .. |
|---|
| 1 | 1 | import { Observable } from 'rxjs/Rx'; |
|---|
| 2 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 3 | import { Http, RequestOptions } from '@angular/http'; |
|---|
| 4 | +import { Locker } from 'angular-safeguard'; |
|---|
| 4 | 5 | import { SeCurisResourceServices } from './base'; |
|---|
| 5 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 6 | 7 | |
|---|
| .. | .. |
|---|
| 13 | 14 | @Injectable() |
|---|
| 14 | 15 | export class OrganizationsService extends SeCurisResourceServices { |
|---|
| 15 | 16 | |
|---|
| 16 | | - constructor(http: Http, $L: LocaleService) { |
|---|
| 17 | | - super($L, http, 'organization'); |
|---|
| 17 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 18 | + super($L, http, store, 'organization'); |
|---|
| 18 | 19 | } |
|---|
| 19 | 20 | |
|---|
| 20 | 21 | |
|---|
| .. | .. |
|---|
| 1 | 1 | import { Observable } from 'rxjs/Rx'; |
|---|
| 2 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 3 | import { Http, RequestOptions } from '@angular/http'; |
|---|
| 4 | +import { Locker } from 'angular-safeguard'; |
|---|
| 4 | 5 | import { SeCurisResourceServices, MySearchParams } from './base'; |
|---|
| 5 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 6 | 7 | |
|---|
| .. | .. |
|---|
| 82 | 83 | |
|---|
| 83 | 84 | @Injectable() |
|---|
| 84 | 85 | export class PacksService extends SeCurisResourceServices { |
|---|
| 85 | | - constructor(http: Http, $L: LocaleService) { |
|---|
| 86 | | - super($L, http, 'pack'); |
|---|
| 86 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 87 | + super($L, http, store, 'pack'); |
|---|
| 87 | 88 | } |
|---|
| 88 | 89 | |
|---|
| 89 | 90 | public get(filter?: PacksFilter | number | string) { |
|---|
| 90 | | - if (!filter || typeof filter === "number" || typeof filter === "string") { |
|---|
| 91 | | - return super.get(filter); |
|---|
| 92 | | - } |
|---|
| 93 | | - let searchParams = new MySearchParams(filter); |
|---|
| 94 | | - let url = `${this.resource}/?${searchParams}`; |
|---|
| 95 | | - return this.http.get(url).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 96 | | - } |
|---|
| 97 | | - |
|---|
| 91 | + if (!filter || typeof filter === "number" || typeof filter === "string") { |
|---|
| 92 | + return super.get(filter); |
|---|
| 93 | + } |
|---|
| 94 | + const searchParams = new MySearchParams(filter); |
|---|
| 95 | + const url = `${this.resource}/?${searchParams}`; |
|---|
| 96 | + // Añade las cabeceras usando el método de la base |
|---|
| 97 | + return this.http.get(url, this.getAuthHeaders()) |
|---|
| 98 | + .map(response => response.json()) |
|---|
| 99 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 100 | + } |
|---|
| 98 | 101 | |
|---|
| 99 | 102 | public activate(id: number) { |
|---|
| 100 | 103 | return super.action(id, "activate"); |
|---|
| .. | .. |
|---|
| 108 | 111 | return super.action(id, "putonhold"); |
|---|
| 109 | 112 | } |
|---|
| 110 | 113 | public nextLicCode(id: number) { |
|---|
| 111 | | - let url = `pack/${id}/next_license_code` |
|---|
| 112 | | - return this.http.get(url).map(response => response.text()); |
|---|
| 114 | + const url = `api/pack/${id}/next_license_code`; |
|---|
| 115 | + return this.http.get(url, this.getAuthHeaders()) |
|---|
| 116 | + .map(response => response.text()); |
|---|
| 113 | 117 | } |
|---|
| 114 | | - |
|---|
| 115 | | - |
|---|
| 116 | | - public isActionAvailable(action:string, pack:any) { |
|---|
| 118 | + |
|---|
| 119 | + public isActionAvailable(action:string, pack:any) { |
|---|
| 117 | 120 | var validStatuses = PACK_ACTIONS_BY_STATUS[action]; |
|---|
| 118 | 121 | return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1; |
|---|
| 119 | 122 | } |
|---|
| .. | .. |
|---|
| 1 | 1 | import { Observable } from 'rxjs/Rx'; |
|---|
| 2 | 2 | import { Injectable } from '@angular/core'; |
|---|
| 3 | 3 | import { Http, RequestOptions } from '@angular/http'; |
|---|
| 4 | +import { Locker } from 'angular-safeguard'; |
|---|
| 4 | 5 | import { SeCurisResourceServices } from './base'; |
|---|
| 5 | 6 | import { LocaleService } from '../common/i18n'; |
|---|
| 6 | 7 | |
|---|
| .. | .. |
|---|
| 18 | 19 | |
|---|
| 19 | 20 | @Injectable() |
|---|
| 20 | 21 | export class UsersService extends SeCurisResourceServices { |
|---|
| 21 | | - constructor(http: Http, $L: LocaleService) { |
|---|
| 22 | | - super($L, http, 'user'); |
|---|
| 22 | + constructor(http: Http, $L: LocaleService, store: Locker) { |
|---|
| 23 | + super($L, http, store, 'user'); |
|---|
| 23 | 24 | } |
|---|
| 24 | 25 | |
|---|
| 25 | 26 | |
|---|
| .. | .. |
|---|
| 27 | 27 | params.append('username', username); |
|---|
| 28 | 28 | params.append('password', password); |
|---|
| 29 | 29 | let options = new RequestOptions({ headers: new Headers({ "Content-Type": "application/x-www-form-urlencoded" })}); |
|---|
| 30 | | - return this.http.post('user/login', params.toString(), options) |
|---|
| 30 | + return this.http.post('api/user/login', params.toString(), options) |
|---|
| 31 | 31 | .map((resp) => this.mapLogin(resp)) |
|---|
| 32 | 32 | .catch((err) => super.processErrorResponse(err)); |
|---|
| 33 | 33 | } |
|---|
| .. | .. |
|---|
| 47 | 47 | } |
|---|
| 48 | 48 | var token = this.store.get("token"); |
|---|
| 49 | 49 | let option = new RequestOptions({ headers: new Headers({ 'X-SECURIS-TOKEN': token }) }); |
|---|
| 50 | | - return this.http.get('check', option) |
|---|
| 50 | + return this.http.get('api/check', option) |
|---|
| 51 | 51 | .map((resp) => this.mapCheck(resp)) |
|---|
| 52 | 52 | .catch((err) => super.processErrorResponse(err)); |
|---|
| 53 | 53 | } |
|---|