Search in sources :

Example 1 with RoleMembershipBean

use of io.apiman.manager.api.beans.idm.RoleMembershipBean in project apiman by apiman.

the class EsStorage method getUserMemberships.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getUserMemberships(java.lang.String)
 */
@Override
public Set<RoleMembershipBean> getUserMemberships(String userId) throws StorageException {
    try {
        @SuppressWarnings("nls") QueryBuilder qb = QueryBuilders.termQuery("userId", userId);
        SearchSourceBuilder builder = new SearchSourceBuilder().query(qb).size(500);
        // $NON-NLS-1$
        List<SearchHit> hits = listEntities(INDEX_MANAGER_POSTFIX_ROLE_MEMBERSHIP, builder);
        Set<RoleMembershipBean> rval = new HashSet<>();
        for (SearchHit hit : hits) {
            RoleMembershipBean roleMembership = EsMarshalling.unmarshallRoleMembership(hit.getSourceAsMap());
            rval.add(roleMembership);
        }
        return rval;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) SearchHit(org.elasticsearch.search.SearchHit) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) StorageException(io.apiman.manager.api.core.exceptions.StorageException) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) HashSet(java.util.HashSet)

Example 2 with RoleMembershipBean

use of io.apiman.manager.api.beans.idm.RoleMembershipBean in project apiman by apiman.

the class OrganizationResourceImpl method grant.

/**
 * @see IOrganizationResource#grant(java.lang.String, io.apiman.manager.api.beans.idm.GrantRolesBean)
 */
@Override
public void grant(String organizationId, GrantRolesBean bean) throws OrganizationNotFoundException, RoleNotFoundException, UserNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.orgAdmin, organizationId);
    // Verify that the references are valid.
    get(organizationId);
    users.get(bean.getUserId());
    for (String roleId : bean.getRoleIds()) {
        roles.get(roleId);
    }
    MembershipData auditData = new MembershipData();
    auditData.setUserId(bean.getUserId());
    try {
        storage.beginTx();
        for (String roleId : bean.getRoleIds()) {
            RoleMembershipBean membership = RoleMembershipBean.create(bean.getUserId(), roleId, organizationId);
            membership.setCreatedOn(new Date());
            // If the membership already exists, that's fine!
            if (storage.getMembership(bean.getUserId(), roleId, organizationId) == null) {
                storage.createMembership(membership);
            }
            auditData.addRole(roleId);
        }
        storage.createAuditEntry(AuditUtils.membershipGranted(organizationId, auditData, securityContext));
        storage.commitTx();
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
}
Also used : MembershipData(io.apiman.manager.api.beans.audit.data.MembershipData) RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) 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 3 with RoleMembershipBean

use of io.apiman.manager.api.beans.idm.RoleMembershipBean 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 4 with RoleMembershipBean

use of io.apiman.manager.api.beans.idm.RoleMembershipBean in project apiman by apiman.

the class OrganizationService method grant.

public void grant(String organizationId, GrantRolesBean bean) throws OrganizationNotFoundException, RoleNotFoundException, UserNotFoundException, NotAuthorizedException {
    // Verify that the references are valid.
    getOrg(organizationId);
    userService.getUserById(bean.getUserId());
    for (String roleId : bean.getRoleIds()) {
        roles.get(roleId);
    }
    MembershipData auditData = new MembershipData();
    auditData.setUserId(bean.getUserId());
    tryAction(() -> {
        for (String roleId : bean.getRoleIds()) {
            RoleMembershipBean membership = RoleMembershipBean.create(bean.getUserId(), roleId, organizationId);
            membership.setCreatedOn(new Date());
            // If the membership already exists, that's fine!
            if (storage.getMembership(bean.getUserId(), roleId, organizationId) == null) {
                storage.createMembership(membership);
            }
            auditData.addRole(roleId);
        }
        storage.createAuditEntry(AuditUtils.membershipGranted(organizationId, auditData, securityContext));
    });
}
Also used : MembershipData(io.apiman.manager.api.beans.audit.data.MembershipData) RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) Date(java.util.Date)

Example 5 with RoleMembershipBean

use of io.apiman.manager.api.beans.idm.RoleMembershipBean in project apiman by apiman.

the class OrganizationService method listMembers.

public List<MemberBean> listMembers(String organizationId) throws OrganizationNotFoundException, NotAuthorizedException {
    getOrg(organizationId);
    return tryAction(() -> {
        Set<RoleMembershipBean> memberships = query.getOrgMemberships(organizationId);
        TreeMap<String, MemberBean> members = new TreeMap<>();
        for (RoleMembershipBean membershipBean : memberships) {
            String userId = membershipBean.getUserId();
            String roleId = membershipBean.getRoleId();
            RoleBean role = storage.getRole(roleId);
            // Role does not exist!
            if (role == null) {
                continue;
            }
            MemberBean member = members.get(userId);
            if (member == null) {
                UserBean user = storage.getUser(userId);
                member = new MemberBean();
                member.setEmail(user.getEmail());
                member.setUserId(userId);
                member.setUserName(user.getFullName());
                member.setRoles(new ArrayList<>());
                members.put(userId, member);
            }
            MemberRoleBean mrb = new MemberRoleBean();
            mrb.setRoleId(roleId);
            mrb.setRoleName(role.getName());
            member.getRoles().add(mrb);
            if (member.getJoinedOn() == null || membershipBean.getCreatedOn().compareTo(member.getJoinedOn()) < 0) {
                member.setJoinedOn(membershipBean.getCreatedOn());
            }
        }
        return new ArrayList<>(members.values());
    });
}
Also used : RoleMembershipBean(io.apiman.manager.api.beans.idm.RoleMembershipBean) MemberBean(io.apiman.manager.api.beans.members.MemberBean) UserBean(io.apiman.manager.api.beans.idm.UserBean) RoleBean(io.apiman.manager.api.beans.idm.RoleBean) MemberRoleBean(io.apiman.manager.api.beans.members.MemberRoleBean) MemberRoleBean(io.apiman.manager.api.beans.members.MemberRoleBean) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap)

Aggregations

RoleMembershipBean (io.apiman.manager.api.beans.idm.RoleMembershipBean)13 StorageException (io.apiman.manager.api.core.exceptions.StorageException)8 RoleBean (io.apiman.manager.api.beans.idm.RoleBean)5 IOException (java.io.IOException)5 MemberRoleBean (io.apiman.manager.api.beans.members.MemberRoleBean)4 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)4 Date (java.util.Date)3 MembershipData (io.apiman.manager.api.beans.audit.data.MembershipData)2 UserBean (io.apiman.manager.api.beans.idm.UserBean)2 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)2 SearchCriteriaBean (io.apiman.manager.api.beans.search.SearchCriteriaBean)2 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)2 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)2 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)2 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)2 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)2 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)2 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)2 ClientAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException)2 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)2