use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class UsersController method updateUser.
/**
* Submit a user edit
*
* @param userId
* The id of the user to edit (required)
* @param firstName
* The firstname to update
* @param lastName
* the lastname to update
* @param email
* the email to update
* @param phoneNumber
* the phone number to update
* @param systemRole
* the role to update
* @param password
* the password to update
* @param confirmPassword
* password confirmation
* @param model
* The model to work on
* @param enabled
* whether the user account should be enabled or disabled.
* @param principal
* a reference to the logged in user.
* @param request
* the request
* @return The name of the user view
*/
@RequestMapping(value = "/{userId}/edit", method = RequestMethod.POST)
public String updateUser(@PathVariable Long userId, @RequestParam(required = false) String firstName, @RequestParam(required = false) String lastName, @RequestParam(required = false) String email, @RequestParam(required = false) String phoneNumber, @RequestParam(required = false) String systemRole, @RequestParam(required = false) String password, @RequestParam(required = false) String enabled, @RequestParam(required = false) String confirmPassword, Model model, Principal principal, HttpServletRequest request) {
logger.debug("Updating user " + userId);
Locale locale = LocaleContextHolder.getLocale();
Map<String, String> errors = new HashMap<>();
Map<String, Object> updatedValues = new HashMap<>();
if (!Strings.isNullOrEmpty(firstName)) {
updatedValues.put("firstName", firstName);
}
if (!Strings.isNullOrEmpty(lastName)) {
updatedValues.put("lastName", lastName);
}
if (!Strings.isNullOrEmpty(email)) {
updatedValues.put("email", email);
}
if (!Strings.isNullOrEmpty(phoneNumber)) {
updatedValues.put("phoneNumber", phoneNumber);
}
if (!Strings.isNullOrEmpty(password) || !Strings.isNullOrEmpty(confirmPassword)) {
if (!password.equals(confirmPassword)) {
errors.put("password", messageSource.getMessage("user.edit.password.match", null, locale));
} else {
updatedValues.put("password", password);
}
}
if (isAdmin(principal)) {
logger.debug("User is admin");
if (!Strings.isNullOrEmpty(enabled)) {
updatedValues.put("enabled", true);
} else {
updatedValues.put("enabled", false);
}
if (!Strings.isNullOrEmpty(systemRole)) {
Role newRole = Role.valueOf(systemRole);
updatedValues.put("systemRole", newRole);
}
}
String returnView;
if (errors.isEmpty()) {
try {
User user = userService.updateFields(userId, updatedValues);
returnView = "redirect:/users/" + userId;
// this will update the users gravatar!
if (user != null && principal.getName().equals(user.getUsername())) {
HttpSession session = request.getSession();
session.setAttribute(UserSecurityInterceptor.CURRENT_USER_DETAILS, user);
}
} catch (ConstraintViolationException | DataIntegrityViolationException | PasswordReusedException ex) {
errors = handleCreateUpdateException(ex, locale);
model.addAttribute("errors", errors);
returnView = getEditUserPage(userId, model);
}
} else {
model.addAttribute("errors", errors);
returnView = getEditUserPage(userId, model);
}
return returnView;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class UsersController method getAjaxUserList.
/**
* Get a list of users based on search criteria.
* @param params {@link DataTablesParams} for the current Users DataTables.
* @param locale {@link Locale}
* @return {@link DataTablesResponse} of the filtered users list.
*/
@RequestMapping(value = "/ajax/list", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DataTablesResponse getAjaxUserList(@DataTablesRequest DataTablesParams params, Locale locale) {
Page<User> userPage = userService.search(UserSpecification.searchUser(params.getSearchValue()), new PageRequest(params.getCurrentPage(), params.getLength(), params.getSort()));
List<DataTablesResponseModel> usersData = new ArrayList<>();
for (User user : userPage) {
// getting internationalized system role from the message source
String roleMessageName = "systemrole." + user.getSystemRole().getName();
String systemRole = messageSource.getMessage(roleMessageName, null, locale);
usersData.add(new DTUser(user.getId(), user.getUsername(), user.getFirstName(), user.getLastName(), user.getEmail(), systemRole, user.getCreatedDate(), user.getModifiedDate(), user.getLastLogin()));
}
return new DataTablesResponse(params, userPage, usersData);
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class ProjectEventsController method getRecentEventsForUser.
/**
* Get recent {@link ProjectEvent}s for the currently logged in user
*
* @param model
* Model for the view. Contains a list named "events". This will
* be a map which will contain "name" which is the name of the
* view fragment to use, and "event" which is a reference to the
* event itself
* @param principal
* currently logged in principal
* @param size
* Number of events to show
* @return The name of the events view
*/
@RequestMapping("/current_user")
public String getRecentEventsForUser(Model model, Principal principal, @RequestParam(required = false, defaultValue = DEFAULT_PAGE_SIZE) Integer size) {
String userName = principal.getName();
User user = userService.getUserByUsername(userName);
Page<ProjectEvent> events = eventService.getEventsForUser(user, new PageRequest(0, size, Direction.DESC, "createdDate"));
List<Map<String, Object>> eventInfo = buildEventsListFromPage(events);
model.addAttribute("events", eventInfo);
return EVENTS_VIEW;
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class ProjectEventsController method addSubscription.
/**
* Update the subscription status on a {@link Project} for a {@link User}
*
* @param userId
* The {@link User} id to update
* @param projectId
* the {@link Project} to subscribe to
* @param subscribe
* boolean whether to be subscribed to the project or not
* @param locale
* locale of the request
* @return Map success message if the subscription status was updated
*/
@RequestMapping(value = "/projects/{projectId}/subscribe/{userId}", method = RequestMethod.POST)
public Map<String, String> addSubscription(@PathVariable Long userId, @PathVariable Long projectId, @RequestParam boolean subscribe, Locale locale) {
User user = userService.read(userId);
Project project = projectService.read(projectId);
userService.updateEmailSubscription(user, project, subscribe);
String message;
if (subscribe) {
message = messageSource.getMessage("user.projects.subscriptions.added", new Object[] { project.getLabel() }, locale);
} else {
message = messageSource.getMessage("user.projects.subscriptions.removed", new Object[] { project.getLabel() }, locale);
}
return ImmutableMap.of("success", "true", "message", message);
}
use of ca.corefacility.bioinformatics.irida.model.user.User in project irida by phac-nml.
the class ProjectMembersController method updateUserRole.
/**
* Update a user's role on a project
*
* @param projectId The ID of the project
* @param userId The ID of the user
* @param projectRole The role to set
* @param locale Locale of the logged in user
* @return Success or error message
*/
@RequestMapping(path = "{projectId}/settings/members/editrole/{userId}", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> updateUserRole(@PathVariable final Long projectId, @PathVariable final Long userId, @RequestParam final String projectRole, final Locale locale) {
final Project project = projectService.read(projectId);
final User user = userService.read(userId);
final ProjectRole role = ProjectRole.fromString(projectRole);
final String roleName = messageSource.getMessage("projectRole." + projectRole, new Object[] {}, locale);
try {
projectService.updateUserProjectRole(project, user, role);
return ImmutableMap.of("success", messageSource.getMessage("project.members.edit.role.success", new Object[] { user.getLabel(), roleName }, locale));
} catch (final ProjectWithoutOwnerException e) {
return ImmutableMap.of("failure", messageSource.getMessage("project.members.edit.role.failure.nomanager", new Object[] { user.getLabel(), roleName }, locale));
}
}
Aggregations