Compare commits

Invalid templates have been ignored

1 invalid template(s) found pull_request_template.md: frontmatter must start with a separator line

..

10 Commits

@ -62,17 +62,18 @@ public class APIApplicationManagerExtensionDataHolder {
public void setRealmService(RealmService realmService) {
this.realmService = realmService;
this.setTenantManager(realmService);
setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
}
private void setTenantManager(RealmService realmService) {
if (realmService == null) {
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
private void setTenantManager(TenantManager tenantManager) {
this.tenantManager = tenantManager;
}
public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager;
}

@ -75,17 +75,18 @@ public class APIPublisherDataHolder {
public void setRealmService(RealmService realmService) {
this.realmService = realmService;
this.setTenantManager(realmService);
setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
}
private void setTenantManager(RealmService realmService) {
if (realmService == null) {
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
private void setTenantManager(TenantManager tenantManager) {
this.tenantManager = tenantManager;
}
public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager;
}

@ -151,20 +151,20 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
}
@DELETE
public Response removeCertificate(@QueryParam("serialNumber") String serialNumber) {
RequestValidationUtil.validateSerialNumber(serialNumber);
public Response removeCertificate(@QueryParam("certificateId") String certificateId) {
RequestValidationUtil.validateCertificateId(certificateId);
CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService();
try {
boolean status = certificateService.removeCertificate(serialNumber);
boolean status = certificateService.removeCertificate(certificateId);
if (!status) {
return Response.status(Response.Status.NOT_FOUND).entity(
"No certificate is found with the given " +
"serial number '" + serialNumber + "'").build();
"certificate id '" + certificateId + "'").build();
} else {
return Response.status(Response.Status.OK).entity(
"Certificate that carries the serial number '" +
serialNumber + "' has been removed").build();
"Certificate that carries the certificate id '" +
certificateId + "' has been removed").build();
}
} catch (CertificateManagementException e) {
String msg = "Error occurred while converting PEM file to X509Certificate";

@ -30,6 +30,14 @@ public class RequestValidationUtil {
}
}
public static void validateCertificateId(String certificateId) {
if (certificateId == null || certificateId.isEmpty()) {
throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(
"Certificate Id cannot be null or empty").build());
}
}
public static void validatePaginationInfo(int offset, int limit) {
if (offset < 0) {
throw new InputValidationException(

@ -95,10 +95,10 @@ public interface CertificateDAO {
/**
* Delete a certificate identified by a serial number()
*
* @param serialNumber serial number
* @param certificateId number
* @return whether the certificate was removed or not.
*/
boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException;
boolean removeCertificate(String certificateId) throws CertificateManagementDAOException;
List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;

@ -237,7 +237,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
Connection conn = this.getConnection();
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME"
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME"
+ " FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -247,6 +247,8 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -263,7 +265,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
}
@Override
public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException {
public boolean removeCertificate(String certificateId) throws CertificateManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
@ -271,15 +273,15 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
try {
conn = this.getConnection();
String query =
"DELETE FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?" +
"DELETE FROM DM_DEVICE_CERTIFICATE WHERE ID = ?" +
" AND TENANT_ID = ? ";
stmt = conn.prepareStatement(query);
stmt.setString(1, serialNumber);
stmt.setString(1, certificateId);
stmt.setInt(2, tenantId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
String msg = "Unable to get the read the certificate with serial" + serialNumber;
String msg = "Unable to get the read the certificate with certificate id" + certificateId;
log.error(msg, e);
throw new CertificateManagementDAOException(msg, e);
} finally {

@ -47,6 +47,27 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection();
}
private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException {
int certificateCount = 0;
try {
Connection conn = this.getConnection();
String sql =
"SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
}
return certificateCount;
}
@Override
public PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementDAOException {
@ -58,7 +79,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
Connection conn = this.getConnection();
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM "
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM "
+ "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -71,6 +92,8 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -79,7 +102,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount);
paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);

@ -53,7 +53,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
Connection conn = this.getConnection();
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM "
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM "
+ "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount);
paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
@ -88,4 +90,26 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection();
}
private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException {
int certificateCount = 0;
try {
Connection conn = this.getConnection();
String sql =
"SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
}
return certificateCount;
}
}

@ -53,7 +53,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
Connection conn = this.getConnection();
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM "
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM "
+ "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount);
paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
@ -88,4 +90,26 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection();
}
private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException {
int certificateCount = 0;
try {
Connection conn = this.getConnection();
String sql =
"SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
}
return certificateCount;
}
}

@ -53,7 +53,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
Connection conn = this.getConnection();
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM "
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM "
+ "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
}
paginationResult = new PaginationResult();
paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount);
paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
@ -88,4 +90,26 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection();
}
private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException {
int certificateCount = 0;
try {
Connection conn = this.getConnection();
String sql =
"SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
}
}
}
} catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
}
return certificateCount;
}
}

