Search in sources :

Example 1 with IdentityEventMessageContext

use of org.wso2.carbon.identity.event.bean.IdentityEventMessageContext in project carbon-identity-framework by wso2.

the class ConsentDeletionUserEventHandler method handleEvent.

/**
 * Delete consents issued against a particular user when a user is deleted.
 *
 * @param event Post User Delete event.
 * @throws IdentityEventException IdentityEventException.
 */
@Override
public void handleEvent(Event event) throws IdentityEventException {
    IdentityEventMessageContext eventContext = new IdentityEventMessageContext(event);
    if (!isEnabled(eventContext)) {
        if (log.isDebugEnabled()) {
            log.debug("ConsentDeletionUserEventHandler is disabled. Not handling the " + event.getEventName() + " event.");
        }
        return;
    }
    Map<String, Object> eventProperties = event.getEventProperties();
    String userName = (String) eventProperties.get(IdentityEventConstants.EventProperty.USER_NAME);
    UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER);
    String domainName = userStoreManager.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    String tenantDomain = getUserTenantDomain(eventProperties);
    String usernameWithUserStoreDomain = UserCoreUtil.addDomainToName(userName, domainName);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Deleting consents for user: %s , in tenant domain :%s", usernameWithUserStoreDomain, tenantDomain));
    }
    ConsentManager consentManager = IdentityConsentDataHolder.getInstance().getPrivilegedConsentManager();
    try {
        List<ReceiptListResponse> receiptListResponses = consentManager.searchReceipts(consentSearchLimit, 0, usernameWithUserStoreDomain, null, "*", null);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Found %d receipts issued for user: %s, in tenant domain: %s", receiptListResponses.size(), usernameWithUserStoreDomain, tenantDomain));
        }
        receiptListResponses.forEach(rethrowConsumer(receiptListResponse -> {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Deleting receipt with ID : %s, issued for application %s", receiptListResponse.getConsentReceiptId(), receiptListResponse.getSpDisplayName()));
            }
            consentManager.deleteReceipt(receiptListResponse.getConsentReceiptId());
        }));
    } catch (ConsentManagementException e) {
        throw new IdentityEventException("Error while deleting consents for user " + userName, e);
    }
}
Also used : IdentityConsentMgtUtils(org.wso2.carbon.identity.consent.mgt.IdentityConsentMgtUtils) IdentityConsentDataHolder(org.wso2.carbon.identity.consent.mgt.internal.IdentityConsentDataHolder) InitConfig(org.wso2.carbon.identity.core.handler.InitConfig) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) Event(org.wso2.carbon.identity.event.event.Event) UserCoreConstants(org.wso2.carbon.user.core.UserCoreConstants) MessageContext(org.wso2.carbon.identity.core.bean.context.MessageContext) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) List(java.util.List) Map(java.util.Map) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext) IdentityEventConstants(org.wso2.carbon.identity.event.IdentityEventConstants) UserCoreUtil(org.wso2.carbon.user.core.util.UserCoreUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) ReceiptListResponse(org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse) ConsentManagementException(org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) ConsentManager(org.wso2.carbon.consent.mgt.core.ConsentManager) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)

Example 2 with IdentityEventMessageContext

use of org.wso2.carbon.identity.event.bean.IdentityEventMessageContext in project carbon-identity-framework by wso2.

the class IdentityEventMessageContextTest method testGetEvent.

@Test
public void testGetEvent() {
    IdentityEventMessageContext identityEventMessageContext = new IdentityEventMessageContext(event);
    Assert.assertNotNull(identityEventMessageContext.getEvent());
}
Also used : IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext) Test(org.testng.annotations.Test)

Example 3 with IdentityEventMessageContext

use of org.wso2.carbon.identity.event.bean.IdentityEventMessageContext in project carbon-identity-framework by wso2.

the class EventDistributionTask method run.

