Search in sources :

Example 6 with ApplicationException

use of io.jans.ca.plugin.adminui.model.exception.ApplicationException in project jans by JanssenProject.

the class LicenseDetailsService method updateLicenseDetails.

public LicenseResponse updateLicenseDetails(LicenseRequest licenseRequest) throws ApplicationException {
    LicenseResponse licenseResponse = new LicenseResponse();
    log.debug("LicenseRequest params: {}", licenseRequest);
    try {
        if (Strings.isNullOrEmpty(licenseRequest.getValidityPeriod())) {
            log.error(ErrorResponse.LICENSE_VALIDITY_PERIOD_NOT_FOUND.getDescription());
            throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.LICENSE_VALIDITY_PERIOD_NOT_FOUND.getDescription());
        }
        if (licenseRequest.getMaxActivations() < 1) {
            log.error(ErrorResponse.INVALID_MAXIMUM_ACTIVATIONS.getDescription());
            throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.INVALID_MAXIMUM_ACTIVATIONS.getDescription());
        }
        if (licenseRequest.getValidityPeriod().length() > 10) {
            licenseRequest.setValidityPeriod(licenseRequest.getValidityPeriod().substring(0, 10));
        }
        AUIConfiguration auiConfiguration = auiConfigurationService.getAUIConfiguration();
        ManagementConfiguration configuration = ManagementConfiguration.builder().managementKey(auiConfiguration.getLicenseConfiguration().getManagementKey()).requestLogging(feign.Logger.Level.FULL).build();
        // search license by license-key
        License activeLicense = auiConfiguration.getLicenseConfiguration().getLicenseManager().getCurrent();
        if (activeLicense == null) {
            licenseResponse.setLicenseEnabled(false);
            return licenseResponse;
        }
        SearchLicensesRequest request = SearchLicensesRequest.builder().licenseKey(activeLicense.getIdentity().getLicenseKey()).limit(1).build();
        com.licensespring.management.LicenseService licenseService = new com.licensespring.management.LicenseService(configuration);
        SearchResult<BackOfficeLicense> response = licenseService.searchLicenses(request);
        // update license details
        UpdateLicenseRequest update = UpdateLicenseRequest.builder().isTrial(false).validityPeriod(licenseRequest.getValidityPeriod()).maxActivations(licenseRequest.getMaxActivations()).enabled(licenseRequest.getLicenseActive()).build();
        BackOfficeLicense updated = licenseService.updateLicense(response.getResults().get(0).getId(), update);
        // create LicenseResponse
        licenseResponse.setLicenseEnabled(true);
        licenseResponse.setProductName(activeLicense.getProduct().getProductName());
        licenseResponse.setProductCode(activeLicense.getProduct().getShortCode());
        licenseResponse.setLicenseType(activeLicense.getData().getLicenseType().name());
        licenseResponse.setLicenseKey(activeLicense.getIdentity().getLicenseKey());
        licenseResponse.setCompanyName(activeLicense.getData().getCustomer().getCompanyName());
        licenseResponse.setCustomerEmail(activeLicense.getData().getCustomer().getEmail());
        licenseResponse.setCustomerFirstName(activeLicense.getData().getCustomer().getFirstName());
        licenseResponse.setCustomerLastName(activeLicense.getData().getCustomer().getLastName());
        licenseResponse.setMaxActivations(updated.getMaxActivations());
        licenseResponse.setLicenseActive(updated.getActive());
        licenseResponse.setValidityPeriod(updated.getValidityPeriod());
        return licenseResponse;
    } catch (Exception e) {
        log.error(ErrorResponse.UPDATE_LICENSE_DETAILS_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.UPDATE_LICENSE_DETAILS_ERROR.getDescription());
    }
}
Also used : UpdateLicenseRequest(com.licensespring.management.dto.request.UpdateLicenseRequest) ManagementConfiguration(com.licensespring.management.ManagementConfiguration) BackOfficeLicense(com.licensespring.management.model.BackOfficeLicense) ActivationLicense(com.licensespring.model.ActivationLicense) License(com.licensespring.License) SearchLicensesRequest(com.licensespring.management.dto.request.SearchLicensesRequest) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) LicenseResponse(io.jans.ca.plugin.adminui.model.auth.LicenseResponse) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) AUIConfiguration(io.jans.ca.plugin.adminui.model.config.AUIConfiguration) BackOfficeLicense(com.licensespring.management.model.BackOfficeLicense)

