HookServices.groovy 3.79 KB

import groovy.transform.Field

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import org.moqui.util.RestClient
import org.moqui.util.ObjectUtilities
import org.moqui.context.ExecutionContext
import org.moqui.entity.EntityCondition
import org.moqui.entity.EntityFind
import org.moqui.entity.EntityList
import org.moqui.entity.EntityValue

import org.moqui.keycloak.HookSynchronization
@Field Logger logger = LoggerFactory.getLogger(getClass().getName())
//'HookServices')

HookSynchronization getHookSync() {
    HookSynchronization hookSync = ec.transaction.getActiveSynchronization(getClass().getName())
    if (hookSync == null) {
        hookSync = new HookSynchronization(ec)
        ec.transaction.putAndEnlistActiveSynchronization(getClass().getName(), hookSync)
    }
    return hookSync
}

Map<String, Object> handleEntityUpdate() {
    logger.info("handleEntityUpdate: ${context.entityName}[${context.value}]")
    logger.info("context: ${context}")
    ExecutionContext ec = context.ec
    Map<String, Object> contextRoot = ec.getContextRoot()

    HookSynchronization hookSync = getHookSync()

    List<Map<String, Object>> queue = [[entityName: context.entityName, value: context.value]]

    while (!queue.isEmpty()) {
        Map<String, Object> entry = queue.remove(0)
        logger.info("processing entry: ${entry}")
        String entityName = entry.entityName
        Object value = entry.value
        String keyName
        switch (entityName) {
            case 'Party':
                EntityList userAccounts = ec.entity.find('UserAccount').condition('partyId', value).list()
                for (EntityValue userAccount: userAccounts) {
                    queue.add([entityName: 'UserAccount', value: userAccount.userId])
                }
                continue
            case 'ContactMech':
                EntityList partyContactMechs = ec.entity.find('PartyContactMech').condition('contactMechId', value).list()
                for (EntityValue partyContactMech: partyContactMechs) {
                    queue.add([entityName: 'Party', value: partyContactMech.partyId])
                }
                continue
            case 'UserAccount':
                keyName = 'userId'
                break
            case 'UserGroup':
                keyName = 'userGroupId'
                break
            case 'UserGroupMember':
                keyName = 'userGroupId'
                break
            case 'UserPermission':
                keyName = 'userPermissionId'
                break
            case 'PartyClassification':
                keyName = 'partyClassificationId'
                break
            case 'RoleType':
                keyName = 'roleTypeId'
                break
        }
        hookSync.add(entityName, keyName, value, [:])
        //ec.getService().special().name("keycloak.KeycloakServices.send#${entityName}").parameter(keyName, value).registerOnCommit()
    }
}

Map<String, Object> handleUpdatePasswordInternal() {
    String userId = ec.context.userId
    String newPassword = ec.context.newPassword
    Boolean requirePasswordChange = ec.context.requirePasswordChange
    ec.getLogger().info("Registered synchronization for update#PasswordInternal: [${userId}]")
    HookSynchronization hookSync = getHookSync()
    hookSync.add('UserAccount', 'userId', userId, [
        newPassword: newPassword,
        requirePasswordChange: requirePasswordChange,
    ])
}

Map<String, Object> processUpdates() {
    List<Map<String, Object>> updates = context.updates

    for (Map<String, Object> update: updates) {
        Map<String, Object> parameters = [:]
        parameters[update.keyName] = update.keyValue
        parameters.putAll(update.extraParameters)
        ec.getService().sync().name("keycloak.KeycloakServices.send#${update.entityName}").parameters(parameters).call()
    }
    return [:]
}