Search in sources :

Example 1 with FunctionalityLockStatus

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;
}
Also used : FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus)

Example 2 with 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;
}
Also used : UserFunctionalityMgtConstants(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityMgtConstants) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) HashMap(java.util.HashMap) UserFunctionalityManager(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManager) UUID(java.util.UUID) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) Log(org.apache.commons.logging.Log) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LogFactory(org.apache.commons.logging.LogFactory) UserFunctionalityManagerImpl(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManagerImpl) UserFunctionalityManagerDAO(org.wso2.carbon.identity.user.functionality.mgt.dao.UserFunctionalityManagerDAO) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 3 with 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.
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with FunctionalityLockStatus

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));
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with FunctionalityLockStatus

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));
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

FunctionalityLockStatus (org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus)12 UserFunctionalityManagementServerException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException)9 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 DataSource (javax.sql.DataSource)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 UserFunctionalityManager (org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManager)4 UserFunctionalityManagementException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException)4 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)2 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)2 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 DataProvider (org.testng.annotations.DataProvider)1 JdbcUtils (org.wso2.carbon.identity.core.util.JdbcUtils)1 UserFunctionalityManagerImpl (org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManagerImpl)1