Search in sources :

Example 1 with UserBean

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

the class SearchResourceImpl method searchUsers.

/**
 * @see io.apiman.manager.api.rest.ISearchResource#searchUsers(io.apiman.manager.api.beans.search.SearchCriteriaBean)
 */
@Override
public SearchResultsBean<UserSearchResult> searchUsers(SearchCriteriaBean criteria) throws InvalidSearchCriteriaException {
    List<UserSearchResult> users = new ArrayList<>();
    try {
        // Maybe this should be a new query in the future?
        List<UserBean> userBeans = query.findUsers(criteria).getBeans();
        for (UserBean user : userBeans) {
            users.add(new UserSearchResult(user.getUsername(), user.getFullName()));
        }
        SearchResultsBean<UserSearchResult> searchResultsBean = new SearchResultsBean<>();
        searchResultsBean.setBeans(users);
        searchResultsBean.setTotalSize(users.size());
        return searchResultsBean;
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) UserBean(io.apiman.manager.api.beans.idm.UserBean) UserSearchResult(io.apiman.manager.api.beans.search.searchResults.UserSearchResult) ArrayList(java.util.ArrayList) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with UserBean

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

the class EsMarshalling method unmarshallUser.

/**
 * Unmarshals the given map source into a bean.
 * @param source the source
 * @return the user
 */
public static UserBean unmarshallUser(Map<String, Object> source) {
    if (source == null) {
        return null;
    }
    UserBean bean = new UserBean();
    bean.setUsername(asString(source.get("username")));
    bean.setEmail(asString(source.get("email")));
    bean.setFullName(asString(source.get("fullName")));
    bean.setJoinedOn(asDate(source.get("joinedOn")));
    postMarshall(bean);
    return bean;
}
Also used : UserBean(io.apiman.manager.api.beans.idm.UserBean)

Example 3 with UserBean

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

the class EsMarshallingTest method testMarshallUserBean.

/**
 * Test method for {@link io.apiman.manager.api.es.EsMarshalling#marshall(io.apiman.manager.api.beans.idm.UserBean)}.
 */
@Test
public void testMarshallUserBean() throws Exception {
    UserBean bean = createBean(UserBean.class);
    XContentBuilder builder = EsMarshalling.marshall(bean);
    Assert.assertEquals("{\"username\":\"USERNAME\",\"email\":\"EMAIL\",\"fullName\":\"FULLNAME\",\"joinedOn\":1}", Strings.toString(builder));
}
Also used : UserBean(io.apiman.manager.api.beans.idm.UserBean) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Example 4 with UserBean

use of io.apiman.manager.api.beans.idm.UserBean 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)

Example 5 with UserBean

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

the class ActionService method approveContract.