@ -32,6 +32,12 @@ public class CertificateResponse {
@ApiModelProperty(name = "serialNumber", value = "It is the unique ID that is used to identify a certificate", required = true)
String serialNumber;
@ApiModelProperty(name = "deviceIdentifier", value = "It is use to identify a certificate list", required = true)
String deviceIdentifier;
@ApiModelProperty(name = "certificateId", value = "It is the unique ID that is used to identify a certificate", required = true)
String certificateId;
@ApiModelProperty(name = "tenantId", value = "The ID of the tenant who adds the certificate", required = true)
int tenantId;
@ -44,8 +50,8 @@ public class CertificateResponse {
@ApiModelProperty(name = "notBefore", value = "The date from when the certificate is valid", required = true)
long notBefore;
@ApiModelProperty(name = "certificateserial", value = "The serial number of the certificate", required = true)
BigInteger certificateserial;
@ApiModelProperty(name = "certificateSerial", value = "The serial number of the certificate", required = true)
BigInteger certificateSerial;
@ApiModelProperty(name = "issuer", value = "The identity of the authority that signs the SSL certificate", required = true)
String issuer;
@ -83,12 +89,12 @@ public class CertificateResponse {
this.notBefore = notBefore;
}
public BigInteger getCertificateserial() {
return certificateserial;
public BigInteger getCertificateSerial() {
return certificateSerial;
}
public void setCertificateserial(BigInteger certificateserial) {
this.certificateserial = certificateserial;
public void setCertificateSerial(BigInteger certificateSerial) {
this.certificateSerial = certificateSerial;
}
public String getIssuer() {
@ -146,4 +152,20 @@ public class CertificateResponse {
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public String getCertificateId() {
return certificateId;
}
public void setCertificateId(String certificateId) {
this.certificateId = certificateId;
}
}

@ -130,7 +130,7 @@ public class CertificateGenerator {
X509Certificate certificate = (X509Certificate) x509Certificate;
certificateResponse.setNotAfter(certificate.getNotAfter().getTime());
certificateResponse.setNotBefore(certificate.getNotBefore().getTime());
certificateResponse.setCertificateserial(certificate.getSerialNumber());
certificateResponse.setCertificateSerial(certificate.getSerialNumber());
certificateResponse.setIssuer(certificate.getIssuerDN().getName());
certificateResponse.setSubject(certificate.getSubjectDN().getName());
certificateResponse.setCertificateVersion(certificate.getVersion());

@ -73,7 +73,7 @@ public interface CertificateManagementService {
PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementException;
boolean removeCertificate(String serialNumber) throws CertificateManagementException;
boolean removeCertificate(String certificateId) throws CertificateManagementException;
List<CertificateResponse> getCertificates() throws CertificateManagementException;

@ -174,20 +174,20 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
}
@Override
public boolean removeCertificate(String serialNumber) throws CertificateManagementException {
public boolean removeCertificate(String certificateId) throws CertificateManagementException {
try {
CertificateManagementDAOFactory.beginTransaction();
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
boolean status = certificateDAO.removeCertificate(serialNumber);
boolean status = certificateDAO.removeCertificate(certificateId);
CertificateManagementDAOFactory.commitTransaction();
return status;
} catch (TransactionManagementException e) {
String msg = "Error occurred while removing certificate carrying serial number '" + serialNumber + "'";
String msg = "Error occurred while removing certificate carrying certificate id '" + certificateId + "'";
log.error(msg, e);
throw new CertificateManagementException(msg, e);
} catch (CertificateManagementDAOException e) {
CertificateManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while removing the certificate carrying serial number '" + serialNumber +
String msg = "Error occurred while removing the certificate carrying certificate id '" + certificateId +
"' from the certificate repository";
log.error(msg, e);
throw new CertificateManagementException(msg, e);

@ -210,7 +210,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
X509Certificate x509Certificate = managementService.generateX509Certificate();
CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
Assert.assertNotNull(certificateResponse);
Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateserial());
Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateSerial());
}
@Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination")

@ -29,6 +29,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistExc
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
@ -48,6 +49,8 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestV
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import io.entgra.device.mgt.core.policy.mgt.common.PolicyAdministratorPoint;
import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@ -56,6 +59,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GroupManagementServiceImpl implements GroupManagementService {
@ -109,8 +113,18 @@ public class GroupManagementServiceImpl implements GroupManagementService {
request.setGroupName(name);
request.setOwner(owner);
request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
UserRealm realmService = DeviceMgtAPIUtils.getRealmService().getTenantUserRealm(tenantId);
String[] roles = realmService.getUserStoreManager().getRoleListOfUser(currentUser);
boolean hasAdminRole = Arrays.asList(roles).contains(DEFAULT_ADMIN_ROLE);
PaginationResult deviceGroupsResult;
if (hasAdminRole) {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
} else{
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
}
DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
@ -119,6 +133,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
} catch (UserStoreException e) {
String msg = "Error occurred while getting user realm.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

@ -31,7 +31,11 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceGroupList;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.admin.GroupManagementAdminService;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@ -40,6 +44,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Arrays;
public class GroupManagementAdminServiceImpl implements GroupManagementAdminService {
@ -94,13 +99,25 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
@DefaultValue("5") @QueryParam("limit") int limit) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
request.setGroupName(name);
request.setOwner(owner);
request.setStatus(status);
request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
UserRealm realmService = DeviceMgtAPIUtils.getRealmService().getTenantUserRealm(tenantId);
String[] roles = realmService.getUserStoreManager().getRoleListOfUser(currentUser);
boolean isAdmin = DEFAULT_ADMIN_ROLE.equals(currentUser);
boolean hasAdminRole = Arrays.asList(roles).contains(DEFAULT_ADMIN_ROLE);
PaginationResult deviceGroupsResult;
if (StringUtils.isBlank(currentUser) || isAdmin || hasAdminRole) {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
} else {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
}
DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
@ -109,6 +126,10 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
} catch (UserStoreException e) {
String msg = "Error occurred while getting user realm.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}

@ -480,6 +480,7 @@ public class OperationManagerImpl implements OperationManager {
int failAttempts = 0;
while (true) {
try {
OperationManagementDAOFactory.beginTransaction();
operationMappingDAO.updateOperationMapping(operation.getId(), device.getEnrolmentInfo().getId(),
io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
OperationManagementDAOFactory.commitTransaction();
@ -502,6 +503,11 @@ public class OperationManagerImpl implements OperationManager {
} catch (InterruptedException ignore) {
break;
}
} catch (TransactionManagementException ex) {
log.error("Error occurred while initiating the transaction", ex);
break;
} finally {
OperationManagementDAOFactory.closeConnection();
}
}
} catch (Exception e) {

@ -74,17 +74,18 @@ public class JWTClientExtensionDataHolder {
public void setRealmService(RealmService realmService) {
this.realmService = realmService;
this.setTenantManager(realmService);
setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
}
private void setTenantManager(RealmService realmService) {
if (realmService == null) {
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
private void setTenantManager(TenantManager tenantManager) {
this.tenantManager = tenantManager;
}
public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager;
}
}

Loading…
Cancel
Save