Search in sources :

Example 1 with UserGroupWithoutOwnerException

use of ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException in project irida by phac-nml.

the class GroupsController method updateUserRole.

/**
 * Update a user's role on a group
 *
 * @param groupId
 *            The ID of the group
 * @param userId
 *            The ID of the user
 * @param groupRole
 *            The role to set
 * @param locale
 *            Locale of the logged in user
 *
 * @return message indicating update result
 */
@RequestMapping(path = "/{groupId}/members/editrole/{userId}", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> updateUserRole(@PathVariable final Long groupId, @PathVariable final Long userId, @RequestParam final String groupRole, final Locale locale) {
    final UserGroup group = userGroupService.read(groupId);
    final User user = userService.read(userId);
    final UserGroupRole userGroupRole = UserGroupRole.fromString(groupRole);
    final String roleName = messageSource.getMessage("group.users.role." + groupRole, new Object[] {}, locale);
    try {
        userGroupService.changeUserGroupRole(user, group, userGroupRole);
        return ImmutableMap.of("success", messageSource.getMessage("group.members.edit.role.success", new Object[] { user.getLabel(), roleName }, locale));
    } catch (final UserGroupWithoutOwnerException e) {
        return ImmutableMap.of("failure", messageSource.getMessage("group.members.edit.role.failure", new Object[] { user.getLabel(), roleName }, locale));
    }
}
Also used : UserGroupRole(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole) User(ca.corefacility.bioinformatics.irida.model.user.User) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) DTUserGroup(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)

Example 2 with UserGroupWithoutOwnerException

use of ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException in project irida by phac-nml.

the class UserGroupServiceImpl method changeUserGroupRole.

/**
 * {@inheritDoc}
 */
@Override
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#userGroup, 'canUpdateUserGroup')")
public UserGroupJoin changeUserGroupRole(final User user, final UserGroup userGroup, final UserGroupRole role) throws UserGroupWithoutOwnerException {
    final UserGroupJoin join = userGroupJoinRepository.findOne(findUserGroupJoin(user, userGroup));
    if (!allowRoleChange(userGroup, join.getRole())) {
        throw new UserGroupWithoutOwnerException("Cannot change this user's group role because it would leave the group without an owner.");
    }
    join.setRole(role);
    return userGroupJoinRepository.save(join);
}
Also used : UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with UserGroupWithoutOwnerException

use of ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException in project irida by phac-nml.

the class UserGroupServiceImpl method removeUserFromGroup.

/**
 * {@inheritDoc}
 */
@Override
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#userGroup, 'canUpdateUserGroup')")
public void removeUserFromGroup(final User user, final UserGroup userGroup) throws UserGroupWithoutOwnerException {
    final UserGroupJoin join = userGroupJoinRepository.findOne(findUserGroupJoin(user, userGroup));
    if (!allowRoleChange(userGroup, join.getRole())) {
        throw new UserGroupWithoutOwnerException("Cannot remove this user from the group because it would leave the group without an owner.");
    }
    userGroupJoinRepository.delete(join);
}
Also used : UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with UserGroupWithoutOwnerException

use of ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException in project irida by phac-nml.

the class GroupsController method removeUserFromGroup.

/**
 * Remove a user from a group.
 *
 * @param userGroupId
 *            the group to remove from.
 * @param userId
 *            the user to remove.
 * @param locale
 *            the locale of the browser.
 * @return a message indicating success.
 */
@RequestMapping(path = "/{userGroupId}/members/{userId}", method = RequestMethod.DELETE)
@ResponseBody
public Map<String, String> removeUserFromGroup(@PathVariable final Long userGroupId, @PathVariable final Long userId, Locale locale) {
    final User user = userService.read(userId);
    final UserGroup group = userGroupService.read(userGroupId);
    try {
        userGroupService.removeUserFromGroup(user, group);
        return ImmutableMap.of("success", messageSource.getMessage("group.users.remove.notification.success", new Object[] { user.getLabel() }, locale));
    } catch (final UserGroupWithoutOwnerException e) {
        return ImmutableMap.of("failure", messageSource.getMessage("group.users.remove.notification.failure", new Object[] { user.getLabel() }, locale));
    }
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) DTUserGroup(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)

Aggregations

UserGroupWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException)4 User (ca.corefacility.bioinformatics.irida.model.user.User)2 UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)2 UserGroupJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin)2 DTUserGroup (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 UserGroupRole (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole)1