use of org.hisp.dhis.user.UserCredentials 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.UserCredentials in project dhis2-core by dhis2.
the class ValidateUserAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
if (username != null) {
UserCredentials match = userService.getUserCredentialsByUsername(username);
if (match != null && (id == null || match.getId() != id)) {
message = i18n.getString("username_in_use");
return ERROR;
}
}
if (openId != null) {
UserCredentials match = userService.getUserCredentialsByOpenId(openId);
if (match != null && (id == null || match.getId() != id)) {
message = i18n.getString("openid_in_use");
return ERROR;
}
}
if (ldapId != null) {
UserCredentials match = userService.getUserCredentialsByLdapId(ldapId);
if (match != null && (id == null || match.getId() != id)) {
message = i18n.getString("ldap_in_use");
return ERROR;
}
}
if (inviteUsername != null) {
UserCredentials match = userService.getUserCredentialsByUsername(inviteUsername);
if (match != null && (id == null || match.getId() != id)) {
message = i18n.getString("username_in_use");
return ERROR;
}
}
if (rawPassword != null && !rawPassword.isEmpty()) {
PasswordValidationResult result;
CredentialsInfo credentialsInfo = new CredentialsInfo(username, rawPassword, email, true);
if (id != null) {
User user = userService.getUser(id);
if (user != null) {
credentialsInfo = new CredentialsInfo(user.getUsername(), rawPassword, user.getEmail(), false);
}
}
result = passwordValidationService.validate(credentialsInfo);
if (!result.isValid()) {
message = i18n.getString(result.getI18ErrorMessage());
return ERROR;
}
}
message = i18n.getString("everything_is_ok");
return SUCCESS;
}
use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class RemoveUserAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
//TODO: Allow user with F_USER_DELETE_WITHIN_MANAGED_GROUP to delete a user within managed groups.
User user = userService.getUser(id);
User currentUser = currentUserService.getCurrentUser();
if (currentUser == null || user == null) {
return ERROR;
}
if (!currentUser.getUserCredentials().canModifyUser(user.getUserCredentials())) {
return ERROR;
}
boolean isCurrentUser = currentUser.equals(user);
UserCredentials userCredentials = user.getUserCredentials();
if (userService.isLastSuperUser(userCredentials)) {
message = i18n.getString("can_not_remove_last_super_user");
return ERROR;
} else {
userService.deleteUser(user);
}
if (isCurrentUser) {
return "logout";
}
return SUCCESS;
}
use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class UnregisteredSMSListener method receive.
@Transactional
@Override
public void receive(IncomingSms sms) {
String message = sms.getText();
SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.UNREGISTERED_PARSER);
UserGroup userGroup = smsCommand.getUserGroup();
String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
if (userGroup != null) {
Collection<User> users = userService.getUsersByPhoneNumber(senderPhoneNumber);
if (users != null && users.size() >= 1) {
String messageError = "This number is already registered for user: ";
for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
User user = iterator.next();
messageError += user.getName();
if (iterator.hasNext()) {
messageError += ", ";
}
}
throw new SMSParserException(messageError);
} else {
Set<User> receivers = new HashSet<>(userGroup.getMembers());
UserCredentials anonymousUser = userService.getUserCredentialsByUsername("anonymous");
if (anonymousUser == null) {
User user = new User();
UserCredentials usercredential = new UserCredentials();
usercredential.setUsername(USER_NAME);
usercredential.setPassword(USER_NAME);
usercredential.setUserInfo(user);
user.setSurname(USER_NAME);
user.setFirstName(USER_NAME);
user.setUserCredentials(usercredential);
userService.addUserCredentials(usercredential);
userService.addUser(user);
anonymousUser = userService.getUserCredentialsByUsername("anonymous");
}
// forward to user group by SMS, E-mail, DHIS conversation
messageService.sendMessage(smsCommand.getName(), message, null, receivers, anonymousUser.getUserInfo(), MessageType.SYSTEM, false);
// confirm SMS was received and forwarded completely
Set<User> feedbackList = new HashSet<>();
User sender = new User();
sender.setPhoneNumber(senderPhoneNumber);
feedbackList.add(sender);
smsSender.sendMessage(smsCommand.getName(), smsCommand.getReceivedMessage(), null, null, feedbackList, true);
sms.setStatus(SmsMessageStatus.PROCESSED);
sms.setParsed(true);
incomingSmsService.update(sms);
}
}
}
use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class AbstractEventService method convertProgramStageInstance.
private Event convertProgramStageInstance(ProgramStageInstance programStageInstance) {
if (programStageInstance == null) {
return null;
}
Event event = new Event();
event.setEvent(programStageInstance.getUid());
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
event.setFollowup(programStageInstance.getProgramInstance().getFollowup());
event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(programStageInstance.getProgramInstance().getStatus()));
event.setStatus(programStageInstance.getStatus());
event.setEventDate(DateUtils.getIso8601NoTz(programStageInstance.getExecutionDate()));
event.setDueDate(DateUtils.getIso8601NoTz(programStageInstance.getDueDate()));
event.setStoredBy(programStageInstance.getStoredBy());
event.setCompletedBy(programStageInstance.getCompletedBy());
event.setCompletedDate(DateUtils.getIso8601NoTz(programStageInstance.getCompletedDate()));
event.setCreated(DateUtils.getIso8601NoTz(programStageInstance.getCreated()));
event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
OrganisationUnit ou = programStageInstance.getOrganisationUnit();
if (ou != null) {
if (!organisationUnitService.isInUserHierarchy(ou)) {
if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
}
}
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
Program program = programStageInstance.getProgramInstance().getProgram();
if (!userCredentials.isSuper() && !userCredentials.getAllPrograms().contains(program)) {
throw new IllegalQueryException("User has no access to program: " + program.getUid());
}
event.setProgram(program.getUid());
event.setEnrollment(programStageInstance.getProgramInstance().getUid());
event.setProgramStage(programStageInstance.getProgramStage().getUid());
event.setAttributeOptionCombo(programStageInstance.getAttributeOptionCombo().getUid());
event.setAttributeCategoryOptions(String.join(";", programStageInstance.getAttributeOptionCombo().getCategoryOptions().stream().map(DataElementCategoryOption::getUid).collect(Collectors.toList())));
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
Coordinate coordinate = null;
if (programStageInstance.getLongitude() != null && programStageInstance.getLatitude() != null) {
coordinate = new Coordinate(programStageInstance.getLongitude(), programStageInstance.getLatitude());
try {
List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
});
coordinate.setLongitude(list.get(0));
coordinate.setLatitude(list.get(1));
} catch (IOException ignored) {
}
}
if (coordinate != null && coordinate.isValid()) {
event.setCoordinate(coordinate);
}
}
Collection<TrackedEntityDataValue> dataValues = dataValueService.getTrackedEntityDataValues(programStageInstance);
for (TrackedEntityDataValue dataValue : dataValues) {
DataValue value = new DataValue();
value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
value.setDataElement(dataValue.getDataElement().getUid());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
event.getDataValues().add(value);
}
List<TrackedEntityComment> comments = programStageInstance.getComments();
for (TrackedEntityComment comment : comments) {
Note note = new Note();
note.setValue(comment.getCommentText());
note.setStoredBy(comment.getCreator());
if (comment.getCreatedDate() != null) {
note.setStoredDate(DateUtils.getIso8601NoTz(comment.getCreatedDate()));
}
event.getNotes().add(note);
}
return event;
}
Aggregations