/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2009-2014 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.web.springframework.security;

import java.io.IOException;
import java.util.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.opennms.netmgt.config.GroupDao;
import org.opennms.netmgt.config.groups.Group;
import org.opennms.netmgt.model.FilterManager;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * <p>AuthFilterEnabler class.</p>
 */
public class AuthFilterEnabler implements Filter {
    
    private FilterManager m_filterManager;
    
    private GroupDao m_groupDao;
    
    /**
     * <p>setFilterManager</p>
     *
     * @param filterManager a {@link org.opennms.netmgt.model.FilterManager} object.
     */
    public void setFilterManager(FilterManager filterManager) {
        m_filterManager = filterManager;
    }
    
    /**
     * <p>setGroupDao</p>
     *
     * @param groupDao a {@link org.opennms.netmgt.config.GroupDao} object.
     */
    public void setGroupDao(GroupDao groupDao) {
        m_groupDao = groupDao;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.ui.SpringSecurityFilter#doFilterHttp(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
     */
    /** {@inheritDoc} */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        
            boolean shouldFilter = AclUtils.shouldFilter(SecurityContextHolder.getContext().getAuthentication().getAuthorities());

        try {
            String[] groupNames;
            if (shouldFilter) {
                String user = SecurityContextHolder.getContext().getAuthentication().getName();

                List<Group> groups = m_groupDao.findGroupsForUser(user);
                groupNames  = new String[groups.size()];
                for(int i = 0; i < groups.size(); i++) {
                    groupNames[i] = groups.get(i).getName();
                }

                if(SecurityContextHolder.getContext().getAuthentication().getDetails() instanceof OnmsAuthenticationDetails){
                    OnmsAuthenticationDetails details = (OnmsAuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();

                    if(details.getUserGroups() != null && details.getUserGroups().length > 0){
                        Set<String> detailsSet = new HashSet<String>(Arrays.asList(details.getUserGroups()));
                        Set<String> groupSet = new HashSet<String>(Arrays.asList(groupNames));

                        if(groupSet.containsAll(detailsSet)){
                            groupNames = details.getUserGroups();
                        }
                    }
                }

                m_filterManager.enableAuthorizationFilter(groupNames);
            }
            chain.doFilter(request, response);

        } finally {
            if (shouldFilter) {
                m_filterManager.disableAuthorizationFilter();
            }

        }

        
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

}

@@ -1646,24 +1646,81 @@
 terManager;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1779,16 +1779,16 @@
 Holder;%0A
-
 %0A/**%0A * 
@@ -1875,24 +1875,113 @@
 lter %7B%0A    %0A
+    private final static Logger LOG = LoggerFactory.getLogger(AuthFilterEnabler.class);%0A%0A
     private 
@@ -3218,25 +3218,93 @@
 .getName();%0A
+                LOG.debug(%22Applying ACL filter for user: %7B%7D%22, user);
 %0A
-
             
@@ -3548,32 +3548,126 @@
                %7D
+%0A                LOG.debug(%22Found groups for user %7B%7D: %7B%7D%22, user, Arrays.toString(groupNames));
 %0A%0A              
@@ -4420,32 +4420,257 @@
              %7D%0A%0A
+                // Log the group names again, since they may have been changed in the statement above%0A                LOG.debug(%22Enabling authorization filter for user %7B%7D with groups: %7B%7D%22, user, Arrays.toString(groupNames));%0A
                 
@@ -4907,32 +4907,32 @@
 %0A            %7D%0A%0A
+
         %7D%0A%0A     
@@ -4924,26 +4924,16 @@
        %7D
-%0A%0A        
 %0A    %7D%0A%0A

Add additional debug logging to the AuthFilter.