use of org.openforis.collect.model.UserInGroup in project collect by openforis.
the class DataService method findAssignableCodeListItems.
/**
* Gets the code list items assignable to the specified attribute.
*
* @param parentEntityId
* @param attrName
* @return
*/
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attrName) {
CollectRecord record = getActiveRecord();
CollectSurvey survey = (CollectSurvey) record.getSurvey();
UserGroup surveyUserGroup = survey.getUserGroup();
User user = sessionManager.getLoggedUser();
final UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyUserGroup, user);
if (userInGroup == null) {
throw new IllegalStateException(String.format("User %s not allowed to access survey %s", user.getUsername(), survey.getName()));
}
Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attrName);
List<CodeListItem> items = codeListManager.loadValidItems(parent, def);
List<CodeListItem> filteredItems = new ArrayList<CodeListItem>(items);
// filter by user group qualifier (if any)
UserGroup group = userGroupManager.loadById(userInGroup.getGroupId());
String qualifierName = group.getQualifier1Name();
String listHierarchicalLevelName = def.getList().isHierarchical() ? def.getHierarchicalLevel() : def.getListName();
if (qualifierName != null && qualifierName.equals(listHierarchicalLevelName)) {
CollectionUtils.filter(filteredItems, new Predicate<CodeListItem>() {
public boolean evaluate(CodeListItem item) {
return item.getCode().equals(group.getQualifier1Value());
}
});
}
List<CodeListItemProxy> result = CodeListItemProxy.fromList(filteredItems);
List<Node<?>> selectedCodes = parent.getChildren(attrName);
CodeListItemProxy.setSelectedItems(result, selectedCodes);
return result;
}
use of org.openforis.collect.model.UserInGroup in project collect by openforis.
the class DataRestoreTask method validateInput.
@Override
protected void validateInput() throws Throwable {
super.validateInput();
UserGroup surveyGroup = targetSurvey.getUserGroup();
String surveyName = targetSurvey.getName();
if (surveyGroup == null) {
throw new IllegalStateException(String.format("No user group for survey %s found", surveyName));
}
UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyGroup, user);
if (userInGroup == null || !DATA_RESTORE_ALLOWED_USER_GROUP_ROLES.contains(userInGroup.getRole())) {
throw new IllegalStateException(String.format("User %s is not allowed to restore data for survey %s", user.getUsername(), surveyName));
}
}
use of org.openforis.collect.model.UserInGroup in project collect by openforis.
the class ClientUserGroupManager method findUsersInGroup.
@SuppressWarnings("rawtypes")
@Override
public List<UserInGroup> findUsersInGroup(UserGroup userGroup) {
List<Map> userGroupRelations = getList(getUsersRestfulApiUrl() + "/group/" + userGroup.getId() + "/users", Map.class);
List<UserInGroup> result = new ArrayList<UserInGroup>();
for (Map userGroupRelation : userGroupRelations) {
UserInGroup userInGroup = new UserInGroup();
userInGroup.setGroupId(userGroup.getId());
userInGroup.setUserId(((Double) userGroupRelation.get("userId")).intValue());
userInGroup.setJoinStatus(UserGroupJoinRequestStatus.fromCode((String) userGroupRelation.get("statusCode")));
userInGroup.setRole(UserGroupRole.fromCode((String) userGroupRelation.get("roleCode")));
result.add(userInGroup);
}
return result;
}
use of org.openforis.collect.model.UserInGroup in project collect by openforis.
the class LocalUserGroupManager method createDefaultPrivateUserGroup.
@Override
@Transactional(readOnly = false, propagation = REQUIRED)
public UserGroup createDefaultPrivateUserGroup(User user, User createdByUser) {
UserGroup userGroup = new UserGroup();
userGroup.setName(getDefaultPrivateUserGroupName(user));
userGroup.setLabel(user.getUsername() + DEFAULT_PRIVATE_USER_GROUP_LABEL_SUFFIX);
userGroup.setVisibility(Visibility.PRIVATE);
userGroup.setSystemDefined(true);
userGroup.setEnabled(true);
userGroup.setCreationDate(new Timestamp(System.currentTimeMillis()));
userGroup.setCreatedBy(createdByUser.getId());
dao.insert(userGroup);
UserInGroup userInGroup = new UserInGroup();
userInGroup.setGroupId(userGroup.getId());
userInGroup.setUserId(user.getId());
userInGroup.setRole(OWNER);
userInGroup.setJoinStatus(ACCEPTED);
userInGroup.setRequestDate(new Date());
userInGroup.setMemberSince(new Date());
dao.insertRelation(userInGroup);
return userGroup;
}
use of org.openforis.collect.model.UserInGroup in project collect by openforis.
the class LocalUserGroupManager method joinToDefaultPublicGroup.
@Override
@Transactional(readOnly = false, propagation = REQUIRED)
public void joinToDefaultPublicGroup(User user, UserGroupRole role) {
UserGroup publicGroup = getDefaultPublicUserGroup();
UserInGroup userInGroup = new UserInGroup();
userInGroup.setGroupId(publicGroup.getId());
userInGroup.setUserId(user.getId());
userInGroup.setRole(role);
userInGroup.setJoinStatus(ACCEPTED);
Date now = new Date();
userInGroup.setRequestDate(now);
userInGroup.setMemberSince(now);
dao.insertRelation(userInGroup);
}
Aggregations