Search in sources :

Example 1 with UserGroupMember

use of de.ids_mannheim.korap.entity.UserGroupMember in project Kustvakt by KorAP.

the class UserGroupService method retrieveUserGroupDto.

public List<UserGroupDto> retrieveUserGroupDto(String username) throws KustvaktException {
    List<UserGroup> userGroups = retrieveUserGroup(username);
    ArrayList<UserGroupDto> dtos = new ArrayList<>(userGroups.size());
    UserGroupMember userAsMember;
    List<UserGroupMember> members;
    UserGroupDto groupDto;
    for (UserGroup group : userGroups) {
        members = retrieveMembers(group.getId(), username);
        userAsMember = groupMemberDao.retrieveMemberById(username, group.getId());
        groupDto = converter.createUserGroupDto(group, members, userAsMember.getStatus(), userAsMember.getRoles());
        dtos.add(groupDto);
    }
    return dtos;
}
Also used : UserGroupDto(de.ids_mannheim.korap.dto.UserGroupDto) ArrayList(java.util.ArrayList) UserGroupMember(de.ids_mannheim.korap.entity.UserGroupMember) UserGroup(de.ids_mannheim.korap.entity.UserGroup)

Example 2 with UserGroupMember

use of de.ids_mannheim.korap.entity.UserGroupMember in project Kustvakt by KorAP.

the class UserGroupService method retrieveMembers.

private List<UserGroupMember> retrieveMembers(int groupId, String username) throws KustvaktException {
    List<UserGroupMember> groupAdmins = groupMemberDao.retrieveMemberByRole(groupId, PredefinedRole.USER_GROUP_ADMIN.getId());
    List<UserGroupMember> members = null;
    for (UserGroupMember admin : groupAdmins) {
        if (admin.getUserId().equals(username)) {
            members = groupMemberDao.retrieveMemberByGroupId(groupId);
            break;
        }
    }
    return members;
}
Also used : UserGroupMember(de.ids_mannheim.korap.entity.UserGroupMember)

Example 3 with UserGroupMember

use of de.ids_mannheim.korap.entity.UserGroupMember in project Kustvakt by KorAP.

the class UserGroupService method addMemberRoles.

public void addMemberRoles(String username, String groupName, String memberUsername, List<Integer> roleIds) throws KustvaktException {
    ParameterChecker.checkStringValue(username, "username");
    ParameterChecker.checkStringValue(groupName, "groupName");
    ParameterChecker.checkStringValue(memberUsername, "memberUsername");
    UserGroup userGroup = userGroupDao.retrieveGroupByName(groupName, true);
    UserGroupStatus groupStatus = userGroup.getStatus();
    if (groupStatus == UserGroupStatus.DELETED) {
        throw new KustvaktException(StatusCodes.GROUP_DELETED, "Usergroup has been deleted.");
    } else if (isUserGroupAdmin(username, userGroup) || adminDao.isAdmin(username)) {
        UserGroupMember member = groupMemberDao.retrieveMemberById(memberUsername, userGroup.getId());
        if (!member.getStatus().equals(GroupMemberStatus.ACTIVE)) {
            throw new KustvaktException(StatusCodes.GROUP_MEMBER_INACTIVE, memberUsername + " has status " + member.getStatus(), memberUsername, member.getStatus().name());
        }
        Set<Role> roles = member.getRoles();
        for (int i = 0; i < roleIds.size(); i++) {
            roles.add(roleDao.retrieveRoleById(roleIds.get(i)));
        }
        member.setRoles(roles);
        groupMemberDao.updateMember(member);
    } else {
        throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED, "Unauthorized operation for user: " + username, username);
    }
}
Also used : KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) HashSet(java.util.HashSet) Set(java.util.Set) UserGroupStatus(de.ids_mannheim.korap.constant.UserGroupStatus) UserGroupMember(de.ids_mannheim.korap.entity.UserGroupMember) UserGroup(de.ids_mannheim.korap.entity.UserGroup)

Example 4 with UserGroupMember

use of de.ids_mannheim.korap.entity.UserGroupMember in project Kustvakt by KorAP.

the class UserGroupService method acceptInvitation.

/**
 * Updates the {@link GroupMemberStatus} of a pending member
 * to {@link GroupMemberStatus#ACTIVE} and add default member
 * roles.
 *
 * @param groupId
 *            groupId
 * @param username
 *            the username of the group member
 * @throws KustvaktException
 */
