use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class DhisBindAuthenticator method authenticate.
@Override
public DirContextOperations authenticate(Authentication authentication) {
boolean ldapConf = configurationProvider.isLdapConfigured();
if (!ldapConf) {
throw new BadCredentialsException("LDAP authentication is not configured");
}
UserCredentials userCredentials = userService.getUserCredentialsByUsername(authentication.getName());
if (userCredentials == null) {
throw new BadCredentialsException("Incorrect user credentials");
}
if (userCredentials.hasLdapId()) {
authentication = new UsernamePasswordAuthenticationToken(userCredentials.getLdapId(), authentication.getCredentials());
}
return super.authenticate(authentication);
}
use of org.hisp.dhis.user.UserCredentials 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.UserCredentials in project dhis2-core by dhis2.
the class HibernateUserCredentialsStore method getUserCredentialsByUsername.
@Override
public UserCredentials getUserCredentialsByUsername(String username) {
Query query = getQuery("from UserCredentials uc where uc.username = :username");
query.setString("username", username);
return (UserCredentials) query.uniqueResult();
}
use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class AddUserAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
if (!userService.canAddOrUpdateUser(ugSelected)) {
throw new AccessDeniedException("You cannot add this user");
}
User currentUser = currentUserService.getCurrentUser();
// ---------------------------------------------------------------------
// User credentials and user
// ---------------------------------------------------------------------
UserCredentials userCredentials = new UserCredentials();
User user = new User();
userCredentials.setUserInfo(user);
user.setUserCredentials(userCredentials);
userCredentials.setUsername(StringUtils.trimToNull(username));
userCredentials.setExternalAuth(externalAuth);
userCredentials.setOpenId(StringUtils.trimToNull(openId));
userCredentials.setLdapId(StringUtils.trimToNull(ldapId));
if (ACCOUNT_ACTION_INVITE.equals(accountAction)) {
userCredentials.setUsername(StringUtils.trimToNull(inviteUsername));
userCredentials.setInvitation(true);
user.setEmail(StringUtils.trimToNull(inviteEmail));
securityService.prepareUserForInvite(user);
} else {
user.setSurname(StringUtils.trimToNull(surname));
user.setFirstName(StringUtils.trimToNull(firstName));
user.setEmail(StringUtils.trimToNull(email));
user.setPhoneNumber(StringUtils.trimToNull(phoneNumber));
userService.encodeAndSetPassword(userCredentials, StringUtils.trimToNull(rawPassword));
}
if (jsonAttributeValues != null) {
attributeService.updateAttributeValues(user, jsonAttributeValues);
}
// ---------------------------------------------------------------------
// Organisation units
// ---------------------------------------------------------------------
Set<OrganisationUnit> dataCaptureOrgUnits = new HashSet<>(selectionManager.getSelectedOrganisationUnits());
user.updateOrganisationUnits(dataCaptureOrgUnits);
Set<OrganisationUnit> dataViewOrgUnits = new HashSet<>(selectionTreeManager.getReloadedSelectedOrganisationUnits());
user.setDataViewOrganisationUnits(dataViewOrgUnits);
if (dataViewOrgUnits.size() == 0 && currentUser.getDataViewOrganisationUnits().size() != 0) {
user.setDataViewOrganisationUnits(new HashSet<>(currentUser.getDataViewOrganisationUnits()));
}
// ---------------------------------------------------------------------
// User roles
// ---------------------------------------------------------------------
Set<UserAuthorityGroup> userAuthorityGroups = new HashSet<>();
for (String id : urSelected) {
userAuthorityGroups.add(userService.getUserAuthorityGroup(id));
}
userService.canIssueFilter(userAuthorityGroups);
userCredentials.setUserAuthorityGroups(userAuthorityGroups);
// ---------------------------------------------------------------------
// Dimension constraints. Note that any new user must inherit dimension
// constraints if any from the current user.
// ---------------------------------------------------------------------
userCredentials.setCogsDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCogsDimensionConstraints()));
userCredentials.setCatDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCatDimensionConstraints()));
for (String id : dcSelected) {
CategoryOptionGroupSet cogs = categoryService.getCategoryOptionGroupSet(id);
if (cogs != null) {
userCredentials.getCogsDimensionConstraints().add(cogs);
continue;
}
DataElementCategory cat = categoryService.getDataElementCategory(id);
if (cat != null) {
userCredentials.getCatDimensionConstraints().add(cat);
continue;
}
}
// ---------------------------------------------------------------------
// Add User
// ---------------------------------------------------------------------
userService.addUser(user);
userService.addUserCredentials(userCredentials);
// ---------------------------------------------------------------------
// User settings
// ---------------------------------------------------------------------
userSettingService.saveUserSetting(UserSettingKey.UI_LOCALE, LocaleUtils.getLocale(localeUi), user);
userSettingService.saveUserSetting(UserSettingKey.DB_LOCALE, LocaleUtils.getLocale(localeDb), user);
if (ACCOUNT_ACTION_INVITE.equals(accountAction)) {
RestoreOptions restoreOptions = inviteUsername == null || inviteUsername.isEmpty() ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
securityService.sendRestoreMessage(userCredentials, getRootPath(), restoreOptions);
}
for (String id : ugSelected) {
UserGroup userGroup = userGroupService.getUserGroup(id);
userGroup.addUser(user);
userGroupService.updateUserGroup(userGroup);
}
if (ouwtSelected != null && manager.search(OrganisationUnit.class, ouwtSelected) != null) {
selectionManager.setSelectedOrganisationUnits(Lists.newArrayList(manager.search(OrganisationUnit.class, ouwtSelected)));
} else {
selectionManager.setSelectedOrganisationUnits(currentUser.getOrganisationUnits());
}
return SUCCESS;
}
use of org.hisp.dhis.user.UserCredentials in project dhis2-core by dhis2.
the class UserObjectBundleHook method postUpdate.
@Override
public void postUpdate(IdentifiableObject persistedObject, ObjectBundle bundle) {
if (!User.class.isInstance(persistedObject) || !bundle.hasExtras(persistedObject, "uc"))
return;
User user = (User) persistedObject;
final UserCredentials userCredentials = (UserCredentials) bundle.getExtras(persistedObject, "uc");
final UserCredentials persistedUserCredentials = bundle.getPreheat().get(bundle.getPreheatIdentifier(), UserCredentials.class, user);
if (!StringUtils.isEmpty(userCredentials.getPassword())) {
userService.encodeAndSetPassword(userCredentials, userCredentials.getPassword());
}
mergeService.merge(new MergeParams<>(userCredentials, persistedUserCredentials).setMergeMode(bundle.getMergeMode()));
preheatService.connectReferences(persistedUserCredentials, bundle.getPreheat(), bundle.getPreheatIdentifier());
persistedUserCredentials.setUserInfo(user);
user.setUserCredentials(persistedUserCredentials);
sessionFactory.getCurrentSession().update(user.getUserCredentials());
bundle.removeExtras(persistedObject, "uc");
}
Aggregations