Example 7 with ApplicationException

use of io.jans.ca.plugin.adminui.model.exception.ApplicationException in project jans by JanssenProject.

the class LicenseResource method updateLicenseDetails.

@PUT
@Path(LICENSE_DETAILS)
@ProtectedApi(scopes = { SCOPE_LICENSE_WRITE })
@Produces(MediaType.APPLICATION_JSON)
public Response updateLicenseDetails(@Valid @NotNull LicenseRequest licenseRequest) {
    try {
        log.info("Trying to update license details.");
        LicenseResponse licenseResponse = licenseDetailsService.updateLicenseDetails(licenseRequest);
        return Response.ok(licenseResponse).build();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.UPDATE_LICENSE_DETAILS_ERROR.getDescription(), e);
        return Response.status(e.getErrorCode()).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(ErrorResponse.UPDATE_LICENSE_DETAILS_ERROR.getDescription(), e);
        return Response.serverError().entity(ErrorResponse.UPDATE_LICENSE_DETAILS_ERROR.getDescription()).build();
    }
}
Also used : LicenseResponse(io.jans.ca.plugin.adminui.model.auth.LicenseResponse) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) ProtectedApi(io.jans.configapi.core.rest.ProtectedApi)

Example 8 with ApplicationException

use of io.jans.ca.plugin.adminui.model.exception.ApplicationException in project jans by JanssenProject.

the class UserManagementService method addPermission.

public List<AdminPermission> addPermission(AdminPermission permissionArg) throws ApplicationException {
    try {
        AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
        List<AdminPermission> permissions = adminConf.getDynamic().getPermissions();
        if (permissions.contains(permissionArg)) {
            return adminConf.getDynamic().getPermissions();
        }
        permissions.add(permissionArg);
        adminConf.getDynamic().setPermissions(permissions);
        entryManager.merge(adminConf);
        return adminConf.getDynamic().getPermissions();
    } catch (Exception e) {
        log.error(ErrorResponse.SAVE_ADMIUI_PERMISSIONS_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.SAVE_ADMIUI_PERMISSIONS_ERROR.getDescription());
    }
}
Also used : ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) AdminPermission(io.jans.as.model.config.adminui.AdminPermission) AdminConf(io.jans.as.model.config.adminui.AdminConf) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Example 9 with ApplicationException

use of io.jans.ca.plugin.adminui.model.exception.ApplicationException in project jans by JanssenProject.

the class UserManagementService method deleteRole.

public List<AdminRole> deleteRole(String role) throws ApplicationException {
    try {
        AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
        List<RolePermissionMapping> roleScopeMapping = adminConf.getDynamic().getRolePermissionMapping().stream().filter(ele -> ele.getRole().equalsIgnoreCase(role)).collect(Collectors.toList());
        if (!roleScopeMapping.isEmpty()) {
            Optional<RolePermissionMapping> rolePermissionMappingOptional = roleScopeMapping.stream().findAny();
            List<String> permissions = Lists.newArrayList();
            if (rolePermissionMappingOptional.isPresent()) {
                permissions = rolePermissionMappingOptional.get().getPermissions();
            }
            if (!permissions.isEmpty()) {
                log.error(ErrorResponse.UNABLE_TO_DELETE_ROLE_MAPPED_TO_PERMISSIONS.getDescription());
                throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.UNABLE_TO_DELETE_ROLE_MAPPED_TO_PERMISSIONS.getDescription());
            }
        }
        List<AdminRole> roles = adminConf.getDynamic().getRoles();
        if (isFalse(getRoleObjByName(role).getDeletable())) {
            log.error(ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
            throw new ApplicationException(Response.Status.BAD_REQUEST.getStatusCode(), ErrorResponse.ROLE_MARKED_UNDELETABLE.getDescription());
        }
        roles.removeIf(ele -> ele.getRole().equals(role));
        adminConf.getDynamic().setRoles(roles);
        entryManager.merge(adminConf);
        return adminConf.getDynamic().getRoles();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.DELETE_ADMIUI_ROLES_ERROR.getDescription());
        throw e;
    } catch (Exception e) {
        log.error(ErrorResponse.DELETE_ADMIUI_ROLES_ERROR.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.DELETE_ADMIUI_ROLES_ERROR.getDescription());
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) AdminPermission(io.jans.as.model.config.adminui.AdminPermission) Singleton(javax.inject.Singleton) PersistenceEntryManager(io.jans.orm.PersistenceEntryManager) Collectors(java.util.stream.Collectors) AdminRole(io.jans.as.model.config.adminui.AdminRole) ErrorResponse(io.jans.ca.plugin.adminui.utils.ErrorResponse) AdminConf(io.jans.as.model.config.adminui.AdminConf) Inject(javax.inject.Inject) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) Response(javax.ws.rs.core.Response) CollectionUtils(org.apache.commons.collections.CollectionUtils) RolePermissionMapping(io.jans.as.model.config.adminui.RolePermissionMapping) Lists(com.google.api.client.util.Lists) RolePermissionMapping(io.jans.as.model.config.adminui.RolePermissionMapping) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) AdminConf(io.jans.as.model.config.adminui.AdminConf) AdminRole(io.jans.as.model.config.adminui.AdminRole) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Example 10 with ApplicationException

