use of org.hisp.dhis.user.UserSettingKey in project dhis2-core by dhis2.
the class UserController method replicateUser.
@SuppressWarnings("unchecked")
@PreAuthorize("hasRole('ALL') or hasRole('F_REPLICATE_USER')")
@RequestMapping(value = "/{uid}/replica", method = RequestMethod.POST)
public void replicateUser(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
User existingUser = userService.getUser(uid);
if (existingUser == null || existingUser.getUserCredentials() == null) {
throw new WebMessageException(WebMessageUtils.conflict("User not found: " + uid));
}
User currentUser = currentUserService.getCurrentUser();
if (!validateCreateUser(existingUser, currentUser)) {
return;
}
Map<String, String> auth = renderService.fromJson(request.getInputStream(), Map.class);
String username = StringUtils.trimToNull(auth != null ? auth.get(KEY_USERNAME) : null);
String password = StringUtils.trimToNull(auth != null ? auth.get(KEY_PASSWORD) : null);
if (auth == null || username == null) {
throw new WebMessageException(WebMessageUtils.conflict("Username must be specified"));
}
if (userService.getUserCredentialsByUsername(username) != null) {
throw new WebMessageException(WebMessageUtils.conflict("Username already taken: " + username));
}
if (password == null) {
throw new WebMessageException(WebMessageUtils.conflict("Password must be specified"));
}
if (!ValidationUtils.passwordIsValid(password)) {
throw new WebMessageException(WebMessageUtils.conflict("Password must have at least 8 characters, one digit, one uppercase"));
}
User userReplica = new User();
mergeService.merge(new MergeParams<>(existingUser, userReplica).setMergeMode(MergeMode.MERGE));
userReplica.setUid(CodeGenerator.generateUid());
userReplica.setCode(null);
userReplica.setCreated(new Date());
UserCredentials credentialsReplica = new UserCredentials();
mergeService.merge(new MergeParams<>(existingUser.getUserCredentials(), credentialsReplica).setMergeMode(MergeMode.MERGE));
credentialsReplica.setUid(CodeGenerator.generateUid());
credentialsReplica.setCode(null);
credentialsReplica.setCreated(new Date());
credentialsReplica.setLdapId(null);
credentialsReplica.setOpenId(null);
credentialsReplica.setUsername(username);
userService.encodeAndSetPassword(credentialsReplica, password);
userReplica.setUserCredentials(credentialsReplica);
credentialsReplica.setUserInfo(userReplica);
userService.addUser(userReplica);
userService.addUserCredentials(credentialsReplica);
userGroupService.addUserToGroups(userReplica, IdentifiableObjectUtils.getUids(existingUser.getGroups()), currentUser);
// ---------------------------------------------------------------------
// Replicate user settings
// ---------------------------------------------------------------------
List<UserSetting> settings = userSettingService.getUserSettings(existingUser);
for (UserSetting setting : settings) {
Optional<UserSettingKey> key = UserSettingKey.getByName(setting.getName());
key.ifPresent(userSettingKey -> userSettingService.saveUserSetting(userSettingKey, setting.getValue(), userReplica));
}
response.addHeader("Location", UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid());
webMessageService.send(WebMessageUtils.created("User replica created"), response, request);
}
use of org.hisp.dhis.user.UserSettingKey in project dhis2-core by dhis2.
the class UserSettingController method getUserSetting.
@RequestMapping(value = "/{key}", method = RequestMethod.GET)
@ResponseBody
public String getUserSetting(@PathVariable("key") String key, @RequestParam(value = "user", required = false) String username, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
if (!keyEnum.isPresent()) {
throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
}
User user = null;
if (username != null) {
UserCredentials credentials = userService.getUserCredentialsByUsername(username);
if (credentials != null) {
user = credentials.getUserInfo();
} else {
throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + username));
}
}
Serializable value = userSettingService.getUserSetting(keyEnum.get(), user);
if (value == null) {
throw new WebMessageException(WebMessageUtils.notFound("User setting not found for key: " + key));
}
return String.valueOf(value);
}
use of org.hisp.dhis.user.UserSettingKey in project dhis2-core by dhis2.
the class UserSettingController method setUserSetting.
// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(value = "/{key}", method = RequestMethod.POST)
public void setUserSetting(@PathVariable(value = "key") String key, @RequestParam(value = "user", required = false) String username, @RequestParam(value = "value", required = false) String value, @RequestBody(required = false) String valuePayload, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
if (key == null) {
throw new WebMessageException(WebMessageUtils.conflict("Key must be specified"));
}
if (value == null && valuePayload == null) {
throw new WebMessageException(WebMessageUtils.conflict("Value must be specified as query param or as payload"));
}
value = ObjectUtils.firstNonNull(value, valuePayload);
Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
if (!keyEnum.isPresent()) {
throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
}
Serializable valueObject = UserSettingKey.getAsRealClass(key, value);
if (username == null) {
userSettingService.saveUserSetting(keyEnum.get(), valueObject);
} else {
userSettingService.saveUserSetting(keyEnum.get(), valueObject, username);
}
webMessageService.send(WebMessageUtils.ok("User setting saved"), response, request);
}
Aggregations