forked from community/device-mgt-core
parent
05e95e2675
commit
5727886ea1
@ -1,173 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt;
|
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
||||||
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
|
||||||
import org.wso2.carbon.device.mgt.common.scope.mgt.ScopeManagementException;
|
|
||||||
import org.wso2.carbon.device.mgt.common.scope.mgt.ScopeManagementService;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAO;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAOFactory;
|
|
||||||
|
|
||||||
import java.lang.annotation.Inherited;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is an implementation of a Scope Management Service.
|
|
||||||
*/
|
|
||||||
public class ScopeManagementServiceImpl implements ScopeManagementService {
|
|
||||||
|
|
||||||
private ScopeManagementDAO scopeManagementDAO;
|
|
||||||
|
|
||||||
public ScopeManagementServiceImpl() {
|
|
||||||
this.scopeManagementDAO = ScopeManagementDAOFactory.getScopeManagementDAO();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateScopes(List<Scope> scopes) throws ScopeManagementException {
|
|
||||||
try {
|
|
||||||
ScopeManagementDAOFactory.beginTransaction();
|
|
||||||
scopeManagementDAO.updateScopes(scopes);
|
|
||||||
ScopeManagementDAOFactory.commitTransaction();
|
|
||||||
} catch (TransactionManagementException e) {
|
|
||||||
ScopeManagementDAOFactory.rollbackTransaction();
|
|
||||||
throw new ScopeManagementException("Transactional error occurred while adding the scopes.", e);
|
|
||||||
} catch (ScopeManagementDAOException e) {
|
|
||||||
ScopeManagementDAOFactory.rollbackTransaction();
|
|
||||||
throw new ScopeManagementException("Error occurred while adding the scopes to database.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateScopes(List<String> scopeKeys, String roleName) throws ScopeManagementException {
|
|
||||||
List<Scope> scopes = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
List<Scope> allScopes = this.getAllScopes();
|
|
||||||
for (Scope scope : allScopes) {
|
|
||||||
for (String key : scopeKeys) {
|
|
||||||
if (scope.getKey().equals(key)) {
|
|
||||||
scope.setRoles(scope.getRoles() + "," + roleName);
|
|
||||||
scopes.add(scope);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ScopeManagementDAOFactory.beginTransaction();
|
|
||||||
scopeManagementDAO.updateScopes(scopes);
|
|
||||||
ScopeManagementDAOFactory.commitTransaction();
|
|
||||||
} catch (TransactionManagementException e) {
|
|
||||||
ScopeManagementDAOFactory.rollbackTransaction();
|
|
||||||
throw new ScopeManagementException("Transactional error occurred while adding the scopes.", e);
|
|
||||||
} catch (ScopeManagementDAOException e) {
|
|
||||||
ScopeManagementDAOFactory.rollbackTransaction();
|
|
||||||
throw new ScopeManagementException("Error occurred while adding the scopes to database.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Scope> getAllScopes() throws ScopeManagementException {
|
|
||||||
List<Scope> scopes = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
ScopeManagementDAOFactory.openConnection();
|
|
||||||
scopes = scopeManagementDAO.getAllScopes();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new ScopeManagementException("SQL error occurred while retrieving scopes from database.", e);
|
|
||||||
} catch (ScopeManagementDAOException e) {
|
|
||||||
throw new ScopeManagementException("Error occurred while retrieving scopes from database.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Scope> getScopesOfRole(String roleName) throws ScopeManagementException {
|
|
||||||
if (roleName == null || roleName.isEmpty()) {
|
|
||||||
throw new ScopeManagementException("Role name is null or empty");
|
|
||||||
}
|
|
||||||
List<Scope> filteredScopes = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
ScopeManagementDAOFactory.openConnection();
|
|
||||||
List<Scope> allScopes = scopeManagementDAO.getScopesHavingRole(roleName);
|
|
||||||
|
|
||||||
String roles[];
|
|
||||||
for (Scope scope : allScopes) {
|
|
||||||
roles = scope.getRoles().split(",");
|
|
||||||
for (String role : roles) {
|
|
||||||
if (roleName.equals(role.trim())) {
|
|
||||||
filteredScopes.add(scope);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new ScopeManagementException("SQL error occurred while retrieving scopes of role from database.", e);
|
|
||||||
} catch (ScopeManagementDAOException e) {
|
|
||||||
throw new ScopeManagementException("Error occurred while retrieving scopes of role from database.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOFactory.closeConnection();
|
|
||||||
}
|
|
||||||
return filteredScopes;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeScopes(String roleName) throws ScopeManagementException {
|
|
||||||
|
|
||||||
List<Scope> scopes = this.getScopesOfRole(roleName);
|
|
||||||
String roles[];
|
|
||||||
ArrayList<String> filteredRoles = new ArrayList<>();
|
|
||||||
for (Scope scope : scopes) {
|
|
||||||
roles = scope.getRoles().split(",");
|
|
||||||
for (String role : roles) {
|
|
||||||
if (!roleName.equals(role.trim())) {
|
|
||||||
filteredRoles.add(role);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scope.setRoles(StringUtils.join(filteredRoles, ","));
|
|
||||||
filteredRoles.clear();
|
|
||||||
}
|
|
||||||
this.updateScopes(scopes);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt.dao;
|
|
||||||
|
|
||||||
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This interface contains the basic database operations related to scope management.
|
|
||||||
*/
|
|
||||||
public interface ScopeManagementDAO {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is used to update the list of scopes.
|
|
||||||
*
|
|
||||||
* @param scopes List of scopes to be updated.
|
|
||||||
* @throws ScopeManagementDAOException
|
|
||||||
*/
|
|
||||||
void updateScopes(List<Scope> scopes) throws ScopeManagementDAOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is used to retrieve all the scopes.
|
|
||||||
*
|
|
||||||
* @return List of scopes.
|
|
||||||
* @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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is to retrieve all the scopes of the given role name.
|
|
||||||
* Thus it returns the scopes even if the part of the given name is matched.
|
|
||||||
*
|
|
||||||
* @param roleName Role name
|
|
||||||
* @return List of scopes
|
|
||||||
* @throws ScopeManagementDAOException
|
|
||||||
*/
|
|
||||||
List<Scope> getScopesHavingRole(String roleName) throws ScopeManagementDAOException;
|
|
||||||
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt.dao;
|
|
||||||
|
|
||||||
public class ScopeManagementDAOException extends Exception {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -315127931137771199L;
|
|
||||||
|
|
||||||
private String errorMessage;
|
|
||||||
|
|
||||||
public String getErrorMessage() {
|
|
||||||
return errorMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setErrorMessage(String errorMessage) {
|
|
||||||
this.errorMessage = errorMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeManagementDAOException(String msg, Exception nestedEx) {
|
|
||||||
super(msg, nestedEx);
|
|
||||||
setErrorMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeManagementDAOException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
setErrorMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeManagementDAOException(String msg) {
|
|
||||||
super(msg);
|
|
||||||
setErrorMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeManagementDAOException() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeManagementDAOException(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,139 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt.dao;
|
|
||||||
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
|
||||||
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.impl.ScopeManagementDAOImpl;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class ScopeManagementDAOFactory {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ScopeManagementDAOFactory.class);
|
|
||||||
private static DataSource dataSource;
|
|
||||||
private static String databaseEngine;
|
|
||||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
|
||||||
|
|
||||||
public static ScopeManagementDAO getScopeManagementDAO() {
|
|
||||||
return new ScopeManagementDAOImpl();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void init(String dataSourceName) {
|
|
||||||
dataSource = resolveDataSource(dataSourceName);
|
|
||||||
try {
|
|
||||||
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.error("Error occurred while retrieving config.datasource connection", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void beginTransaction() throws TransactionManagementException {
|
|
||||||
try {
|
|
||||||
Connection conn = dataSource.getConnection();
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
currentConnection.set(conn);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new TransactionManagementException(
|
|
||||||
"Error occurred while retrieving config.datasource connection", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void openConnection() throws SQLException {
|
|
||||||
currentConnection.set(dataSource.getConnection());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Connection getConnection() throws SQLException {
|
|
||||||
if (currentConnection.get() == null) {
|
|
||||||
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
|
||||||
"This might have ideally caused by not properly initiating the transaction via " +
|
|
||||||
"'beginTransaction'/'openConnection' methods");
|
|
||||||
}
|
|
||||||
return currentConnection.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void closeConnection() {
|
|
||||||
Connection con = currentConnection.get();
|
|
||||||
if (con != null) {
|
|
||||||
try {
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.error("Error occurred while close the connection");
|
|
||||||
}
|
|
||||||
currentConnection.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void commitTransaction() {
|
|
||||||
try {
|
|
||||||
Connection conn = currentConnection.get();
|
|
||||||
if (conn != null) {
|
|
||||||
conn.commit();
|
|
||||||
} else {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Datasource connection associated with the current thread is null, hence commit " +
|
|
||||||
"has not been attempted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.error("Error occurred while committing the transaction", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void rollbackTransaction() {
|
|
||||||
try {
|
|
||||||
Connection conn = currentConnection.get();
|
|
||||||
if (conn != null) {
|
|
||||||
conn.rollback();
|
|
||||||
} else {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
|
|
||||||
"has not been attempted");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.error("Error occurred while roll-backing the transaction", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve data source from the data source name.
|
|
||||||
*
|
|
||||||
* @param dataSourceName data source name
|
|
||||||
* @return data source resolved from the data source definition
|
|
||||||
*/
|
|
||||||
private static DataSource resolveDataSource(String dataSourceName) {
|
|
||||||
DataSource dataSource;
|
|
||||||
if (dataSourceName == null || dataSourceName.isEmpty()) {
|
|
||||||
throw new RuntimeException("Scope Management Repository data source configuration is null and " +
|
|
||||||
"thus, is not initialized");
|
|
||||||
}
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Initializing Scope Management Repository data source using the JNDI Lookup Definition");
|
|
||||||
}
|
|
||||||
dataSource = DeviceManagementDAOUtil.lookupDataSource(dataSourceName, null);
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt.dao;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class ScopeManagementDAOUtil {
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ScopeManagementDAOUtil.class);
|
|
||||||
|
|
||||||
public static void cleanupResources(Statement stmt, ResultSet rs) {
|
|
||||||
if (rs != null) {
|
|
||||||
try {
|
|
||||||
rs.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.warn("Error occurred while closing the result set", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (stmt != null) {
|
|
||||||
try {
|
|
||||||
stmt.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.warn("Error occurred while closing the statement", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static void cleanupResources(Statement stmt) {
|
|
||||||
if (stmt != null) {
|
|
||||||
try {
|
|
||||||
stmt.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
log.warn("Error occurred while closing the statement", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,148 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2016 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
||||||
*
|
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
|
||||||
* in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.wso2.carbon.device.mgt.core.scope.mgt.dao.impl;
|
|
||||||
|
|
||||||
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAO;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAOFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.core.scope.mgt.dao.ScopeManagementDAOUtil;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ScopeManagementDAOImpl implements ScopeManagementDAO {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateScopes(List<Scope> scopes) throws ScopeManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "UPDATE IDN_OAUTH2_SCOPE SET ROLES=? WHERE SCOPE_KEY=?";
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
|
|
||||||
// creating a batch request
|
|
||||||
for (Scope scope : scopes) {
|
|
||||||
stmt.setString(1, scope.getRoles());
|
|
||||||
stmt.setString(2, scope.getKey());
|
|
||||||
stmt.addBatch();
|
|
||||||
}
|
|
||||||
stmt.executeBatch();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new ScopeManagementDAOException("Error occurred while updating the details of the scopes.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public List<Scope> getAllScopes() throws ScopeManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
List<Scope> scopes;
|
|
||||||
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "SELECT * FROM IDN_OAUTH2_SCOPE";
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
rs = stmt.executeQuery();
|
|
||||||
scopes = this.getScopesFromResultSet(rs);
|
|
||||||
return scopes;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new ScopeManagementDAOException("Error occurred while fetching the details of the scopes.", e);
|
|
||||||
} finally {
|
|
||||||
ScopeManagementDAOUtil.cleanupResources(stmt, rs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Scope> getScopesHavingRole(String roleName) throws ScopeManagementDAOException {
|
|
||||||
Connection conn;
|
|
||||||
PreparedStatement stmt = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
List<Scope> scopes;
|
|
||||||
|
|
||||||
try {
|
|
||||||
conn = this.getConnection();
|
|
||||||
String sql = "SELECT * FROM IDN_OAUTH2_SCOPE WHERE ROLES LIKE ?";
|
|
||||||
stmt = conn.prepareStatement(sql);
|
|
||||||
stmt.setString(1, "%" + roleName + "%");
|
|
||||||
rs = stmt.executeQuery();
|
|
||||||
scopes = this.getScopesFromResultSet(rs);
|
|
||||||
return scopes;
|
|
||||||
} 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 {
|
|
||||||
return ScopeManagementDAOFactory.getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Scope> getScopesFromResultSet(ResultSet rs) throws SQLException {
|
|
||||||
List<Scope> scopes = new ArrayList<>();
|
|
||||||
Scope scope;
|
|
||||||
while (rs.next()) {
|
|
||||||
scope = new Scope();
|
|
||||||
scope.setKey(rs.getString("SCOPE_KEY"));
|
|
||||||
scope.setName(rs.getString("NAME"));
|
|
||||||
scope.setDescription(rs.getString("DESCRIPTION"));
|
|
||||||
scope.setRoles(rs.getString("ROLES"));
|
|
||||||
scopes.add(scope);
|
|
||||||
}
|
|
||||||
return scopes;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.oauth.extensions.handlers.grant;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.oauth.extensions.OAuthExtUtils;
|
||||||
|
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ExtendedPasswordGrantHandler extends org.wso2.carbon.apimgt.keymgt.handlers.ExtendedPasswordGrantHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validateScope(OAuthTokenReqMessageContext tokReqMsgCtx) {
|
||||||
|
return OAuthExtUtils.setScopes(tokReqMsgCtx);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue