protected void verifyGroupMemberExists(String groupId, String userId) { // Check existence Group group = identityService.createGroupQuery().groupId(groupId).singleResult(); User user = identityService.createUserQuery().userId(userId).singleResult(); for (User groupMember : identityService.createUserQuery().memberOfGroup(groupId).list()) { if (groupMember.getId().equals(userId)) { user = groupMember; } } if (group == null || user == null) { throw new NotFoundException(); } }
@ApiOperation(value = "Delete a member from a group", tags = { "Groups" }) @ApiResponses(value = { @ApiResponse(code = 204, message = "Indicates the group was found and the member has been deleted. The response body is left empty intentionally."), @ApiResponse(code = 404, message = "Indicates the requested group was not found or that the user is not a member of the group. The status description contains additional information about the error.") }) @DeleteMapping("/groups/{groupId}/members/{userId}") public void deleteMembership(@ApiParam(name = "groupId") @PathVariable("groupId") String groupId, @ApiParam(name = "userId") @PathVariable("userId") String userId, HttpServletRequest request, HttpServletResponse response) { Group group = getGroupFromRequest(groupId); // Check if user is not a member of group since API doesn't return typed exception if (identityService.createUserQuery().memberOfGroup(group.getId()).userId(userId).count() != 1) { throw new FlowableObjectNotFoundException("User '" + userId + "' is not part of group '" + group.getId() + "'.", null); } identityService.deleteMembership(userId, group.getId()); response.setStatus(HttpStatus.NO_CONTENT.value()); } }
protected UserQuery createUserQuery(String filter, String sort) { UserQuery userQuery = identityService.createUserQuery(); if (StringUtils.isNotEmpty(filter)) { userQuery.userFullNameLikeIgnoreCase("%" + filter + "%"); } if (StringUtils.isNotEmpty(sort)) { if ("idDesc".equals(sort)) { userQuery.orderByUserId().desc(); } else if ("idAsc".equals(sort)) { userQuery.orderByUserId().asc(); } else if ("emailAsc".equals(sort)) { userQuery.orderByUserEmail().asc(); } else if ("emailDesc".equals(sort)) { userQuery.orderByUserEmail().desc(); } } return userQuery; }
protected UserQuery createUsersForGroupQuery(String groupId, String filter) { UserQuery userQuery = identityService.createUserQuery().memberOfGroup(groupId); if (StringUtils.isNotEmpty(filter)) { userQuery.userFullNameLikeIgnoreCase("%" + filter + "%"); } return userQuery; }
protected User getUserFromRequest(String userId) { User user = identityService.createUserQuery().userId(userId).singleResult(); if (user == null) { throw new FlowableObjectNotFoundException("Could not find a user with id '" + userId + "'.", User.class); } if (restApiInterceptor != null) { restApiInterceptor.accessUserInfoById(user); } return user; }
query.userId(allRequestParams.get("id")); query.userFirstName(allRequestParams.get("firstName")); query.userLastName(allRequestParams.get("lastName")); query.userDisplayName(allRequestParams.get("displayName")); query.userEmail(allRequestParams.get("email")); query.userFirstNameLike(allRequestParams.get("firstNameLike")); query.userLastNameLike(allRequestParams.get("lastNameLike")); query.userDisplayNameLike(allRequestParams.get("displayNameLike")); query.userEmailLike(allRequestParams.get("emailLike")); query.memberOfGroup(allRequestParams.get("memberOfGroup"));
public CachedUser load(final String userId) throws Exception { User userFromDatabase = null; if (ldapProperties == null || !ldapProperties.isEnabled()) { userFromDatabase = identityService.createUserQuery().userIdIgnoreCase(userId.toLowerCase()).singleResult(); } else { userFromDatabase = identityService.createUserQuery().userId(userId).singleResult(); } if (userFromDatabase == null) { throw new UsernameNotFoundException("User " + userId + " was not found in the database"); } Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>(); UserInformation userInformation = userService.getUserInformation(userFromDatabase.getId()); for (String privilege : userInformation.getPrivileges()) { grantedAuthorities.add(new SimpleGrantedAuthority(privilege)); } return new CachedUser(userFromDatabase, grantedAuthorities); }
public User createNewUser(String id, String firstName, String lastName, String email, String password) { if (StringUtils.isBlank(id) || StringUtils.isBlank(password) || StringUtils.isBlank(firstName)) { throw new BadRequestException("Id, password and first name are required"); } if (email != null && identityService.createUserQuery().userEmail(email).count() > 0) { throw new ConflictingRequestException("User already registered", "ACCOUNT.SIGNUP.ERROR.ALREADY-REGISTERED"); } User user = identityService.newUser(id); user.setFirstName(firstName); user.setLastName(lastName); user.setEmail(email); identityService.saveUser(user); User savedUser = identityService.createUserQuery().userEmail(email).singleResult(); savedUser.setPassword(password); identityService.updateUserPassword(savedUser); return user; }
protected void clearAllUsersAndGroups() { // Privileges List<Privilege> privileges = idmIdentityService.createPrivilegeQuery().list(); for (Privilege privilege : privileges) { idmIdentityService.deletePrivilege(privilege.getId()); } // Groups List<Group> groups = idmIdentityService.createGroupQuery().list(); for (Group group : groups) { List<User> members = idmIdentityService.createUserQuery().memberOfGroup(group.getId()).list(); for (User member : members) { idmIdentityService.deleteMembership(member.getId(), group.getId()); } idmIdentityService.deleteGroup(group.getId()); } // Users List<User> users = idmIdentityService.createUserQuery().list(); for (User user : users) { idmIdentityService.deleteUser(user.getId()); } }
@ApiOperation(value = "Create a user", tags = { "Users" }) @ApiResponses(value = { @ApiResponse(code = 201, message = "Indicates the user was created."), @ApiResponse(code = 400, message = "Indicates the id of the user was missing.") }) @PostMapping(value = "/users", produces = "application/json") public UserResponse createUser(@RequestBody UserRequest userRequest, HttpServletRequest request, HttpServletResponse response) { if (userRequest.getId() == null) { throw new FlowableIllegalArgumentException("Id cannot be null."); } // Check if a user with the given ID already exists so we return a CONFLICT if (identityService.createUserQuery().userId(userRequest.getId()).count() > 0) { throw new FlowableConflictException("A user with id '" + userRequest.getId() + "' already exists."); } User created = identityService.newUser(userRequest.getId()); created.setEmail(userRequest.getEmail()); created.setFirstName(userRequest.getFirstName()); created.setLastName(userRequest.getLastName()); created.setDisplayName(userRequest.getDisplayName()); created.setPassword(userRequest.getPassword()); if (restApiInterceptor != null) { restApiInterceptor.createNewUser(created); } identityService.saveUser(created); response.setStatus(HttpStatus.CREATED.value()); return idmRestResponseFactory.createUserResponse(created, false); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List<User> execute(CommandContext commandContext) { ProcessDefinitionEntity processDefinition = CommandContextUtil.getProcessDefinitionEntityManager(commandContext).findById(processDefinitionId); if (processDefinition == null) { throw new FlowableObjectNotFoundException("Cannot find process definition with id " + processDefinitionId, ProcessDefinition.class); } IdentityService identityService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getIdentityService(); List<String> userIds = new ArrayList<>(); List<IdentityLink> identityLinks = (List) processDefinition.getIdentityLinks(); for (IdentityLink identityLink : identityLinks) { if (identityLink.getUserId() != null && identityLink.getUserId().length() > 0) { if (!userIds.contains(identityLink.getUserId())) { userIds.add(identityLink.getUserId()); } } } if (userIds.size() > 0) { return identityService.createUserQuery().userIds(userIds).list(); } else { return new ArrayList<>(); } }
public long countTotalGroupUsers(String groupId, String filter, Integer page, Integer pageSize) { return createUsersForGroupQuery(groupId, filter).count(); }
public List<User> getGroupUsers(String groupId, String filter, Integer page, Integer pageSize) { int pageValue = page != null ? page.intValue() : 0; int pageSizeValue = pageSize != null ? pageSize.intValue() : 50; UserQuery userQuery = createUsersForGroupQuery(groupId, filter); return userQuery.listPage(pageValue, pageSizeValue); }
public void bulkUpdatePassword(List<String> userIds, String newPassword) { for (String userId : userIds) { User user = identityService.createUserQuery().userId(userId).singleResult(); if (user != null) { user.setPassword(newPassword); identityService.updateUserPassword(user); } } }
public long getUserCount(String filter, String sort, Integer start, String groupId) { return createUserQuery(filter, sort).count(); }
public List<User> getUsers(String filter, String sort, Integer start) { Integer startValue = start != null ? start.intValue() : 0; Integer size = MAX_USER_SIZE; // TODO: pass actual size return createUserQuery(filter, sort).listPage(startValue, (size != null && size > 0) ? size : MAX_USER_SIZE); }
protected boolean isUserPrivilege(String privilegeId, String userId) { User user = identityService.createUserQuery().userId(userId).singleResult(); if (user == null) { throw new IllegalArgumentException("Invalid user id"); } return identityService.createPrivilegeQuery().privilegeId(privilegeId).userId(userId).count() > 0; }
@ApiOperation(value = "Add a member to a group", tags = { "Groups" }) @ApiResponses(value = { @ApiResponse(code = 201, message = "Indicates the group was found and the member has been added."), @ApiResponse(code = 400, message = "Indicates the userId was not included in the request body."), @ApiResponse(code = 404, message = "Indicates the requested group was not found."), @ApiResponse(code = 409, message = "Indicates the requested user is already a member of the group.") }) @PostMapping(value = "/groups/{groupId}/members", produces = "application/json") public MembershipResponse createMembership(@ApiParam(name = "groupId") @PathVariable String groupId, @RequestBody MembershipRequest memberShip, HttpServletRequest request, HttpServletResponse response) { Group group = getGroupFromRequest(groupId); if (memberShip.getUserId() == null) { throw new FlowableIllegalArgumentException("UserId cannot be null."); } // Check if user is member of group since API doesn't return typed exception if (identityService.createUserQuery().memberOfGroup(group.getId()).userId(memberShip.getUserId()).count() > 0) { throw new FlowableConflictException("User '" + memberShip.getUserId() + "' is already part of group '" + group.getId() + "'."); } identityService.createMembership(memberShip.getUserId(), group.getId()); response.setStatus(HttpStatus.CREATED.value()); return restResponseFactory.createMembershipResponse(memberShip.getUserId(), group.getId()); } }
public void changePassword(String originalPassword, String newPassword) { User user = identityService.createUserQuery().userId(SecurityUtils.getCurrentUserId()).singleResult(); if (!user.getPassword().equals(originalPassword)) { throw new NotFoundException(); } user.setPassword(newPassword); identityService.updateUserPassword(user); }
public void updateUserDetails(String userId, String firstName, String lastName, String email) { User user = identityService.createUserQuery().userId(userId).singleResult(); if (user != null) { user.setFirstName(firstName); user.setLastName(lastName); user.setEmail(email); identityService.saveUser(user); } }