public void acceptInvitation(String groupName, String username) throws KustvaktException {
    ParameterChecker.checkStringValue(username, "userId");
    ParameterChecker.checkStringValue(groupName, "groupId");
    UserGroup userGroup = userGroupDao.retrieveGroupByName(groupName, false);
    if (userGroup.getStatus() == UserGroupStatus.DELETED) {
        throw new KustvaktException(StatusCodes.GROUP_DELETED, "Group " + userGroup.getName() + " has been deleted.", userGroup.getName());
    }
    UserGroupMember member = groupMemberDao.retrieveMemberById(username, userGroup.getId());
    GroupMemberStatus status = member.getStatus();
    if (status.equals(GroupMemberStatus.DELETED)) {
        throw new KustvaktException(StatusCodes.GROUP_MEMBER_DELETED, username + " has already been deleted from the group " + userGroup.getName(), username, userGroup.getName());
    } else if (member.getStatus().equals(GroupMemberStatus.ACTIVE)) {
        throw new KustvaktException(StatusCodes.GROUP_MEMBER_EXISTS, "Username " + username + " with status " + status + " exists in the user-group " + userGroup.getName(), username, status.name(), userGroup.getName());
    } else // status pending
    {
        if (DEBUG) {
            jlog.debug("status: " + member.getStatusDate());
        }
        ZonedDateTime expiration = member.getStatusDate().plusMinutes(30);
        ZonedDateTime now = ZonedDateTime.now();
        if (DEBUG) {
            jlog.debug("expiration: " + expiration + ", now: " + now);
        }
        if (expiration.isAfter(now)) {
            member.setStatus(GroupMemberStatus.ACTIVE);
            setMemberRoles();
            member.setRoles(memberRoles);
            groupMemberDao.updateMember(member);
        } else {
            throw new KustvaktException(StatusCodes.INVITATION_EXPIRED);
        }
    }
}
Also used : KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) ZonedDateTime(java.time.ZonedDateTime) UserGroupMember(de.ids_mannheim.korap.entity.UserGroupMember) GroupMemberStatus(de.ids_mannheim.korap.constant.GroupMemberStatus) UserGroup(de.ids_mannheim.korap.entity.UserGroup)

Example 5 with UserGroupMember

use of de.ids_mannheim.korap.entity.UserGroupMember in project Kustvakt by KorAP.

the class UserGroupService method doDeleteMember.

/**
 * Updates the {@link GroupMemberStatus} of a member to
 * {@link GroupMemberStatus#DELETED}
 *
 * @param userId
 *            user to be deleted
 * @param groupId
 *            user-group id
 * @param deletedBy
 *            user that issue the delete
 * @param isSoftDelete
 *            true if database entry is to be deleted
 *            permanently, false otherwise
 * @throws KustvaktException
 */
private void doDeleteMember(String username, int groupId, String deletedBy, boolean isSoftDelete) throws KustvaktException {
    UserGroup group = userGroupDao.retrieveGroupById(groupId);
    UserGroupMember member = groupMemberDao.retrieveMemberById(username, groupId);
    GroupMemberStatus status = member.getStatus();
    if (isSoftDelete && status.equals(GroupMemberStatus.DELETED)) {
        throw new KustvaktException(StatusCodes.GROUP_MEMBER_DELETED, username + " has already been deleted from the group " + group.getName(), username, group.getName());
    }
    groupMemberDao.deleteMember(member, deletedBy, isSoftDelete);
}
Also used : KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) UserGroupMember(de.ids_mannheim.korap.entity.UserGroupMember) GroupMemberStatus(de.ids_mannheim.korap.constant.GroupMemberStatus) UserGroup(de.ids_mannheim.korap.entity.UserGroup)

Aggregations

UserGroupMember (de.ids_mannheim.korap.entity.UserGroupMember)26 KustvaktException (de.ids_mannheim.korap.exceptions.KustvaktException)14 UserGroup (de.ids_mannheim.korap.entity.UserGroup)12 Role (de.ids_mannheim.korap.entity.Role)11 PredefinedRole (de.ids_mannheim.korap.constant.PredefinedRole)9 Query (javax.persistence.Query)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)7 Predicate (javax.persistence.criteria.Predicate)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 NoResultException (javax.persistence.NoResultException)5 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)4 GroupMemberStatus (de.ids_mannheim.korap.constant.GroupMemberStatus)4 HashSet (java.util.HashSet)4 SpringJerseyTest (de.ids_mannheim.korap.config.SpringJerseyTest)3 UserGroupDto (de.ids_mannheim.korap.dto.UserGroupDto)3 UserGroupStatus (de.ids_mannheim.korap.constant.UserGroupStatus)2 QueryDO (de.ids_mannheim.korap.entity.QueryDO)2