Skip to content

Commit

Permalink
Fix reviewed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kalaiyarasiganeshalingam committed Jan 27, 2025
1 parent f2e3dcf commit 3a41869
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.wso2.micro.integrator.management.apis.security.handler.SecurityUtils;
import org.wso2.micro.integrator.security.user.api.UserStoreException;
import org.wso2.micro.integrator.security.user.api.UserStoreManager;
import org.wso2.micro.integrator.security.user.core.util.UserCoreUtil;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -62,13 +63,12 @@ public class UserResource implements MiApiResource {

// HTTP method types supported by the resource
protected Set<String> methods;
private String superAdminUsername = "";

public UserResource() {
methods = new HashSet<>();
methods.add(Constants.HTTP_GET);
methods.add(Constants.HTTP_DELETE);
methods.add(Constants.HTTP_METHOD_PATCH);
superAdminUsername = Utils.getSuperAdminUserName();
}

@Override
Expand Down Expand Up @@ -163,8 +163,7 @@ protected JSONObject handleDelete(MessageContext messageContext)
}
UserStoreManager userStoreManager = Utils.getUserStore(domain);
String[] roles = userStoreManager.getRoleListOfUser(user);

if (this.superAdminUsername.equals(performedBy)) {
if (UserCoreUtil.isPrimaryAdminUser(performedBy, Utils.getRealmConfiguration())) {
userStoreManager.deleteUser(user);
} else if (!Arrays.asList(roles).contains(ADMIN)) {
userStoreManager.deleteUser(user);
Expand Down Expand Up @@ -215,7 +214,7 @@ protected JSONObject handlePatch(MessageContext messageContext,
throw new UserStoreException("The current user password cannot be null.");
}
userStoreManager.updateCredential(user, newPassword, oldPassword);
} else if (this.superAdminUsername.equals(performedBy)) {
} else if (UserCoreUtil.isPrimaryAdminUser(performedBy, Utils.getRealmConfiguration())) {
userStoreManager.updateCredentialByAdmin(user, newPassword);
} else if (Arrays.asList(performerRoles).contains(ADMIN) &&
!Arrays.asList(userRoles).contains(ADMIN)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.llom.OMTextImpl;
import org.apache.axis2.AxisFault;
import org.apache.commons.io.IOUtils;
Expand All @@ -38,11 +36,7 @@
import org.ops4j.pax.logging.PaxLoggingConstants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.useradmin.User;
import org.wso2.micro.core.util.AuditLogger;
import org.wso2.micro.core.util.CarbonException;
import org.wso2.micro.integrator.core.internal.MicroIntegratorBaseConstants;
import org.wso2.micro.integrator.core.util.MicroIntegratorBaseUtils;
import org.wso2.micro.integrator.initializer.utils.ConfigurationHolder;
import org.wso2.micro.integrator.registry.MicroIntegratorRegistry;
import org.wso2.micro.integrator.security.MicroIntegratorSecurityUtils;
Expand All @@ -54,14 +48,11 @@
import org.wso2.micro.service.mgt.ServiceAdmin;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -90,14 +81,6 @@
public class Utils {

private static final Log LOG = LogFactory.getLog(Utils.class);
public static final String USER_MGT_XML_PATH = "wso2.user.mgt.xml";
public static final String REALM = "Realm";
public static final String CONFIGURATION = "Configuration";
public static final String ADMIN_USER = "AdminUser";
public static final String USERNAME = "UserName";
public static final String REPOSITORY = "repository";
public static final String CONF = "conf";
public static final String MGT_FILE_NAME = "user-mgt.xml";

public static String getQueryParameter(MessageContext messageContext, String key) {

Expand Down Expand Up @@ -704,50 +687,4 @@ public static String getResourceName(String path) {
}
return "";
}

static String getSuperAdminUserName() {
String userMgt = getUserMgtXMLPath();
if (userMgt != null) {
File userMgtXml = new File(userMgt);
if (!userMgtXml.exists()) {
LOG.error("Error occurred while getting username of super admin: User-mgt.xml is not found");
return null;
}
try (InputStream inStream = Files.newInputStream(Paths.get(userMgt))) {
StAXOMBuilder builder = new StAXOMBuilder(inStream);
OMElement configuration = builder.getDocumentElement();
return configuration.getFirstChildWithName(new QName(REALM)).
getFirstChildWithName(new QName(CONFIGURATION)).
getFirstChildWithName(new QName(ADMIN_USER)).
getFirstChildWithName(new QName(USERNAME)).getText();
} catch (XMLStreamException | IOException e) {
LOG.error("Error occurred while getting username of super admin: " + e.getMessage());
return null;
}
} else {
return null;
}
}
private static String getUserMgtXMLPath() {
String carbonHome = getCarbonHome();
if (carbonHome != null) {
String configPath = System.getProperty(USER_MGT_XML_PATH);
if (configPath == null) {
configPath = Paths.get(getCarbonConfigDirPath() , MGT_FILE_NAME).toString();
}
return configPath;
}
return null;
}

private static String getCarbonConfigDirPath() {
String carbonConfigDirPath = System.getProperty(MicroIntegratorBaseConstants.CARBON_CONFIG_DIR_PATH);
if (carbonConfigDirPath == null) {
carbonConfigDirPath = System.getenv(MicroIntegratorBaseConstants.CARBON_CONFIG_DIR_PATH_ENV);
if (carbonConfigDirPath == null) {
return Paths.get(getCarbonHome(), REPOSITORY, CONF).toString();
}
}
return carbonConfigDirPath;
}
}

0 comments on commit 3a41869

Please sign in to comment.