Search in sources :

Example 1 with NewOrganizationBean

use of io.apiman.manager.api.beans.orgs.NewOrganizationBean in project apiman by apiman.

the class OrganizationResourceImpl method create.

/**
 * @see IOrganizationResource#create(io.apiman.manager.api.beans.orgs.NewOrganizationBean)
 */
@Override
public OrganizationBean create(NewOrganizationBean bean) throws OrganizationAlreadyExistsException, InvalidNameException {
    if (config.isAdminOnlyOrgCreationEnabled()) {
        securityContext.checkAdminPermissions();
    }
    FieldValidator.validateName(bean.getName());
    List<RoleBean> autoGrantedRoles;
    SearchCriteriaBean criteria = new SearchCriteriaBean();
    criteria.setPage(1);
    criteria.setPageSize(100);
    // $NON-NLS-1$ //$NON-NLS-2$
    criteria.addFilter("autoGrant", "true", SearchCriteriaFilterOperator.bool_eq);
    try {
        autoGrantedRoles = query.findRoles(criteria).getBeans();
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
    if ("true".equals(System.getProperty("apiman.manager.require-auto-granted-org", "true"))) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        if (autoGrantedRoles.isEmpty()) {
            // $NON-NLS-1$
            throw new SystemErrorException(Messages.i18n.format("OrganizationResourceImpl.NoAutoGrantRoleAvailable"));
        }
    }
    OrganizationBean orgBean = new OrganizationBean();
    orgBean.setName(bean.getName());
    orgBean.setDescription(bean.getDescription());
    orgBean.setId(BeanUtils.idFromName(bean.getName()));
    orgBean.setCreatedOn(new Date());
    orgBean.setCreatedBy(securityContext.getCurrentUser());
    orgBean.setModifiedOn(new Date());
    orgBean.setModifiedBy(securityContext.getCurrentUser());
    try {
        // Store/persist the new organization
        storage.beginTx();
        if (storage.getOrganization(orgBean.getId()) != null) {
            throw ExceptionFactory.organizationAlreadyExistsException(bean.getName());
        }
        storage.createOrganization(orgBean);
        storage.createAuditEntry(AuditUtils.organizationCreated(orgBean, securityContext));
        // Auto-grant memberships in roles to the creator of the organization
        for (RoleBean roleBean : autoGrantedRoles) {
            String currentUser = securityContext.getCurrentUser();
            String orgId = orgBean.getId();
            RoleMembershipBean membership = RoleMembershipBean.create(currentUser, roleBean.getId(), orgId);
            membership.setCreatedOn(new Date());
            storage.createMembership(membership);
        }
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Created organization %s: %s", orgBean.getName(), orgBean));
        return orgBean;
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) SearchCriteriaBean(io.apiman.manager.api.beans.search.SearchCriteriaBean) MemberRoleBean(io.apiman.manager.api.beans.members.MemberRoleBean) RoleBean(io.apiman.manager.api.beans.idm.RoleBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) NewOrganizationBean(io.apiman.manager.api.beans.orgs.NewOrganizationBean) UpdateOrganizationBean(io.apiman.manager.api.beans.orgs.UpdateOrganizationBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) Date(java.util.Date) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)

Example 2 with NewOrganizationBean

use of io.apiman.manager.api.beans.orgs.NewOrganizationBean in project apiman by apiman.

the class DevPortalService method createHomeOrg.

public OrganizationBean createHomeOrg(NewOrganizationBean newOrg) {
    OrganizationBean existingOrg = tryAction(() -> storage.getOrganization(BeanUtils.idFromName(newOrg.getName())));
    if (existingOrg != null) {
        // First check who owns the existing organization, otherwise we could get into trouble by letting people spam create orgs.
        if (securityContext.hasPermission(PermissionType.clientEdit, existingOrg.getId())) {
            OrganizationAlreadyExistsException ex = ExceptionFactory.organizationAlreadyExistsException(existingOrg.getName());
            LOG.error(ex, "Tried to create a new home org for the developer, but one already exists where they have clientEdit permissions");
            throw ex;
        }
        // Use a name with a randomised suffix in the case that someone already created an organization with a user's name (e.g. FooUser-70ac3d)
        String newOrgId = newOrg.getName() + UUID.randomUUID().toString().substring(0, 6);
        LOG.warn("We tried to create a home organization for the user {0}, but it already existed. " + "This is likely due to another user coincidentally creating an org with the same name " + "An organization with a random suffix will be created: {1}.", securityContext.getCurrentUser(), newOrgId);
        newOrg.setName(newOrgId);
    }
    LOG.info("Creating home org {0} for {1}...", newOrg.getName(), securityContext.getCurrentUser());
    return orgService.createOrg(newOrg);
}
Also used : OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) NewOrganizationBean(io.apiman.manager.api.beans.orgs.NewOrganizationBean)

