package org.pac4j.springframework.security.profile;

import org.pac4j.core.context.JEEContext;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.springframework.security.util.SpringSecurityHelper;

import java.util.LinkedHashMap;

/**
 * Specific profile manager for Spring Security.
 *
 * @author Jerome Leleu
 * @since 2.0.1
 */
public class SpringSecurityProfileManager extends ProfileManager<CommonProfile> {

    public SpringSecurityProfileManager(final WebContext context) {
        super(context);
    }

    public SpringSecurityProfileManager(final WebContext context, final SessionStore<JEEContext> sessionStore) {
        super(context, sessionStore);
    }

    @Override
    protected void saveAll(LinkedHashMap<String, CommonProfile> profiles, final boolean saveInSession) {
        super.saveAll(profiles, saveInSession);

        SpringSecurityHelper.populateAuthentication(retrieveAll(saveInSession));
    }
}

@@ -341,16 +341,88 @@
 yHelper;
+%0Aimport org.springframework.security.core.context.SecurityContextHolder;
 %0A%0Aimport
@@ -1142,11 +1142,178 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void remove(final boolean removeFromSession) %7B%0A        super.remove(removeFromSession);%0A%0A        SecurityContextHolder.clearContext();%0A    %7D
 %0A%7D%0A

clear Spring Security context in addition to the pac4j context