Fixed scope update issue in API update

revert-70aa11f8
mharindu 8 years ago
parent b3d309583d
commit 827567733c

@ -107,6 +107,10 @@
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId> <artifactId>javax.ws.rs-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
</dependency>
</dependencies> </dependencies>

@ -18,7 +18,6 @@
package org.wso2.carbon.apimgt.webapp.publisher; package org.wso2.carbon.apimgt.webapp.publisher;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.APIManagementException;
@ -30,6 +29,10 @@ import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
import org.wso2.carbon.apimgt.webapp.publisher.config.WebappPublisherConfig; import org.wso2.carbon.apimgt.webapp.publisher.config.WebappPublisherConfig;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.core.util.Utils; import org.wso2.carbon.core.util.Utils;
import org.wso2.carbon.device.mgt.common.scope.mgt.ScopeManagementException;
import org.wso2.carbon.device.mgt.common.scope.mgt.ScopeManagementService;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import java.util.*; import java.util.*;
@ -121,16 +124,18 @@ public class APIPublisherUtil {
if (scope != null) { if (scope != null) {
if (apiScopes.get(scope.getKey()) == null) { if (apiScopes.get(scope.getKey()) == null) {
apiScopes.put(scope.getKey(), scope); apiScopes.put(scope.getKey(), scope);
} else {
existingScope = apiScopes.get(scope.getKey());
existingPermissions = existingScope.getRoles();
existingPermissions = getDistinctPermissions(existingPermissions + "," + scope.getRoles());
existingScope.setRoles(existingPermissions);
apiScopes.put(scope.getKey(), existingScope);
} }
} }
} }
Set<Scope> scopes = new HashSet<>(apiScopes.values()); Set<Scope> scopes = new HashSet<>(apiScopes.values());
// adding existing persisted roles to the scopes
try {
setExistingRoles(scopes);
} catch (ScopeManagementException | UserStoreException e) {
throw new APIManagementException("Error occurred while retrieving roles for the existing scopes");
}
// set current scopes to API
api.setScopes(scopes); api.setScopes(scopes);
// this has to be done because of the use of pass by reference // this has to be done because of the use of pass by reference
@ -307,9 +312,34 @@ public class APIPublisherUtil {
return apiConfig; return apiConfig;
} }
private static String getDistinctPermissions(String permissions) { /**
String[] unique = new HashSet<String>(Arrays.asList(permissions.split(","))).toArray(new String[0]); * This method is used to set the existing roles of the given scope.
return StringUtils.join(unique, ","); *
* @param scopes List of scopes.
* @throws ScopeManagementException
*/
private static void setExistingRoles(Set<Scope> scopes) throws ScopeManagementException, UserStoreException {
String scopeKey;
String roles;
ScopeManagementService scopeManagementService = WebappPublisherUtil.getScopeManagementService();
UserRealm userRealm = WebappPublisherUtil.getUserRealm();
if (scopeManagementService == null) {
throw new ScopeManagementException("Error occurred while initializing scope management service");
} else if (userRealm == null) {
throw new UserStoreException("Error occurred while initializing realm service");
} else {
String adminRole = userRealm.getRealmConfiguration().getAdminRoleName();
for (Scope scope : scopes) {
scopeKey = scope.getKey();
roles = scopeManagementService.getRolesOfScope(scopeKey);
if (roles == null) {
roles = adminRole;
}
scope.setRoles(roles);
}
}
} }
} }

