use of io.apiman.manager.api.beans.search.SearchCriteriaBean 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);
}
}
use of io.apiman.manager.api.beans.search.SearchCriteriaBean in project apiman by apiman.
the class RoleResourceImpl method list.
/**
* @see IRoleResource#list()
*/
@Override
public List<RoleBean> list() {
// No permission check needed
try {
SearchCriteriaBean criteria = new SearchCriteriaBean();
// $NON-NLS-1$
criteria.setOrder("name", true);
// Hide sensitive data and set only needed data for the UI
if (securityContext.isAdmin()) {
return query.findRoles(criteria).getBeans();
} else {
List<RoleBean> roles = new ArrayList<>();
for (RoleBean role : query.findRoles(criteria).getBeans()) {
roles.add(RestHelper.hideSensitiveDataFromRoleBean(securityContext, role));
}
return roles;
}
} catch (StorageException e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.search.SearchCriteriaBean in project apiman by apiman.
the class JpaStorage method findOrganizations.
/**
* {@inheritDoc}
*/
@Override
public SearchResultsBean<OrganizationSummaryBean> findOrganizations(SearchCriteriaBean criteria, PermissionConstraint permissionConstraint) throws StorageException {
Consumer<CriteriaBuilder<OrganizationBean>> constraintFunc = builder -> {
};
if (permissionConstraint.isConstrained()) {
// With constraint, first allow the user's explicitly permitted orgs, plus orgs with discoverable APIs.
constraintFunc = (builder) -> builder.whereOr().where(OrganizationBean_.ID).in(permissionConstraint.getPermittedOrgs()).where("org.id").in().from(DiscoverabilityEntity.class, "d").select("d.orgId").where("d.discoverability").isNotNull().where("d.discoverability").in(permissionConstraint.getAllowedDiscoverabilities()).end().endOr();
}
SearchResultsBean<OrganizationBean> orgs = find(criteria, List.of(new OrderByBean(true, OrganizationBean_.ID)), constraintFunc, OrganizationBean.class, "org", true);
SearchResultsBean<OrganizationSummaryBean> rval = new SearchResultsBean<>();
rval.setTotalSize(orgs.getTotalSize());
List<OrganizationBean> beans = orgs.getBeans();
for (OrganizationBean bean : beans) {
OrganizationSummaryBean osb = new OrganizationSummaryBean();
osb.setId(bean.getId());
osb.setName(bean.getName());
osb.setDescription(bean.getDescription());
rval.getBeans().add(osb);
}
return rval;
}
use of io.apiman.manager.api.beans.search.SearchCriteriaBean in project apiman by apiman.
the class JpaStorage method findPlans.
/**
* {@inheritDoc}
*/
@Override
public SearchResultsBean<PlanSummaryBean> findPlans(String organizationId, SearchCriteriaBean criteria, PermissionConstraint permissionConstraint) throws StorageException {
Consumer<CriteriaBuilder<PlanBean>> constraintFunc = builder -> {
};
if (permissionConstraint.isConstrained()) {
constraintFunc = builder -> builder.whereOr().where("plan.organization.id").in(permissionConstraint.getPermittedOrgs()).where("plan.id").in().from(DiscoverabilityEntity.class, "d").select("d.planId").where("d.orgId").eq(organizationId).where("d.planId").isNotNull().where("d.discoverability").in(permissionConstraint.getAllowedDiscoverabilities()).end().endOr();
}
SearchResultsBean<PlanBean> result = find(criteria, List.of(new OrderByBean(true, PlanBean_.ID), new OrderByBean(true, "organization.id")), constraintFunc, PlanBean.class, "plan", true);
// TODO(msavy): replace with projection or mapping
SearchResultsBean<PlanSummaryBean> rval = new SearchResultsBean<>();
rval.setTotalSize(result.getTotalSize());
List<PlanBean> plans = result.getBeans();
rval.setBeans(new ArrayList<>(plans.size()));
for (PlanBean plan : plans) {
PlanSummaryBean summary = new PlanSummaryBean();
OrganizationBean organization = plan.getOrganization();
summary.setId(plan.getId());
summary.setName(plan.getName());
summary.setDescription(plan.getDescription());
summary.setOrganizationId(plan.getOrganization().getId());
summary.setOrganizationName(organization.getName());
rval.getBeans().add(summary);
}
return rval;
}
use of io.apiman.manager.api.beans.search.SearchCriteriaBean in project apiman by apiman.
the class JpaStorage method auditEntity.
/**
* {@inheritDoc}
*/
@Override
public <T> SearchResultsBean<AuditEntryBean> auditEntity(String organizationId, String entityId, String entityVersion, Class<T> type, PagingBean paging) throws StorageException {
SearchCriteriaBean criteria = new SearchCriteriaBean();
if (paging != null) {
criteria.setPaging(paging);
} else {
criteria.setPage(1);
criteria.setPageSize(20);
}
criteria.setOrder("id", false);
if (organizationId != null) {
criteria.addFilter("organizationId", organizationId, SearchCriteriaFilterOperator.eq);
}
if (entityId != null) {
criteria.addFilter("entityId", entityId, SearchCriteriaFilterOperator.eq);
}
if (entityVersion != null) {
criteria.addFilter("entityVersion", entityVersion, SearchCriteriaFilterOperator.eq);
}
if (type != null) {
AuditEntityType entityType = null;
if (type == OrganizationBean.class) {
entityType = AuditEntityType.Organization;
} else if (type == ClientBean.class) {
entityType = AuditEntityType.Client;
} else if (type == ApiBean.class) {
entityType = AuditEntityType.Api;
} else if (type == PlanBean.class) {
entityType = AuditEntityType.Plan;
}
if (entityType != null) {
criteria.addFilter("entityType", entityType.name(), SearchCriteriaFilterOperator.eq);
}
}
return find(criteria, List.of(new OrderByBean(true, AuditEntryBean_.ID)), AuditEntryBean.class, true);
}
Aggregations