public void approveContract(ContractActionDto action, String approverId) {
    // Must exist
    ContractBean contract = tryAction(() -> storage.getContract(action.getContractId()));
    if (contract == null) {
        throw ExceptionFactory.actionException(Messages.i18n.format("ContractDoesNotExist"));
    }
    // Must be in AwaitingApproval state (no need to approve otherwise!)
    if (contract.getStatus() != ContractStatus.AwaitingApproval) {
        throw ExceptionFactory.invalidContractStatus(ContractStatus.AwaitingApproval, contract.getStatus());
    }
    // We probably need an optimised query :-).
    ClientVersionBean cvb = contract.getClient();
    ApiVersionBean avb = contract.getApi();
    OrganizationBean org = avb.getApi().getOrganization();
    PlanVersionBean plan = contract.getPlan();
    OrganizationBean orgA = avb.getApi().getOrganization();
    OrganizationBean orgC = cvb.getClient().getOrganization();
    UserBean approver = tryAction(() -> storage.getUser(approverId));
    // Set the contract to approved state and send approved event.
    contract.setStatus(ContractStatus.Created);
    LOGGER.debug("{0} approved a contract: {1} -> {2}", approverId, contract, action);
    // In the second phase we need to check the other contracts to see whether they are all in the 'ready' state
    // If so, then it's ready to publish.
    List<ContractBean> contracts = tryAction(() -> Streams.stream((storage.getAllContracts(org.getId(), cvb.getClient().getId(), cvb.getVersion())))).collect(Collectors.toList());
    List<ContractBean> awaitingApprovalList = contracts.stream().filter(c -> c.getStatus().equals(ContractStatus.AwaitingApproval)).collect(Collectors.toList());
    if (awaitingApprovalList.size() > 0) {
        LOGGER.debug("A contract was approved, but {0} other contracts are still awaiting approval, " + "so client version {1} will remain in its pending state until the remaining contract approvals " + "are granted: {2}.", awaitingApprovalList.size(), cvb.getVersion(), awaitingApprovalList);
    } else {
        LOGGER.debug("All contracts for {0} have been approved", cvb.getVersion());
        tryAction(() -> {
            if (clientValidator.isReady(cvb)) {
                // Set client to ready status and fire change status event
                LOGGER.debug("Client set to ready as all contracts have been approved");
                cvb.setStatus(ClientStatus.Ready);
                clientAppService.fireClientStatusChangeEvent(cvb, ClientStatus.AwaitingApproval);
                // If auto-promote, then we immediately register.
                if (action.isAutoPromote()) {
                    LOGGER.debug("Auto approving: {0}", cvb);
                    registerClient(orgC.getId(), cvb.getClient().getId(), cvb.getVersion());
                }
            }
        });
    }
    // storage.flush();
    fireContractApprovedEvent(approver, contract, orgC, cvb, orgA, avb, plan);
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ContractApprovalEvent(io.apiman.manager.api.beans.events.ContractApprovalEvent) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ISecurityContext(io.apiman.manager.api.security.ISecurityContext) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) IGatewayLinkFactory(io.apiman.manager.api.gateway.IGatewayLinkFactory) ApimanEventHeaders(io.apiman.manager.api.beans.events.ApimanEventHeaders) UserBean(io.apiman.manager.api.beans.idm.UserBean) Client(io.apiman.gateway.engine.beans.Client) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) UserMapper(io.apiman.manager.api.beans.idm.UserMapper) IClientValidator(io.apiman.manager.api.core.IClientValidator) Map(java.util.Map) URI(java.net.URI) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) Contract(io.apiman.gateway.engine.beans.Contract) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) Policy(io.apiman.gateway.engine.beans.Policy) Transactional(javax.transaction.Transactional) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) EventService(io.apiman.manager.api.events.EventService) Set(java.util.Set) UUID(java.util.UUID) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) ContractStatus(io.apiman.manager.api.beans.contracts.ContractStatus) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) List(java.util.List) IApimanLogger(io.apiman.common.logging.IApimanLogger) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ContractActionDto(io.apiman.manager.api.beans.actions.ContractActionDto) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) HashMap(java.util.HashMap) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) AuditUtils(io.apiman.manager.api.rest.impl.audit.AuditUtils) ArrayList(java.util.ArrayList) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) DataAccessUtilMixin(io.apiman.manager.api.rest.impl.util.DataAccessUtilMixin) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) Iterator(java.util.Iterator) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) IStorage(io.apiman.manager.api.core.IStorage) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) Api(io.apiman.gateway.engine.beans.Api) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) Messages(io.apiman.manager.api.rest.exceptions.i18n.Messages) PlanStatus(io.apiman.manager.api.beans.plans.PlanStatus) ExceptionFactory(io.apiman.manager.api.rest.exceptions.util.ExceptionFactory) UserBean(io.apiman.manager.api.beans.idm.UserBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Aggregations

UserBean (io.apiman.manager.api.beans.idm.UserBean)11 StorageException (io.apiman.manager.api.core.exceptions.StorageException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 RoleBean (io.apiman.manager.api.beans.idm.RoleBean)2 RoleMembershipBean (io.apiman.manager.api.beans.idm.RoleMembershipBean)2 UpdateUserBean (io.apiman.manager.api.beans.idm.UpdateUserBean)2 MemberBean (io.apiman.manager.api.beans.members.MemberBean)2 MemberRoleBean (io.apiman.manager.api.beans.members.MemberRoleBean)2 Streams (com.google.common.collect.Streams)1 ApimanLoggerFactory (io.apiman.common.logging.ApimanLoggerFactory)1 IApimanLogger (io.apiman.common.logging.IApimanLogger)1 Api (io.apiman.gateway.engine.beans.Api)1 Client (io.apiman.gateway.engine.beans.Client)1 Contract (io.apiman.gateway.engine.beans.Contract)1 Policy (io.apiman.gateway.engine.beans.Policy)1 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)1 ContractActionDto (io.apiman.manager.api.beans.actions.ContractActionDto)1 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)1 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)1