Search in sources :

Example 6 with UserStorePersistanceDTO

use of org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO in project carbon-identity-framework by wso2.

the class DatabaseBasedUserStoreDAOImpl method doGetUserStore.

@Override
protected UserStorePersistanceDTO doGetUserStore(String domainName) throws IdentityUserStoreMgtException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    InputStream scriptBinaryStream = null;
    InputStream clonedStream = null;
    String userStoreProperties = null;
    UserStorePersistanceDTO userStorePersistanceDTO = new UserStorePersistanceDTO();
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement prepStmt = connection.prepareStatement(UserStoreMgtDBQueries.GET_USERSTORE_PROPERTIES)) {
        prepStmt.setString(1, domainName);
        prepStmt.setInt(2, tenantId);
        prepStmt.setString(3, USERSTORE);
        try (ResultSet rSet = prepStmt.executeQuery()) {
            if (rSet.next()) {
                RealmConfiguration realmConfiguration = null;
                scriptBinaryStream = rSet.getBinaryStream(1);
                clonedStream = rSet.getBinaryStream(1);
                if (scriptBinaryStream != null) {
                    realmConfiguration = getRealmConfiguration(domainName, scriptBinaryStream);
                }
                if (clonedStream != null) {
                    userStoreProperties = IOUtils.toString(clonedStream);
                }
                userStorePersistanceDTO.setUserStoreProperties(userStoreProperties);
                if (realmConfiguration != null) {
                    userStorePersistanceDTO.setUserStoreDTO(getUserStoreDTO(realmConfiguration));
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("No user store properties found for domain: " + domainName + " in tenant: " + tenantId);
                }
            }
        } catch (IOException | UserStoreException | XMLStreamException e) {
            throw new IdentityUserStoreMgtException("Error occured while getting user store properties for " + "domain:" + domainName + " in tenant:" + tenantId, e);
        } finally {
            try {
                if (scriptBinaryStream != null) {
                    scriptBinaryStream.close();
                }
                if (clonedStream != null) {
                    clonedStream.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new IdentityUserStoreMgtException("Error occured while loading user stores.", e);
            }
        }
    } catch (SQLException e) {
        throw new IdentityUserStoreMgtException("Could not read the user store properties for the domain:" + domainName + " in tenant:" + tenantId, e);
    }
    return userStorePersistanceDTO;
}
Also used : IdentityUserStoreMgtException(org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreMgtException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) XMLStreamException(javax.xml.stream.XMLStreamException) ResultSet(java.sql.ResultSet) UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserStorePersistanceDTO(org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO)

Example 7 with UserStorePersistanceDTO

use of org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO in project carbon-identity-framework by wso2.

the class DatabaseBasedUserStoreDAOImpl method doGetAllUserStores.

@Override
protected UserStorePersistanceDTO[] doGetAllUserStores() throws IdentityUserStoreMgtException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    List<UserStorePersistanceDTO> userStorePersistanceDTOs = new ArrayList<>();
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement prepStmt = connection.prepareStatement(GET_ALL_USERSTORE_PROPERTIES)) {
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, USERSTORE);
        try (ResultSet rSet = prepStmt.executeQuery()) {
            while (rSet.next()) {
                String identifier = rSet.getString(1);
                InputStream scriptBinaryStream = null;
                InputStream clonedStream = null;
                String userStorePropertyValues = null;
                try {
                    scriptBinaryStream = rSet.getBinaryStream(2);
                    clonedStream = rSet.getBinaryStream(2);
                    RealmConfiguration realmConfiguration = null;
                    if (scriptBinaryStream != null) {
                        realmConfiguration = getRealmConfiguration(identifier, scriptBinaryStream);
                    }
                    if (clonedStream != null) {
                        userStorePropertyValues = IOUtils.toString(clonedStream);
                    }
                    getUserStorePersistanceDTOs(userStorePersistanceDTOs, realmConfiguration, userStorePropertyValues);
                } finally {
                    if (scriptBinaryStream != null) {
                        scriptBinaryStream.close();
                    }
                    if (clonedStream != null) {
                        clonedStream.close();
                    }
                }
            }
        } catch (org.wso2.carbon.user.api.UserStoreException e) {
            throw new IdentityUserStoreMgtException("Error occured while listing user stores in tenant: " + tenantId, e);
        } catch (XMLStreamException | IOException e) {
            throw new IdentityUserStoreMgtException("Could not read the user store properties in tenant:" + tenantId, e);
        }
    } catch (SQLException e) {
        throw new IdentityUserStoreMgtException("Could not read the user store properties in tenant:" + tenantId, e);
    }
    return userStorePersistanceDTOs.toArray(new UserStorePersistanceDTO[userStorePersistanceDTOs.size()]);
}
Also used : IdentityUserStoreMgtException(org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreMgtException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) RealmConfiguration(org.wso2.carbon.user.api.RealmConfiguration) XMLStreamException(javax.xml.stream.XMLStreamException) ResultSet(java.sql.ResultSet) UserStorePersistanceDTO(org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO)

Example 8 with UserStorePersistanceDTO

use of org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO in project carbon-identity-framework by wso2.

the class DatabaseBasedUserStoreDAOImpl method doUpdateUserStoreDomainName.