@Override
public void run() {
    running = true;
    // Run forever until stop the bundle. Will stop in eventQueue.take()
    while (running) {
        try {
            final Event event = eventQueue.take();
            IdentityEventMessageContext eventContext = new IdentityEventMessageContext(event);
            for (final AbstractEventHandler module : notificationSendingModules) {
                // If the module is subscribed to the event, module will be executed.
                if (module.isEnabled(eventContext)) {
                    // Create a runnable and submit to the thread pool for sending message.
                    Runnable msgSender = new Runnable() {

                        @Override
                        public void run() {
                            if (log.isDebugEnabled()) {
                                log.debug("Executing " + module.getName() + " on event" + event.getEventName());
                            }
                            try {
                                module.handleEvent(event);
                            } catch (IdentityEventException e) {
                                log.error("Error while invoking notification sending module " + module.getName(), e);
                            }
                        }
                    };
                    IdentityEventServiceDataHolder.getInstance().getThreadPool().submit(msgSender);
                }
            }
        } catch (InterruptedException e) {
            log.error("Error while picking up event from event queue", e);
        }
    }
}
Also used : AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) Event(org.wso2.carbon.identity.event.event.Event) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)

Example 4 with IdentityEventMessageContext

use of org.wso2.carbon.identity.event.bean.IdentityEventMessageContext in project carbon-identity-framework by wso2.

the class AbstractEventHandler method canHandle.

public boolean canHandle(MessageContext messageContext) throws IdentityRuntimeException {
    Event event = ((IdentityEventMessageContext) messageContext).getEvent();
    String eventName = event.getEventName();
    List<Subscription> subscriptionList = null;
    if (configs != null) {
        subscriptionList = configs.getSubscriptions();
    } else {
        return false;
    }
    if (subscriptionList != null) {
        for (Subscription subscription : subscriptionList) {
            if (subscription.getSubscriptionName().equals(eventName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Event(org.wso2.carbon.identity.event.event.Event) Subscription(org.wso2.carbon.identity.event.bean.Subscription) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)

Example 5 with IdentityEventMessageContext

use of org.wso2.carbon.identity.event.bean.IdentityEventMessageContext in project carbon-identity-framework by wso2.

the class IdentityEventServiceImpl method handleEvent.

@Override
public void handleEvent(Event event) throws IdentityEventException {
    List<AbstractEventHandler> eventHandlerList = IdentityEventServiceComponent.eventHandlerList;
    IdentityEventMessageContext eventContext = new IdentityEventMessageContext(event);
    for (final AbstractEventHandler handler : eventHandlerList) {
        if (handler.canHandle(eventContext)) {
            if (handler.isAssociationAsync(event.getEventName())) {
                eventDistributionTask.addEventToQueue(event);
            } else {
                handler.handleEvent(event);
            }
        }
    }
}
Also used : AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) IdentityEventMessageContext(org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)

Aggregations

IdentityEventMessageContext (org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)8 Event (org.wso2.carbon.identity.event.event.Event)6 Test (org.testng.annotations.Test)3 AbstractEventHandler (org.wso2.carbon.identity.event.handler.AbstractEventHandler)3 Subscription (org.wso2.carbon.identity.event.bean.Subscription)2 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 ConsentManager (org.wso2.carbon.consent.mgt.core.ConsentManager)1 ConsentManagementException (org.wso2.carbon.consent.mgt.core.exception.ConsentManagementException)1 ReceiptListResponse (org.wso2.carbon.consent.mgt.core.model.ReceiptListResponse)1 IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)1 IdentityConsentMgtUtils (org.wso2.carbon.identity.consent.mgt.IdentityConsentMgtUtils)1 IdentityConsentDataHolder (org.wso2.carbon.identity.consent.mgt.internal.IdentityConsentDataHolder)1 MessageContext (org.wso2.carbon.identity.core.bean.context.MessageContext)1 InitConfig (org.wso2.carbon.identity.core.handler.InitConfig)1 LambdaExceptionUtils.rethrowConsumer (org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer)1 IdentityEventConstants (org.wso2.carbon.identity.event.IdentityEventConstants)1