use of org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerImpl method getLockStatus.
/**
* {@inheritDoc}
*/
@Override
public FunctionalityLockStatus getLockStatus(String userId, int tenantId, String functionalityIdentifier) throws UserFunctionalityManagementException {
if (!isPerUserFunctionalityLockingEnabled()) {
throw new UnsupportedOperationException("Per-user functionality locking is not enabled.");
}
if (StringUtils.isEmpty(userId) || !isUserIdExists(userId, tenantId)) {
if (log.isDebugEnabled()) {
log.debug("Cannot retrieve user from userId: " + userId);
}
throw buildUserNotFoundError();
}
FunctionalityLockStatus functionalityLockStatus = userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier);
if (functionalityLockStatus == null) {
return FunctionalityLockStatus.UNLOCKED_STATUS;
}
long unlockTime = functionalityLockStatus.getUnlockTime();
if (unlockTime < System.currentTimeMillis()) {
userFunctionalityManagerDAO.deleteMappingForUser(userId, tenantId, functionalityIdentifier);
return FunctionalityLockStatus.UNLOCKED_STATUS;
}
return functionalityLockStatus;
}
use of org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerDAOImpl method getFunctionalityLockStatus.
/**
* {@inheritDoc}
*/
@Override
public FunctionalityLockStatus getFunctionalityLockStatus(String userId, int tenantId, String functionalityIdentifier) throws UserFunctionalityManagementServerException {
FunctionalityLockStatus functionalityLockStatus;
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
functionalityLockStatus = jdbcTemplate.fetchSingleRecord(UserFunctionalityMgtConstants.SqlQueries.GET_FUNCTIONALITY_LOCK_STATUS, ((resultSet, i) -> new FunctionalityLockStatus(resultSet.getBoolean("IS_FUNCTIONALITY_LOCKED"), resultSet.getLong("FUNCTIONALITY_UNLOCK_TIME"), resultSet.getString("FUNCTIONALITY_LOCK_REASON_CODE"), resultSet.getString("FUNCTIONALITY_LOCK_REASON"))), preparedStatement -> {
preparedStatement.setString(1, userId);
preparedStatement.setInt(2, tenantId);
preparedStatement.setString(3, functionalityIdentifier);
});
} catch (DataAccessException e) {
String message = String.format("Error occurred while retrieving functionality lock status from DB for " + "functionality identifier: %s, user Id: %s and tenant Id: %d.", functionalityIdentifier, userId, tenantId);
if (log.isDebugEnabled()) {
log.debug(message, e);
}
throw new UserFunctionalityManagementServerException(message, e);
}
return functionalityLockStatus;
}
use of org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerDAOImplTest method testDeleteFunctionality.
@Test
public void testDeleteFunctionality() {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(false, 0, null, null);
try {
userFunctionalityManagerDAO.addFunctionalityLock("userId", 1, "functionalityIdentifier1", functionalityLockStatus);
userFunctionalityManagerDAO.deleteMappingForUser("userId", 1, "functionalityIdentifier1");
userFunctionalityManagerDAO.deleteMappingForUser("userId", 2, "functionalityIdentifier2");
assertNull(userFunctionalityManagerDAO.getFunctionalityLockStatus("userId", 1, "functionalityIdentifier1"));
} catch (UserFunctionalityManagementServerException e) {
log.error("FunctionalityManagementServer Exception", e);
}
} catch (SQLException e) {
// Mock behaviour. Hence ignored.
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerDAOImplTest method testAddFunctionality.
@Test(dataProvider = "TestFunctionalityData")
public void testAddFunctionality(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked, int functionalityUnlockTime, String functionalityLockReasonCode, String functionalityLockReason) {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
try {
userFunctionalityManagerDAO.addFunctionalityLock(userId, tenantId, functionalityIdentifier, functionalityLockStatus);
} catch (UserFunctionalityManagementServerException e) {
log.error(String.format("Error while adding functionality: %s", functionalityIdentifier), e);
}
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), functionalityLockStatus.getLockStatus());
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getUnlockTime(), functionalityLockStatus.getUnlockTime());
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockStatus.getLockReason());
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReasonCode(), functionalityLockStatus.getLockReasonCode());
} catch (SQLException e) {
// Mock behaviour. Hence ignored.
} catch (UserFunctionalityManagementServerException e) {
assertEquals(e.getMessage(), String.format("Error occurred while adding the functionality: %s, for user: %s, for tenant" + " id: %d, having the parameters, functionality lock status: %b, functionality " + "unlock time: %d, functionality lock reason code: %s, functionality lock reason: " + "%s.", functionalityIdentifier, userId, tenantId, isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason));
}
}
use of org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus in project carbon-identity-framework by wso2.
the class UserFunctionalityManagerDAOImplTest method testGetFunctionalityLockStatus.
@Test(dataProvider = "TestFunctionalityData")
public void testGetFunctionalityLockStatus(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked, int functionalityUnlockTime, String functionalityLockReasonCode, String functionalityLockReason) {
DataSource dataSource = mock(DataSource.class);
TestUtils.mockDataSource(dataSource);
try (Connection connection = TestUtils.getConnection()) {
Connection spyConnection = TestUtils.spyConnection(connection);
when(dataSource.getConnection()).thenReturn(spyConnection);
FunctionalityLockStatus functionalityLockStatus = new FunctionalityLockStatus(isFunctionalityLocked, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
try {
userFunctionalityManagerDAO.addFunctionalityLock(userId, tenantId, functionalityIdentifier, functionalityLockStatus);
} catch (UserFunctionalityManagementServerException e) {
log.error(String.format("Error while adding functionality: %s", functionalityIdentifier), e);
}
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), isFunctionalityLocked);
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getUnlockTime(), functionalityUnlockTime);
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReasonCode(), functionalityLockReasonCode);
Assert.assertEquals(userFunctionalityManagerDAO.getFunctionalityLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockReason);
} catch (SQLException e) {
// Mock behaviour. Hence ignored.
} catch (UserFunctionalityManagementServerException e) {
assertEquals(e.getMessage(), String.format("Error occurred while retrieving functionality lock status from DB for " + "functionality id: %s, user Id: %s and tenant Id: %d.", functionalityIdentifier, userId, tenantId));
}
}
Aggregations