Search in sources :

Example 1 with UserInGroup

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;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) User(org.openforis.collect.model.User) UserInGroup(org.openforis.collect.model.UserInGroup) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) UserGroup(org.openforis.collect.model.UserGroup) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with UserInGroup

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));
    }
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) UserGroup(org.openforis.collect.model.UserGroup)

Example 3 with UserInGroup

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;
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UserInGroup

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;
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) Timestamp(java.sql.Timestamp) Date(java.util.Date) UserGroup(org.openforis.collect.model.UserGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserInGroup

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);
}
Also used : UserInGroup(org.openforis.collect.model.UserInGroup) Date(java.util.Date) UserGroup(org.openforis.collect.model.UserGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserInGroup (org.openforis.collect.model.UserInGroup)12 UserGroup (org.openforis.collect.model.UserGroup)7 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Transactional (org.springframework.transaction.annotation.Transactional)4 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 User (org.openforis.collect.model.User)3 HashMap (java.util.HashMap)2 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Condition (org.jooq.Condition)1 Record (org.jooq.Record)1 RecordHandler (org.jooq.RecordHandler)1 RecordAccessControlManager (org.openforis.collect.manager.RecordAccessControlManager)1 CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)1 CollectRecord (org.openforis.collect.model.CollectRecord)1 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)1 NodeChangeMap (org.openforis.collect.model.NodeChangeMap)1 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)1