use of org.hisp.dhis.user.UserSetting in project dhis2-core by dhis2.
the class DeleteCurrentUserAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
message = "";
User user = currentUserService.getCurrentUser();
UserCredentials userCredentials = user.getUserCredentials();
username = userCredentials.getUsername();
String oldPasswordFromDB = userCredentials.getPassword();
if (oldPassword == null) {
return INPUT;
}
oldPassword = oldPassword.trim();
if (oldPassword.length() == 0) {
return INPUT;
}
if (!passwordManager.matches(oldPassword, oldPasswordFromDB)) {
message = i18n.getString("wrong_password");
return INPUT;
} else {
Collection<UserSetting> userSettings = userSettingService.getAllUserSettings();
for (UserSetting userSetting : userSettings) {
userSettingService.deleteUserSetting(userSetting);
}
if (userService.isLastSuperUser(userCredentials)) {
message = i18n.getString("can_not_remove_last_super_user");
return INPUT;
} else {
userService.deleteUser(user);
}
return "logout";
}
}
use of org.hisp.dhis.user.UserSetting in project dhis2-core by dhis2.
the class UserController method replicateUser.
@SuppressWarnings("unchecked")
@PreAuthorize("hasRole('ALL') or hasRole('F_REPLICATE_USER')")
@PostMapping("/{uid}/replica")
@ResponseBody
public WebMessage replicateUser(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
User existingUser = userService.getUser(uid);
if (existingUser == null) {
return conflict("User not found: " + uid);
}
User currentUser = currentUserService.getCurrentUser();
validateCreateUser(existingUser, currentUser);
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) {
return conflict("Username must be specified");
}
if (userService.getUserByUsername(username) != null) {
return conflict("Username already taken: " + username);
}
if (password == null) {
return conflict("Password must be specified");
}
if (!ValidationUtils.passwordIsValid(password)) {
return 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));
copyAttributeValues(userReplica);
userReplica.setId(0);
userReplica.setUuid(UUID.randomUUID());
userReplica.setUid(CodeGenerator.generateUid());
userReplica.setCode(null);
userReplica.setCreated(new Date());
userReplica.setLdapId(null);
userReplica.setOpenId(null);
userReplica.setUsername(username);
userService.encodeAndSetPassword(userReplica, password);
userService.addUser(userReplica);
userGroupService.addUserToGroups(userReplica, 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));
}
return created("User replica created").setLocation(UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid());
}
use of org.hisp.dhis.user.UserSetting in project dhis2-core by dhis2.
the class HibernateUserSettingStore method getAllUserSettings.
@Override
@SuppressWarnings("unchecked")
public List<UserSetting> getAllUserSettings(User user) {
Session session = sessionFactory.getCurrentSession();
Query<UserSetting> query = session.createQuery("from UserSetting us where us.user = :user");
query.setParameter("user", user);
query.setCacheable(CACHEABLE);
return query.list();
}
use of org.hisp.dhis.user.UserSetting in project dhis2-core by dhis2.
the class HibernateUserSettingStore method getUserSetting.
@Override
@SuppressWarnings("unchecked")
public UserSetting getUserSetting(User user, String name) {
Session session = sessionFactory.getCurrentSession();
Query<UserSetting> query = session.createQuery("from UserSetting us where us.user = :user and us.name = :name");
query.setParameter("user", user);
query.setParameter("name", name);
query.setCacheable(CACHEABLE);
return query.uniqueResult();
}
Aggregations