|
|
@ -17,7 +17,9 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
package org.wso2.carbon.device.application.mgt.core.dao.impl.visibility;
|
|
|
|
package org.wso2.carbon.device.application.mgt.core.dao.impl.visibility;
|
|
|
|
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.application.mgt.common.Visibility;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
|
|
|
|
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
|
|
|
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
|
|
|
|
import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO;
|
|
|
|
import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO;
|
|
|
|
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
|
|
|
|
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
|
|
|
@ -36,121 +38,106 @@ import java.util.List;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements VisibilityDAO {
|
|
|
|
public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements VisibilityDAO {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private static final Log log = LogFactory.getLog(GenericVisibilityDAOImpl.class);
|
|
|
|
public int getVisibilityID(Visibility.Type visibilityType) throws VisibilityManagementDAOException {
|
|
|
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
|
|
|
|
String sql = "SELECT ID FROM APPM_RESOURCE_TYPE WHERE NAME = ?";
|
|
|
|
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
|
|
|
|
stmt.setString(1, visibilityType.toString().toUpperCase());
|
|
|
|
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
|
|
|
|
if (resultSet.next()) {
|
|
|
|
|
|
|
|
return resultSet.getInt("ID");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
|
|
|
|
"for the visibility management of applications", e);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred when trying to get the ID of the" +
|
|
|
|
|
|
|
|
" visibility type - " + visibilityType.toString(), e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
Util.cleanupResources(stmt, resultSet);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void add(int applicationID, int visibilityTypeID, List<String> allowedList)
|
|
|
|
public void addUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles, int applicationId, int tenantId) throws
|
|
|
|
throws VisibilityManagementDAOException {
|
|
|
|
VisibilityManagementDAOException {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Request received in DAO Layer to add unrestricted roles");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection conn;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
try {
|
|
|
|
ResultSet rs = null;
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
int index = 0;
|
|
|
|
String sql = "INSERT INTO APPM_VISIBILITY (VALUE, RESOURCE_TYPE_ID, APPLICATION_ID) VALUES (?, ?, ?)";
|
|
|
|
String sql = "INSERT INTO AP_UNRESTRICTED_ROLES (ROLE, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
try{
|
|
|
|
if (allowedList == null) {
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
stmt.setString(1, null);
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
stmt.setInt(2, visibilityTypeID);
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
stmt.setInt(3, applicationID);
|
|
|
|
for (UnrestrictedRole role : unrestrictedRoles) {
|
|
|
|
stmt.execute();
|
|
|
|
stmt.setString(++index, role.getRole());
|
|
|
|
} else {
|
|
|
|
stmt.setInt(++index, tenantId);
|
|
|
|
for (String allowed : allowedList) {
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
stmt.setString(1, allowed);
|
|
|
|
|
|
|
|
stmt.setInt(2, visibilityTypeID);
|
|
|
|
|
|
|
|
stmt.setInt(3, applicationID);
|
|
|
|
|
|
|
|
stmt.addBatch();
|
|
|
|
stmt.addBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stmt.executeBatch();
|
|
|
|
stmt.executeBatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
"for adding the visibility mapping for the application ID - " + applicationID, e);
|
|
|
|
}catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding the visibility mapping " +
|
|
|
|
|
|
|
|
"for the application ID - " + applicationID, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
Util.cleanupResources(stmt, null);
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void delete(int applicationId) throws VisibilityManagementDAOException {
|
|
|
|
public List<UnrestrictedRole> getUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
try {
|
|
|
|
log.debug("Request received in DAO Layer to get unrestricted roles");
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
|
|
|
|
String sql = "DELETE FROM APPM_VISIBILITY WHERE APPLICATION_ID = ?";
|
|
|
|
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
|
|
|
|
stmt.setInt(1, applicationId);
|
|
|
|
|
|
|
|
stmt.execute();
|
|
|
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
|
|
|
|
"for deleting the visibility mapping for the application ID - " + applicationId, e);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while deleting the visibility mapping " +
|
|
|
|
|
|
|
|
"for the application ID - " + applicationId, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
Util.cleanupResources(stmt, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection conn;
|
|
|
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>();
|
|
|
|
|
|
|
|
UnrestrictedRole unrestrictedRole = null;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
String sql = "SELECT ID, ROLE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = ? AND TENANT_ID = ?;";
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
|
|
|
|
stmt.setInt(++index, tenantId);
|
|
|
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (rs.next()){
|
|
|
|
|
|
|
|
unrestrictedRole = new UnrestrictedRole();
|
|
|
|
|
|
|
|
unrestrictedRole.setId(rs.getInt("ID"));
|
|
|
|
|
|
|
|
unrestrictedRole.setRole(rs.getString("ROLE"));
|
|
|
|
|
|
|
|
unrestrictedRoles.add(unrestrictedRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return unrestrictedRoles;
|
|
|
|
|
|
|
|
|
|
|
|
public Visibility get(int applicationId) throws VisibilityManagementDAOException {
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
}catch (SQLException e) {
|
|
|
|
final String visibilityTypeColumn = "VISIBILITY_TYPE";
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
final String allowedValColumn = "ALLOWED_VAL";
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
Connection connection = getDBConnection();
|
|
|
|
|
|
|
|
String sql = "SELECT APPM_VISIBILITY.VALUE as " + allowedValColumn + ", APPM_RESOURCE_TYPE.NAME AS " +
|
|
|
|
|
|
|
|
visibilityTypeColumn + " FROM APPM_VISIBILITY JOIN APPM_RESOURCE_TYPE " +
|
|
|
|
|
|
|
|
"ON APPM_VISIBILITY.RESOURCE_TYPE_ID = APPM_RESOURCE_TYPE.ID " +
|
|
|
|
|
|
|
|
"WHERE APPM_VISIBILITY.APPLICATION_ID = ?";
|
|
|
|
|
|
|
|
stmt = connection.prepareStatement(sql);
|
|
|
|
|
|
|
|
stmt.setInt(1, applicationId);
|
|
|
|
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
|
|
|
|
Visibility visibility = new Visibility();
|
|
|
|
|
|
|
|
List<String> allowedVal = new ArrayList<>();
|
|
|
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
|
|
|
if (visibility.getType() == null) {
|
|
|
|
|
|
|
|
visibility.setType(Visibility.Type.valueOf(resultSet.getString(visibilityTypeColumn)));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String val = resultSet.getString(allowedValColumn);
|
|
|
|
|
|
|
|
if (val != null) {
|
|
|
|
|
|
|
|
allowedVal.add(val);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void deleteUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles, int applicationId, int tenantId) throws VisibilityManagementDAOException {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Request received in DAO Layer to delete unrestricted roles");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!allowedVal.isEmpty()) {
|
|
|
|
Connection conn;
|
|
|
|
visibility.setAllowedList(allowedVal);
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
String sql = "DELETE FROM AP_UNRESTRICTED_ROLES WHERE AP_APP_ID = 1 AND ROLE = 'role1' AND TENANT_ID = -1234;";
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
conn = this.getDBConnection();
|
|
|
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (UnrestrictedRole role : unrestrictedRoles) {
|
|
|
|
|
|
|
|
stmt.setInt(++index, applicationId);
|
|
|
|
|
|
|
|
stmt.setString(++index, role.getRole());
|
|
|
|
|
|
|
|
stmt.setInt(++index, role.getTenantId());
|
|
|
|
|
|
|
|
stmt.addBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return visibility;
|
|
|
|
stmt.executeBatch();
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the connection " +
|
|
|
|
}catch (DBConnectionException e) {
|
|
|
|
"for getting the visibility mapping for the application ID - " + applicationId, e);
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while obtaining the DB connection when adding roles", e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}catch (SQLException e) {
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while getting the visibility mapping " +
|
|
|
|
throw new VisibilityManagementDAOException("Error occurred while adding unrestricted roles", e);
|
|
|
|
"for the application ID - " + applicationId, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
Util.cleanupResources(stmt, resultSet);
|
|
|
|
Util.cleanupResources(stmt, rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|