FINERACT-1060: Stop delete / update of system user

This commit is contained in:
Petri Tuomola
2020-07-03 15:35:33 +02:00
committed by Michael Vorburger ⛑️
parent c92e926d6c
commit 551819ee18
5 changed files with 58 additions and 3 deletions
@@ -32,6 +32,7 @@ import org.apache.fineract.integrationtests.common.Utils;
import org.apache.fineract.integrationtests.common.organisation.StaffHelper;
import org.apache.fineract.integrationtests.useradministration.roles.RolesHelper;
import org.apache.fineract.integrationtests.useradministration.users.UserHelper;
import org.apache.fineract.useradministration.service.AppUserConstants;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -155,4 +156,19 @@ public class UserAdministrationTest {
reason.get("defaultUserMessage"));
}
@Test
public void testDeleteSystemUser() {
final Integer userId = UserHelper.getUserId(requestSpec, responseSpec, AppUserConstants.SYSTEM_USER_NAME);
Assertions.assertNotNull(userId);
UserHelper.deleteUser(requestSpec, expectStatusCode(403), userId.intValue());
}
@Test
public void testModifySystemUser() {
final Integer userId = UserHelper.getUserId(requestSpec, responseSpec, AppUserConstants.SYSTEM_USER_NAME);
Assertions.assertNotNull(userId);
final List errors = (List) UserHelper.updateUser(this.requestSpec, expectStatusCode(403), userId, "systemtest", "errors");
}
}
@@ -18,9 +18,13 @@
*/
package org.apache.fineract.integrationtests.useradministration.users;
import io.restassured.path.json.JsonPath;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import java.util.HashMap;
import java.util.List;
import org.apache.fineract.integrationtests.common.Utils;
import org.junit.jupiter.api.Assertions;
public class UserHelper {
@@ -44,6 +48,20 @@ public class UserHelper {
getTestCreateUserAsJSONForSelfService(roleId, staffId, clientId), attribute);
}
public static Integer getUserId(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, String userName) {
String json = Utils.performServerGet(requestSpec, responseSpec, CREATE_USER_URL, null);
Assertions.assertNotNull(json);
List<HashMap<String, Object>> userList = JsonPath.from(json).getList("$");
for (HashMap<String, Object> user : userList) {
if (user.get("username").equals(userName)) {
return (Integer) user.get("id");
}
}
return null;
}
public static String getTestCreateUserAsJSON(int roleId, int staffId) {
return "{ \"username\": \"" + Utils.randomNameGenerator("User_Name_", 3)
+ "\", \"firstname\": \"Test\", \"lastname\": \"User\", \"email\": \"whatever@mifos.org\","
@@ -264,6 +264,12 @@ public class AppUser extends AbstractPersistableCustom implements PlatformUser {
final String usernameParamName = "username";
if (command.isChangeInStringParameterNamed(usernameParamName, this.username)) {
// TODO Remove this check once we are identifying system user based on user ID
if (isSystemUser()) {
throw new NoAuthorizationException("User name of current system user may not be modified");
}
final String newValue = command.stringValueOfParameterNamed(usernameParamName);
actualChanges.put(usernameParamName, newValue);
this.username = newValue;
@@ -343,6 +349,10 @@ public class AppUser extends AbstractPersistableCustom implements PlatformUser {
* Any fields with unique constraints and prepended with id of record.
*/
public void delete() {
if (isSystemUser()) {
throw new NoAuthorizationException("User configured as the system user cannot be deleted");
}
this.deleted = true;
this.enabled = false;
this.accountNonExpired = false;
@@ -355,6 +365,15 @@ public class AppUser extends AbstractPersistableCustom implements PlatformUser {
return this.deleted;
}
public boolean isSystemUser() {
// TODO Determine system user by ID not by user name
if (this.username.equals(AppUserConstants.SYSTEM_USER_NAME)) {
return true;
}
return false;
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
return populateGrantedAuthorities();
@@ -19,6 +19,7 @@
package org.apache.fineract.useradministration.domain;
import org.apache.fineract.useradministration.exception.UserNotFoundException;
import org.apache.fineract.useradministration.service.AppUserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -33,10 +34,9 @@ public class AppUserRepositoryWrapper {
}
public AppUser fetchSystemUser() {
String userName = "system";
AppUser user = this.appUserRepository.findAppUserByName(userName);
AppUser user = this.appUserRepository.findAppUserByName(AppUserConstants.SYSTEM_USER_NAME);
if (user == null) {
throw new UserNotFoundException(userName);
throw new UserNotFoundException(AppUserConstants.SYSTEM_USER_NAME);
}
return user;
}
@@ -24,4 +24,6 @@ public class AppUserConstants {
public static final String IS_SELF_SERVICE_USER = "isSelfServiceUser";
public static final String CLIENTS = "clients";
// TODO: Remove hard coding of system user name and make this a configurable parameter
public static final String SYSTEM_USER_NAME = "system";
}