Example 3 with NewOrganizationBean

use of io.apiman.manager.api.beans.orgs.NewOrganizationBean in project apiman by apiman.

the class OrganizationService method createOrg.

public OrganizationBean createOrg(NewOrganizationBean bean) throws OrganizationAlreadyExistsException, InvalidNameException {
    FieldValidator.validateName(bean.getName());
    List<RoleBean> autoGrantedRoles;
    SearchCriteriaBean criteria = new SearchCriteriaBean();
    criteria.setPage(1);
    criteria.setPageSize(100);
    // $NON-NLS-1$ //$NON-NLS-2$
    criteria.addFilter("autoGrant", "true", SearchCriteriaFilterOperator.bool_eq);
    try {
        autoGrantedRoles = query.findRoles(criteria).getBeans();
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
    if ("true".equals(System.getProperty("apiman.manager.require-auto-granted-org", "true"))) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        if (autoGrantedRoles.isEmpty()) {
            // $NON-NLS-1$
            throw new SystemErrorException(Messages.i18n.format("OrganizationResourceImpl.NoAutoGrantRoleAvailable"));
        }
    }
    return tryAction(() -> {
        OrganizationBean orgBean = new OrganizationBean();
        orgBean.setName(bean.getName());
        orgBean.setDescription(bean.getDescription());
        orgBean.setId(BeanUtils.idFromName(bean.getName()));
        orgBean.setCreatedOn(new Date());
        orgBean.setCreatedBy(securityContext.getCurrentUser());
        orgBean.setModifiedOn(new Date());
        orgBean.setModifiedBy(securityContext.getCurrentUser());
        // Store/persist the new organization
        if (storage.getOrganization(orgBean.getId()) != null) {
            throw ExceptionFactory.organizationAlreadyExistsException(bean.getName());
        }
        storage.createOrganization(orgBean);
        storage.createAuditEntry(AuditUtils.organizationCreated(orgBean, securityContext));
        // Auto-grant memberships in roles to the creator of the organization
        for (RoleBean roleBean : autoGrantedRoles) {
            String currentUser = securityContext.getCurrentUser();
            String orgId = orgBean.getId();
            RoleMembershipBean membership = RoleMembershipBean.create(currentUser, roleBean.getId(), orgId);
            membership.setCreatedOn(new Date());
            storage.createMembership(membership);
        }
        // $NON-NLS-1$
        LOGGER.debug(String.format("Created organization %s: %s", orgBean.getName(), orgBean));
        return orgBean;
    });
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) SearchCriteriaBean(io.apiman.manager.api.beans.search.SearchCriteriaBean) RoleBean(io.apiman.manager.api.beans.idm.RoleBean) MemberRoleBean(io.apiman.manager.api.beans.members.MemberRoleBean) UpdateOrganizationBean(io.apiman.manager.api.beans.orgs.UpdateOrganizationBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) NewOrganizationBean(io.apiman.manager.api.beans.orgs.NewOrganizationBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) Date(java.util.Date)

Aggregations

NewOrganizationBean (io.apiman.manager.api.beans.orgs.NewOrganizationBean)3 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)3 RoleBean (io.apiman.manager.api.beans.idm.RoleBean)2 RoleMembershipBean (io.apiman.manager.api.beans.idm.RoleMembershipBean)2 MemberRoleBean (io.apiman.manager.api.beans.members.MemberRoleBean)2 UpdateOrganizationBean (io.apiman.manager.api.beans.orgs.UpdateOrganizationBean)2 SearchCriteriaBean (io.apiman.manager.api.beans.search.SearchCriteriaBean)2 StorageException (io.apiman.manager.api.core.exceptions.StorageException)2 OrganizationAlreadyExistsException (io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException)2 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)1 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)1 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)1 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)1 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)1 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)1 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)1 ClientAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException)1 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)1 ClientVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException)1 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)1