Search in sources :

Example 1 with UserFunctionalityManagementException

use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException 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 UserFunctionalityManagementException

use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerImplTest method testLockFunctionalityForUser.

@Test(dataProvider = "LockFunctionalityForUserData")
public void testLockFunctionalityForUser(String functionalityIdentifier, String userId, int tenantId, boolean expected) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    long functionalityUnlockTime = System.currentTimeMillis() + 300000;
    String functionalityLockReasonCode = "Lock code";
    String functionalityLockReason = "Lock Reason 2";
    try {
        try (Connection connection = TestUtils.getConnection()) {
            Connection spyConnection = TestUtils.spyConnection(connection);
            when(dataSource.getConnection()).thenReturn(spyConnection);
            try {
                mockIsUserStoreManager(userId);
                userFunctionalityManager.lock(userId, tenantId, functionalityIdentifier, functionalityUnlockTime, functionalityLockReasonCode, functionalityLockReason);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), expected);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), functionalityLockReason);
            } catch (UserFunctionalityManagementException e) {
                log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
            } catch (UserStoreException e) {
                log.error("Error while checking userid in userstore", e);
            }
        }
    } catch (SQLException e) {
        log.error("SQL Exception", e);
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with UserFunctionalityManagementException

use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerImplTest method testUnlockFunctionalityForUser.

@Test(dataProvider = "UnlockFunctionalityForUserData")
public void testUnlockFunctionalityForUser(String functionalityIdentifier, String userId, int tenantId, boolean isFunctionalityLocked) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try {
        try (Connection connection = TestUtils.getConnection()) {
            Connection spyConnection = TestUtils.spyConnection(connection);
            when(dataSource.getConnection()).thenReturn(spyConnection);
            try {
                mockIsUserStoreManager(userId);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), isFunctionalityLocked);
                userFunctionalityManager.unlock(userId, tenantId, functionalityIdentifier);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockStatus(), false);
            } catch (UserFunctionalityManagementException e) {
                log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
            } catch (UserStoreException e) {
                log.error("Error while checking userid in userstore", e);
            }
        }
    } catch (SQLException e) {
        log.error("SQL Exception", e);
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with UserFunctionalityManagementException

use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.

the class UserFunctionalityPropertyDAOImplTest method testDeleteAllFunctionalityLockProperties.

@Test(dataProvider = "TestFunctionalityLockPropertiesData")
public void testDeleteAllFunctionalityLockProperties(String userId, int tenantId, String functionalityIdentifier, Map<String, String> properties) {
    DataSource dataSource = mock(DataSource.class);
    mockDataSource(dataSource);
    try (Connection connection = getConnection()) {
        Connection spyConnection = spyConnection(connection);
        when(dataSource.getConnection()).thenReturn(spyConnection);
        mockUser(userId);
        userFunctionalityManager.setProperties(userId, tenantId, functionalityIdentifier, properties);
        userFunctionalityPropertyDAO.deleteAllPropertiesForUser(userId, tenantId, functionalityIdentifier);
        Map<String, String> functionalityLockProperties = userFunctionalityPropertyDAO.getAllProperties(userId, tenantId, functionalityIdentifier);
        assertTrue(functionalityLockProperties.isEmpty());
    } catch (SQLException | UserFunctionalityManagementException | UserStoreException e) {
    // Mock behaviour. Hence ignored.
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TestUtils.spyConnection(org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.spyConnection) TestUtils.getConnection(org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.getConnection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Matchers.anyString(org.mockito.Matchers.anyString) DataSource(javax.sql.DataSource) TestUtils.mockDataSource(org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.mockDataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with UserFunctionalityManagementException

use of org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerImplTest method testGetFunctionalityLockReasonForUser.

@Test(dataProvider = "FunctionalityLockedReasonData")
public void testGetFunctionalityLockReasonForUser(String functionalityIdentifier, String userId, int tenantId, String expected) {
    DataSource dataSource = mock(DataSource.class);
    TestUtils.mockDataSource(dataSource);
    try {
        try (Connection connection = TestUtils.getConnection()) {
            Connection spyConnection = TestUtils.spyConnection(connection);
            when(dataSource.getConnection()).thenReturn(spyConnection);
            try {
                mockIsUserStoreManager(userId);
                assertEquals(userFunctionalityManager.getLockStatus(userId, tenantId, functionalityIdentifier).getLockReason(), expected);
            } catch (UserFunctionalityManagementException e) {
                log.error(String.format("Error while selecting functionality: %s", functionalityIdentifier), e);
            } catch (UserStoreException e) {
                log.error("Error while checking userid in userstore", e);
            }
        }
    } catch (SQLException e) {
        log.error("SQL Exception", e);
    }
}
Also used : UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataSource(javax.sql.DataSource) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

UserFunctionalityManagementException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException)12 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 DataSource (javax.sql.DataSource)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Test (org.testng.annotations.Test)7 UserStoreException (org.wso2.carbon.user.api.UserStoreException)7 UserFunctionalityManager (org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManager)5 HashMap (java.util.HashMap)2 FunctionalityLockStatus (org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus)2 Matchers.anyString (org.mockito.Matchers.anyString)1 Property (org.wso2.carbon.identity.application.common.model.Property)1 UserFunctionalityManagementClientException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementClientException)1 UserFunctionalityManagementServerException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException)1 TestUtils.getConnection (org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.getConnection)1 TestUtils.mockDataSource (org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.mockDataSource)1 TestUtils.spyConnection (org.wso2.carbon.identity.user.functionality.mgt.util.TestUtils.spyConnection)1