Search in sources :

Example 11 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method subscribeEmail.

@PUT
@Path("/email/subscription/{bundleName}/{applicationName}/{type}")
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@Transactional
public boolean subscribeEmail(@Context SecurityContext sec, @PathParam("bundleName") String bundleName, @PathParam("applicationName") String applicationName, @PathParam("type") EmailSubscriptionType type) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    Application app = applicationRepository.getApplication(bundleName, applicationName);
    if (app == null) {
        throw new NotFoundException();
    } else {
        return emailSubscriptionRepository.subscribe(principal.getAccount(), principal.getName(), bundleName, applicationName, type);
    }
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) NotFoundException(javax.ws.rs.NotFoundException) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT) Transactional(javax.transaction.Transactional)

Example 12 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method deleteEndpoint.

@DELETE
@Path("/{id}")
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@APIResponse(responseCode = "204", description = "The integration has been deleted", content = @Content(schema = @Schema(type = SchemaType.STRING)))
@Transactional
public Response deleteEndpoint(@Context SecurityContext sec, @PathParam("id") UUID id) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    EndpointType endpointType = endpointRepository.getEndpointTypeById(principal.getAccount(), id);
    checkSystemEndpoint(endpointType);
    if (obEnabled) {
        Endpoint e = endpointRepository.getEndpoint(principal.getAccount(), id);
        if (e != null) {
            EndpointProperties properties = e.getProperties();
            if (properties instanceof CamelProperties) {
                CamelProperties cp = (CamelProperties) properties;
                // Special case wrt OpenBridge
                if (e.getSubType().equals("slack")) {
                    String processorId = cp.getExtras().get(OB_PROCESSOR_ID);
                    if (processorId != null) {
                        // Should not be null under normal operations.
                        try {
                            bridgeApiService.deleteProcessor(bridge.getId(), processorId, bridgeAuth.getToken());
                        } catch (Exception ex) {
                            LOGGER.warn("Removal of OB processor failed:" + ex.getMessage());
                        // Nothing more we can do
                        }
                    } else {
                        LOGGER.warn("ProcessorId was null for endpoint " + id.toString());
                    }
                }
            }
        }
    }
    endpointRepository.deleteEndpoint(principal.getAccount(), id);
    return Response.noContent().build();
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) EndpointType(com.redhat.cloud.notifications.models.EndpointType) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Transactional(javax.transaction.Transactional)

Example 13 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method updateEndpoint.

@PUT
@Path("/{id}")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@APIResponse(responseCode = "200", content = @Content(schema = @Schema(type = SchemaType.STRING)))
@Transactional
public Response updateEndpoint(@Context SecurityContext sec, @PathParam("id") UUID id, @NotNull @Valid Endpoint endpoint) {
    // This prevents from updating an endpoint from whatever EndpointType to a system EndpointType
    checkSystemEndpoint(endpoint.getType());
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    endpoint.setAccountId(principal.getAccount());
    endpoint.setId(id);
    EndpointType endpointType = endpointRepository.getEndpointTypeById(principal.getAccount(), id);
    // This prevents from updating an endpoint from system EndpointType to a whatever EndpointType
    checkSystemEndpoint(endpointType);
    endpointRepository.updateEndpoint(endpoint);
    return Response.ok().build();
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) EndpointType(com.redhat.cloud.notifications.models.EndpointType) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT) Transactional(javax.transaction.Transactional)

Example 14 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method unsubscribeEmail.

@DELETE
@Path("/email/subscription/{bundleName}/{applicationName}/{type}")
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@Transactional
public boolean unsubscribeEmail(@Context SecurityContext sec, @PathParam("bundleName") String bundleName, @PathParam("applicationName") String applicationName, @PathParam("type") EmailSubscriptionType type) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    Application app = applicationRepository.getApplication(bundleName, applicationName);
    if (app == null) {
        throw new NotFoundException();
    } else {
        return emailSubscriptionRepository.unsubscribe(principal.getAccount(), principal.getName(), bundleName, applicationName, type);
    }
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) NotFoundException(javax.ws.rs.NotFoundException) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 15 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class UserConfigResource method saveSettings.

@POST
@Path("/notification-preference")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
@Operation(hidden = true)
@Transactional
public Response saveSettings(@Context SecurityContext sec, @Valid SettingsValues values) {
    final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    final String account = principal.getAccount();
    final String name = principal.getName();
    final List<Boolean> subscriptionRequests = new ArrayList<>();
    values.bundles.forEach((bundleName, bundleSettingsValue) -> bundleSettingsValue.applications.forEach((applicationName, applicationSettingsValue) -> {
        Application app = applicationRepository.getApplication(bundleName, applicationName);
        applicationSettingsValue.notifications.forEach((emailSubscriptionType, subscribed) -> {
            if (subscribed) {
                if (app != null) {
                    subscriptionRequests.add(emailSubscriptionRepository.subscribe(account, name, bundleName, applicationName, emailSubscriptionType));
                }
            } else {
                subscriptionRequests.add(emailSubscriptionRepository.unsubscribe(account, name, bundleName, applicationName, emailSubscriptionType));
            }
        });
    }));
    boolean allisSuccess = subscriptionRequests.stream().allMatch(Boolean.TRUE::equals);
    Response.ResponseBuilder builder;
    if (allisSuccess) {
        builder = Response.ok();
    } else {
        // Prevent from saving
        builder = Response.serverError().entity("Storing of settings Failed.");
        builder.type("text/plain");
    }
    return builder.build();
}
Also used : PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Constants(com.redhat.cloud.notifications.Constants) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Valid(javax.validation.Valid) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) BundleSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) BadRequestException(javax.ws.rs.BadRequestException) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) BundleRepository(com.redhat.cloud.notifications.db.repositories.BundleRepository) Application(com.redhat.cloud.notifications.models.Application) ApplicationRepository(com.redhat.cloud.notifications.db.repositories.ApplicationRepository) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues) Transactional(javax.transaction.Transactional) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Bundle(com.redhat.cloud.notifications.models.Bundle) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) SettingsValueJsonForm(com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm) Operation(org.eclipse.microprofile.openapi.annotations.Operation) EntityTag(javax.ws.rs.core.EntityTag) ApplicationSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue) List(java.util.List) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) TemplateEngineClient(com.redhat.cloud.notifications.templates.TemplateEngineClient) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) ArrayList(java.util.ArrayList) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(org.eclipse.microprofile.openapi.annotations.Operation) Transactional(javax.transaction.Transactional)

Aggregations

RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)15 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)13 RolesAllowed (javax.annotation.security.RolesAllowed)12 Transactional (javax.transaction.Transactional)10 GET (javax.ws.rs.GET)7 NotFoundException (javax.ws.rs.NotFoundException)7 APIResponse (org.eclipse.microprofile.openapi.annotations.responses.APIResponse)6 CompositeEndpointType (com.redhat.cloud.notifications.models.CompositeEndpointType)5 EndpointType (com.redhat.cloud.notifications.models.EndpointType)5 BadRequestException (javax.ws.rs.BadRequestException)5 Consumes (javax.ws.rs.Consumes)5 Application (com.redhat.cloud.notifications.models.Application)4 DELETE (javax.ws.rs.DELETE)4 POST (javax.ws.rs.POST)4 PUT (javax.ws.rs.PUT)4 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)3 Endpoint (com.redhat.cloud.notifications.models.Endpoint)3 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 Response (javax.ws.rs.core.Response)3