use of io.apiman.manager.api.beans.idm.RoleBean in project apiman by apiman.
the class EsMarshalling method unmarshallRole.
/**
* Unmarshals the given map source into a bean.
* @param source the source
* @return the role
*/
public static RoleBean unmarshallRole(Map<String, Object> source) {
if (source == null) {
return null;
}
RoleBean bean = new RoleBean();
bean.setId(asString(source.get("id")));
bean.setName(asString(source.get("name")));
bean.setDescription(asString(source.get("description")));
bean.setCreatedBy(asString(source.get("createdBy")));
bean.setCreatedOn(asDate(source.get("createdOn")));
bean.setAutoGrant(asBoolean(source.get("autoGrant")));
@SuppressWarnings("unchecked") List<Object> permissions = (List<Object>) source.get("permissions");
if (permissions != null && !permissions.isEmpty()) {
bean.setPermissions(new HashSet<>());
for (Object permission : permissions) {
bean.getPermissions().add(asEnum(permission, PermissionType.class));
}
}
postMarshall(bean);
return bean;
}
use of io.apiman.manager.api.beans.idm.RoleBean in project apiman by apiman.
the class EsMarshallingTest method testMarshallRoleBean.
/**
* Test method for {@link io.apiman.manager.api.es.EsMarshalling#marshall(io.apiman.manager.api.beans.idm.RoleBean)}.
*/
@Test
public void testMarshallRoleBean() throws Exception {
RoleBean bean = createBean(RoleBean.class);
XContentBuilder builder = EsMarshalling.marshall(bean);
Assert.assertEquals("{\"id\":\"ID\",\"name\":\"NAME\",\"description\":\"DESCRIPTION\",\"createdBy\":\"CREATEDBY\",\"createdOn\":1,\"autoGrant\":true,\"permissions\":[\"orgView\",\"orgEdit\"]}", Strings.toString(builder));
}
use of io.apiman.manager.api.beans.idm.RoleBean 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.idm.RoleBean 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());
});
}
use of io.apiman.manager.api.beans.idm.RoleBean in project apiman by apiman.
the class RoleResourceImpl method delete.
/**
* @see IRoleResource#delete(java.lang.String)
*/
@Override
public void delete(String roleId) throws RoleNotFoundException, NotAuthorizedException {
securityContext.checkAdminPermissions();
RoleBean bean = get(roleId);
try {
storage.deleteRole(bean);
} catch (StorageException e) {
throw new SystemErrorException(e);
}
}
Aggregations