use of io.jans.ca.plugin.adminui.model.exception.ApplicationException in project jans by JanssenProject.

the class UserManagementService method mapPermissionsToRole.

public List<RolePermissionMapping> mapPermissionsToRole(RolePermissionMapping rolePermissionMappingArg) throws ApplicationException {
    try {
        AdminConf adminConf = entryManager.find(AdminConf.class, CONFIG_DN);
        List<RolePermissionMapping> roleScopeMappingList = getRolePermMapByRole(adminConf, rolePermissionMappingArg);
        if (roleScopeMappingList == null || roleScopeMappingList.isEmpty()) {
            RolePermissionMapping rolePermissionMapping = new RolePermissionMapping();
            rolePermissionMapping.setRole(rolePermissionMappingArg.getRole());
            roleScopeMappingList = Lists.newArrayList();
            roleScopeMappingList.add(rolePermissionMapping);
        }
        // remove duplicate permissions
        Set<String> scopesSet = new LinkedHashSet<>(rolePermissionMappingArg.getPermissions());
        List<String> combinedScopes = new ArrayList<>(scopesSet);
        if (adminConf.getDynamic().getRolePermissionMapping().stream().anyMatch(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole()))) {
            adminConf.getDynamic().getRolePermissionMapping().stream().filter(ele -> ele.getRole().equalsIgnoreCase(rolePermissionMappingArg.getRole())).collect(Collectors.toList()).forEach(ele -> ele.setPermissions(combinedScopes));
        } else {
            roleScopeMappingList.forEach(ele -> ele.setPermissions(combinedScopes));
            adminConf.getDynamic().getRolePermissionMapping().addAll(roleScopeMappingList);
        }
        entryManager.merge(adminConf);
        return adminConf.getDynamic().getRolePermissionMapping();
    } catch (ApplicationException e) {
        log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription());
        throw e;
    } catch (Exception e) {
        log.error(ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription(), e);
        throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_MAPPING_ROLE_PERMISSION.getDescription());
    }
}
Also used : RolePermissionMapping(io.jans.as.model.config.adminui.RolePermissionMapping) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException) AdminConf(io.jans.as.model.config.adminui.AdminConf) ApplicationException(io.jans.ca.plugin.adminui.model.exception.ApplicationException)

Aggregations

ApplicationException (io.jans.ca.plugin.adminui.model.exception.ApplicationException)18 AdminConf (io.jans.as.model.config.adminui.AdminConf)10 AdminPermission (io.jans.as.model.config.adminui.AdminPermission)6 AdminRole (io.jans.as.model.config.adminui.AdminRole)5 RolePermissionMapping (io.jans.as.model.config.adminui.RolePermissionMapping)5 TokenResponse (io.jans.ca.plugin.adminui.model.auth.TokenResponse)5 AUIConfiguration (io.jans.ca.plugin.adminui.model.config.AUIConfiguration)4 ErrorResponse (io.jans.ca.plugin.adminui.utils.ErrorResponse)4 Response (javax.ws.rs.core.Response)4 Lists (com.google.api.client.util.Lists)3 PersistenceEntryManager (io.jans.orm.PersistenceEntryManager)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 Singleton (javax.inject.Singleton)3 CollectionUtils (org.apache.commons.collections.CollectionUtils)3 Logger (org.slf4j.Logger)3 TokenRequest (io.jans.as.client.TokenRequest)2 Jwt (io.jans.as.model.jwt.Jwt)2