@Override
protected void doUpdateUserStoreDomainName(String domainName, UserStorePersistanceDTO userStorePersistanceDTO) throws IdentityUserStoreMgtException {
    try {
        String newDomainName = userStorePersistanceDTO.getUserStoreDTO().getDomainId();
        triggerListnersOnUserStorePreUpdate(domainName, newDomainName);
        updateUserStoreProperties(domainName, userStorePersistanceDTO);
        removeRealmFromSecondaryUserStoreManager(domainName);
        addRealmToSecondaryUserStoreManager(userStorePersistanceDTO);
        triggerListenersOnUserStorePostUpdate(domainName, newDomainName);
    } catch (UserStoreClientException e) {
        throw buildIdentityUserStoreClientException("Userstore " + domainName + " cannot be updated.", e);
    } catch (UserStoreException | XMLStreamException e) {
        throw new IdentityUserStoreMgtException("Error occured while updating the userstore.", e);
    }
}
Also used : IdentityUserStoreMgtException(org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreMgtException) XMLStreamException(javax.xml.stream.XMLStreamException) SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException(org.wso2.carbon.identity.user.store.configuration.utils.SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException) UserStoreClientException(org.wso2.carbon.user.api.UserStoreClientException) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 9 with UserStorePersistanceDTO

use of org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO in project carbon-identity-framework by wso2.

the class DatabaseBasedUserStoreDAOImpl method updateUserStoreProperties.

private void updateUserStoreProperties(String domainName, UserStorePersistanceDTO userStorePersistanceDTO) throws IdentityUserStoreMgtException {
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    String msg = "Error occured while updating user store properties for the domain:" + domainName + "in the " + "tenant:" + tenantId;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection()) {
        try (PreparedStatement pst = connection.prepareStatement(UserStoreMgtDBQueries.UPDATE_USERSTORE_PROPERTIES)) {
            setBlobValue(userStorePersistanceDTO.getUserStoreProperties(), pst, 1);
            pst.setString(2, userStorePersistanceDTO.getUserStoreDTO().getDomainId());
            pst.setString(3, domainName);
            pst.setInt(4, tenantId);
            pst.setString(5, USERSTORE);
            pst.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
            if (log.isDebugEnabled()) {
                log.debug("The userstore domain:" + domainName + "updated for the tenant:" + tenantId);
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new IdentityUserStoreMgtException(msg, e);
        } catch (IOException ex) {
            throw new IdentityUserStoreMgtException(msg, ex);
        }
    } catch (SQLException e) {
        throw new IdentityUserStoreMgtException(msg);
    }
}
Also used : IdentityUserStoreMgtException(org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreMgtException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 10 with UserStorePersistanceDTO

use of org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO in project carbon-identity-framework by wso2.

the class FileBasedUserStoreDAOImpl method doAddUserStore.

@Override
protected void doAddUserStore(UserStorePersistanceDTO userStorePersistanceDTO) throws IdentityUserStoreMgtException {
    String domainName = userStorePersistanceDTO.getUserStoreDTO().getDomainId();
    try {
        // Run pre user-store add listeners.
        triggerListenersOnUserStorePreAdd(domainName);
        boolean validDomain = isValidDomainToAdd(domainName);
        validateForFederatedDomain(domainName);
        if (validDomain) {
            Path userStoreConfigFile = getUserStoreConfigurationFile(userStorePersistanceDTO.getUserStoreDTO());
            if (Files.exists(userStoreConfigFile)) {
                throw buildException(userStorePersistanceDTO.getUserStoreDTO().getDomainId(), false);
            }
            writeToUserStoreConfigurationFile(userStoreConfigFile, userStorePersistanceDTO.getUserStoreDTO(), false, false, domainName);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("The user store domain: " + domainName + "is not a valid domain name.");
            }
        }
    } catch (UserStoreException e) {
        throw new IdentityUserStoreClientException("Error occurred while adding the user store with the domain: " + domainName, e);
    }
}
Also used : Path(java.nio.file.Path) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityUserStoreClientException(org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreClientException) SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException(org.wso2.carbon.identity.user.store.configuration.utils.SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException)

Aggregations

UserStorePersistanceDTO (org.wso2.carbon.identity.user.store.configuration.dto.UserStorePersistanceDTO)9 UserStoreException (org.wso2.carbon.user.api.UserStoreException)9 IdentityUserStoreMgtException (org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreMgtException)8 XMLStreamException (javax.xml.stream.XMLStreamException)5 IOException (java.io.IOException)4 SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException (org.wso2.carbon.identity.user.store.configuration.utils.SecondaryUserStoreConfigurationUtil.buildIdentityUserStoreClientException)4 RealmConfiguration (org.wso2.carbon.user.api.RealmConfiguration)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 IdentityUserStoreClientException (org.wso2.carbon.identity.user.store.configuration.utils.IdentityUserStoreClientException)3 ResultSet (java.sql.ResultSet)2 UserStoreDTO (org.wso2.carbon.identity.user.store.configuration.dto.UserStoreDTO)2 UserStoreClientException (org.wso2.carbon.user.api.UserStoreClientException)2 MaskedProperty (org.wso2.carbon.identity.user.store.configuration.beans.MaskedProperty)1 UserRealm (org.wso2.carbon.user.core.UserRealm)1