use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.
the class DhisConvenienceTest method createUserAuthorityGroup.
public static UserAuthorityGroup createUserAuthorityGroup(char uniqueCharacter, String... auths) {
UserAuthorityGroup role = new UserAuthorityGroup();
role.setAutoFields();
role.setUid(BASE_UID + uniqueCharacter);
role.setName("UserAuthorityGroup" + uniqueCharacter);
for (String auth : auths) {
role.getAuthorities().add(auth);
}
return role;
}
use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.
the class DhisConvenienceTest method createAdminUser.
protected User createAdminUser(String... authorities) {
checkUserServiceWasInjected();
UserAuthorityGroup group = createAuthorityGroup("Superuser", authorities);
group.setUid("yrB6vc5Ip3r");
String username = DEFAULT_USERNAME;
String password = DEFAULT_ADMIN_PASSWORD;
User user = createUser(username);
user.setUuid(UUID.fromString("6507f586-f154-4ec1-a25e-d7aa51de5216"));
user.setUid("M5zQapPyTZI");
user.setName("Admin");
user.setUsername(username);
user.setPassword(password);
user.getUserAuthorityGroups().add(group);
userService.addUser(user);
userService.encodeAndSetPassword(user, password);
userService.updateUser(user);
userService.addUserAuthorityGroup(group);
return user;
}
use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.
the class AbstractCrudControllerTest method testUpdateObjectProperty.
@Test
void testUpdateObjectProperty() {
String peter = "{'name': 'Peter', 'firstName':'Peter', 'surname':'Pan', 'username':'peter47'}";
String peterUserId = assertStatus(HttpStatus.CREATED, POST("/users", peter));
JsonResponse roles = GET("/userRoles?fields=id").content();
String roleId = roles.getArray("userRoles").getObject(0).getString("id").string();
assertStatus(HttpStatus.NO_CONTENT, POST("/userRoles/" + roleId + "/users/" + peterUserId));
JsonUser oldPeter = GET("/users/{id}", peterUserId).content().as(JsonUser.class);
assertEquals("Peter", oldPeter.getFirstName());
assertEquals(1, oldPeter.getArray("userRoles").size());
List<User> allUsers2 = userService.getAllUsers();
Set<UserAuthorityGroup> g1 = allUsers2.get(0).getUserAuthorityGroups();
Set<UserAuthorityGroup> g2 = allUsers2.get(1).getUserAuthorityGroups();
assertStatus(HttpStatus.NO_CONTENT, PATCH("/users/" + peterUserId + "/firstName", Body("{'firstName': 'Fry'}"), ContentType(MediaType.APPLICATION_JSON)));
List<User> allUsers3 = userService.getAllUsers();
Set<UserAuthorityGroup> g3 = allUsers3.get(0).getUserAuthorityGroups();
Set<UserAuthorityGroup> g4 = allUsers3.get(1).getUserAuthorityGroups();
JsonUser newPeter = GET("/users/{id}", peterUserId).content().as(JsonUser.class);
assertEquals("Fry", newPeter.getFirstName());
// are user roles still there?
assertEquals(1, newPeter.getArray("userRoles").size());
}
use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.
the class AccountController method createAccount.
@PostMapping
@ResponseBody
public WebMessage createAccount(@RequestParam String username, @RequestParam String firstName, @RequestParam String surname, @RequestParam String password, @RequestParam String email, @RequestParam String phoneNumber, @RequestParam String employer, @RequestParam(required = false) String inviteUsername, @RequestParam(required = false) String inviteToken, @RequestParam(value = "g-recaptcha-response", required = false) String recapResponse, HttpServletRequest request) throws IOException {
User user = null;
String restoreToken = null;
boolean invitedByEmail = (inviteUsername != null && !inviteUsername.isEmpty());
boolean canChooseUsername = true;
if (invitedByEmail) {
String[] idAndRestoreToken = securityService.decodeEncodedTokens(inviteToken);
String idToken = idAndRestoreToken[0];
restoreToken = idAndRestoreToken[1];
user = userService.getUserByIdToken(idToken);
if (user == null) {
return badRequest("Invitation link not valid");
}
boolean canRestore = securityService.canRestore(user, restoreToken, RestoreType.INVITE);
if (!canRestore) {
return badRequest("Invitation code not valid");
}
RestoreOptions restoreOptions = securityService.getRestoreOptions(restoreToken);
canChooseUsername = restoreOptions.isUsernameChoice();
if (!email.equals(user.getEmail())) {
return badRequest("Email don't match invited email");
}
} else {
boolean allowed = configurationService.getConfiguration().selfRegistrationAllowed();
if (!allowed) {
return badRequest("User self registration is not allowed");
}
}
// ---------------------------------------------------------------------
// Trim input
// ---------------------------------------------------------------------
username = StringUtils.trimToNull(username);
firstName = StringUtils.trimToNull(firstName);
surname = StringUtils.trimToNull(surname);
password = StringUtils.trimToNull(password);
email = StringUtils.trimToNull(email);
phoneNumber = StringUtils.trimToNull(phoneNumber);
employer = StringUtils.trimToNull(employer);
recapResponse = StringUtils.trimToNull(recapResponse);
CredentialsInfo credentialsInfo = new CredentialsInfo(username, password, email, true);
if (username == null || username.trim().length() > MAX_LENGTH) {
return badRequest("User name is not specified or invalid");
}
User usernameAlreadyTakenCredentials = userService.getUserByUsername(username);
if (canChooseUsername && usernameAlreadyTakenCredentials != null) {
return badRequest("User name is already taken");
}
if (firstName == null || firstName.trim().length() > MAX_LENGTH) {
return badRequest("First name is not specified or invalid");
}
if (surname == null || surname.trim().length() > MAX_LENGTH) {
return badRequest("Last name is not specified or invalid");
}
if (password == null) {
return badRequest("Password is not specified");
}
PasswordValidationResult result = passwordValidationService.validate(credentialsInfo);
if (!result.isValid()) {
return badRequest(result.getErrorMessage());
}
if (email == null || !ValidationUtils.emailIsValid(email)) {
return badRequest("Email is not specified or invalid");
}
if (phoneNumber == null || phoneNumber.trim().length() > MAX_PHONE_NO_LENGTH) {
return badRequest("Phone number is not specified or invalid");
}
if (employer == null || employer.trim().length() > MAX_LENGTH) {
return badRequest("Employer is not specified or invalid");
}
if (!systemSettingManager.selfRegistrationNoRecaptcha()) {
if (recapResponse == null) {
return badRequest("Please verify that you are not a robot");
}
// ---------------------------------------------------------------------
// Check result from API, return 500 if validation failed
// ---------------------------------------------------------------------
RecaptchaResponse recaptchaResponse = securityService.verifyRecaptcha(recapResponse, request.getRemoteAddr());
if (!recaptchaResponse.success()) {
log.warn("Recaptcha validation failed: " + recaptchaResponse.getErrorCodes());
return badRequest("Recaptcha validation failed: " + recaptchaResponse.getErrorCodes());
}
}
if (invitedByEmail) {
boolean restored = securityService.restore(user, restoreToken, password, RestoreType.INVITE);
if (!restored) {
log.info("Invite restore failed for: " + inviteUsername);
return badRequest("Unable to create invited user account");
}
user = new User();
user.setFirstName(firstName);
user.setSurname(surname);
user.setEmail(email);
user.setPhoneNumber(phoneNumber);
user.setEmployer(employer);
if (canChooseUsername) {
user.setUsername(username);
} else {
username = user.getUsername();
}
userService.encodeAndSetPassword(user, password);
userService.updateUser(user);
log.info("User " + username + " accepted invitation for " + inviteUsername);
} else {
UserAuthorityGroup userRole = configurationService.getConfiguration().getSelfRegistrationRole();
OrganisationUnit orgUnit = configurationService.getConfiguration().getSelfRegistrationOrgUnit();
user = new User();
user.setFirstName(firstName);
user.setSurname(surname);
user.setEmail(email);
user.setPhoneNumber(phoneNumber);
user.setEmployer(employer);
user.getOrganisationUnits().add(orgUnit);
user.getDataViewOrganisationUnits().add(orgUnit);
user.setUsername(username);
userService.encodeAndSetPassword(user, password);
user.setSelfRegistered(true);
user.getUserAuthorityGroups().add(userRole);
userService.addUser(user);
log.info("Created user with username: " + username);
}
Set<GrantedAuthority> authorities = getAuthorities(user.getUserAuthorityGroups());
authenticate(username, password, authorities, request);
return ok("Account created");
}
use of org.hisp.dhis.user.UserAuthorityGroup in project dhis2-core by dhis2.
the class DefaultAdminUserPopulator method executeInTransaction.
@Override
public void executeInTransaction() {
// user.
if (userService.getUserCount() > 0) {
return;
}
// ---------------------------------------------------------------------
// Assumes no UserAuthorityGroup called "Superuser" in database
// ---------------------------------------------------------------------
String username = "admin";
String password = "district";
User user = new User();
user.setUid("M5zQapPyTZI");
user.setCode("admin");
user.setFirstName(username);
user.setSurname(username);
userService.addUser(user);
UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
userAuthorityGroup.setUid("yrB6vc5Ip3r");
userAuthorityGroup.setCode("Superuser");
userAuthorityGroup.setName("Superuser");
userAuthorityGroup.setDescription("Superuser");
userAuthorityGroup.setAuthorities(ALL_AUTHORITIES);
userService.addUserAuthorityGroup(userAuthorityGroup);
user.setUuid(UUID.fromString("6507f586-f154-4ec1-a25e-d7aa51de5216"));
user.setCode(username);
user.setUsername(username);
user.getUserAuthorityGroups().add(userAuthorityGroup);
userService.encodeAndSetPassword(user, password);
userService.addUser(user);
}
Aggregations