@ -18,7 +18,16 @@
package org.wso2.carbon.apimgt.webapp.publisher; package org.wso2.carbon.apimgt.webapp.publisher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.scope.mgt.ScopeManagementService;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -31,6 +40,10 @@ import java.io.File;
*/ */
public class WebappPublisherUtil { public class WebappPublisherUtil {
private static Log log = LogFactory.getLog(WebappPublisherUtil.class);
private static final int CARBON_SUPER = -1234;
public static Document convertToDocument(File file) throws WebappPublisherConfigurationFailedException { public static Document convertToDocument(File file) throws WebappPublisherConfigurationFailedException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
@ -44,4 +57,32 @@ public class WebappPublisherUtil {
} }
} }
public static ScopeManagementService getScopeManagementService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ScopeManagementService scopeManagementService =
(ScopeManagementService) ctx.getOSGiService(ScopeManagementService.class, null);
if (scopeManagementService == null) {
String msg = "Scope Management Service has not been initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
return scopeManagementService;
}
/**
* Getting the current tenant's user realm
*/
public static UserRealm getUserRealm() throws UserStoreException {
RealmService realmService;
UserRealm realm;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
realmService = (RealmService) ctx.getOSGiService(RealmService.class, null);
if (realmService == null) {
throw new IllegalStateException("Realm service not initialized");
}
realm = realmService.getTenantUserRealm(CARBON_SUPER);
return realm;
}
} }

@ -42,4 +42,12 @@ public interface ScopeManagementService {
*/ */
List<Scope> getAllScopes() throws ScopeManagementException; List<Scope> getAllScopes() throws ScopeManagementException;
/**
* This method is to retrieve the roles of the given scope
* @param scopeKey key of the scope
* @return List of roles
* @throws ScopeManagementException
*/
String getRolesOfScope(String scopeKey) throws ScopeManagementException;
} }

@ -66,13 +66,32 @@ public class ScopeManagementServiceImpl implements ScopeManagementService {
ScopeManagementDAOFactory.openConnection(); ScopeManagementDAOFactory.openConnection();
scopes = scopeManagementDAO.getAllScopes(); scopes = scopeManagementDAO.getAllScopes();
} catch (SQLException e) { } catch (SQLException e) {
throw new ScopeManagementException("SQL error occurred while adding scopes to database.", e); throw new ScopeManagementException("SQL error occurred while retrieving scopes from database.", e);
} catch (ScopeManagementDAOException e) { } catch (ScopeManagementDAOException e) {
throw new ScopeManagementException("Error occurred while adding scopes to database.", e); throw new ScopeManagementException("Error occurred while retrieving scopes from database.", e);
} finally { } finally {
ScopeManagementDAOFactory.closeConnection(); ScopeManagementDAOFactory.closeConnection();
} }
return scopes; return scopes;
} }
@Override
public String getRolesOfScope(String scopeKey) throws ScopeManagementException {
String roles;
if (scopeKey == null || scopeKey.isEmpty()) {
throw new ScopeManagementException("Scope key is null or empty");
}
try {
ScopeManagementDAOFactory.openConnection();
roles = scopeManagementDAO.getRolesOfScope(scopeKey);
} catch (SQLException e) {
throw new ScopeManagementException("SQL error occurred while retrieving roles of scope from database.", e);
} catch (ScopeManagementDAOException e) {
throw new ScopeManagementException("Error occurred while retrieving roles of scope from database.", e);
} finally {
ScopeManagementDAOFactory.closeConnection();
}
return roles;
}
} }

@ -43,4 +43,12 @@ public interface ScopeManagementDAO {
*/ */
List<Scope> getAllScopes() throws ScopeManagementDAOException; List<Scope> getAllScopes() throws ScopeManagementDAOException;
/**
* This method is to retrieve the roles of the given scope
* @param scopeKey key of the scope
* @return List of roles
* @throws ScopeManagementDAOException
*/
String getRolesOfScope(String scopeKey) throws ScopeManagementDAOException;
} }

@ -89,6 +89,31 @@ public class ScopeManagementDAOImpl implements ScopeManagementDAO {
} }
} }
@Override
public String getRolesOfScope(String scopeKey) throws ScopeManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String roles = null;
try {
conn = this.getConnection();
String sql = "SELECT ROLES FROM IDN_OAUTH2_SCOPE WHERE SCOPE_KEY = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, scopeKey);
rs = stmt.executeQuery();
if (rs.next()) {
roles = rs.getString("ROLES");
}
return roles;
} catch (SQLException e) {
throw new ScopeManagementDAOException("Error occurred while fetching the details of the scopes.", e);
} finally {
ScopeManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return ScopeManagementDAOFactory.getConnection(); return ScopeManagementDAOFactory.getConnection();
} }

Loading…
Cancel
Save