use of io.apiman.manager.api.beans.idm.PermissionBean in project apiman by apiman.
the class UserResourceImpl method getInfo.
// TODO(msavy): refactor and move to service
/**
* {@inheritDoc}
*/
@Override
public CurrentUserBean getInfo() {
String userId = securityContext.getCurrentUser();
return tryAction(() -> {
CurrentUserBean currentUser = new CurrentUserBean();
UserBean user = userService.getUserById(userId);
if (user == null) {
user = new UserBean();
user.setUsername(userId);
if (securityContext.getFullName() != null) {
user.setFullName(securityContext.getFullName());
} else {
user.setFullName(userId);
}
if (securityContext.getEmail() != null) {
user.setEmail(securityContext.getEmail());
} else {
// $NON-NLS-1$
user.setEmail("");
}
user.setJoinedOn(new Date());
if (securityContext.getLocale() != null) {
user.setLocale(securityContext.getLocale());
}
storage.createUser(user);
userBootstrapper.bootstrapUser(user, storage);
currentUser.setPermissions(new HashSet<>());
} else {
LOGGER.debug("Got existing user: {0}", user);
Set<PermissionBean> permissions = query.getPermissions(userId);
currentUser.setPermissions(permissions);
updateMutableFields(user);
}
currentUser.initFromUser(user);
currentUser.setAdmin(securityContext.isAdmin());
return currentUser;
});
}
use of io.apiman.manager.api.beans.idm.PermissionBean in project apiman by apiman.
the class IndexedPermissions method index.
/**
* Index the permissions.
* @param bean
*/
private void index(Set<PermissionBean> permissions) {
for (PermissionBean permissionBean : permissions) {
PermissionType permissionName = permissionBean.getName();
String orgQualifier = permissionBean.getOrganizationId();
String qualifiedPermission = createQualifiedPermissionKey(permissionName, orgQualifier);
organizations.add(orgQualifier);
qualifiedPermissions.add(qualifiedPermission);
Set<String> orgs = permissionToOrgsMap.computeIfAbsent(permissionName, k -> new HashSet<>());
orgs.add(orgQualifier);
}
}
use of io.apiman.manager.api.beans.idm.PermissionBean in project apiman by apiman.
the class JpaStorage method getPermissions.
/**
* {@inheritDoc}
*/
@Override
public Set<PermissionBean> getPermissions(String userId) throws StorageException {
try {
List<RoleMembershipBean> resultList = getCriteriaBuilderFactory().create(getActiveEntityManager(), RoleMembershipBean.class).where("userId").eq(userId).setMaxResults(// I think this is set arbitrarily?
500).getResultList();
List<String> roleIds = resultList.stream().map(RoleMembershipBean::getRoleId).collect(Collectors.toList());
Map<String, RoleBean> roleMap = getRolesById(roleIds).stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
Set<PermissionBean> permissions = new HashSet<>(resultList.size());
for (RoleMembershipBean membership : resultList) {
String qualifier = membership.getOrganizationId();
for (PermissionType permission : roleMap.get(membership.getRoleId()).getPermissions()) {
PermissionBean p = new PermissionBean();
p.setName(permission);
p.setOrganizationId(qualifier);
permissions.add(p);
}
}
return permissions;
} catch (Throwable t) {
LOGGER.error(t.getMessage(), t);
throw new StorageException(t);
}
}
use of io.apiman.manager.api.beans.idm.PermissionBean in project apiman by apiman.
the class EsStorage method getPermissions.
/**
* @see io.apiman.manager.api.core.IStorageQuery#getPermissions(java.lang.String)
*/
@Override
public Set<PermissionBean> getPermissions(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<PermissionBean> rval = new HashSet<>(hits.size());
if (!hits.isEmpty()) {
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSourceAsMap();
// $NON-NLS-1$
String roleId = String.valueOf(source.get("roleId"));
// $NON-NLS-1$
String qualifier = String.valueOf(source.get("organizationId"));
RoleBean role = getRole(roleId);
if (role != null) {
for (PermissionType permission : role.getPermissions()) {
PermissionBean p = new PermissionBean();
p.setName(permission);
p.setOrganizationId(qualifier);
rval.add(p);
}
}
}
}
return rval;
} catch (Exception e) {
throw new StorageException(e);
}
}
Aggregations