001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.authentication;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import javax.servlet.FilterChain;
021: import javax.servlet.ServletException;
022: import javax.servlet.ServletRequest;
023: import javax.servlet.ServletResponse;
024: import org.acegisecurity.providers.anonymous.AnonymousProcessingFilter;
025: import org.acegisecurity.userdetails.memory.UserAttribute;
026: import org.jamwiki.WikiBase;
027: import org.jamwiki.model.Role;
028: import org.jamwiki.model.WikiGroup;
029: import org.jamwiki.utils.WikiLogger;
030: import org.jamwiki.utils.WikiUtil;
031:
032: /**
033: * This class allows anonymous users to be provided default roles from the
034: * JAMWiki database.
035: */
036: public class JAMWikiAnonymousProcessingFilter extends
037: AnonymousProcessingFilter {
038:
039: /** Standard logger. */
040: private static final WikiLogger logger = WikiLogger
041: .getLogger(JAMWikiAnonymousProcessingFilter.class.getName());
042: /** Default roles for anonymous users */
043: private static Role[] groupRoles = null;
044:
045: /**
046: * Set default roles for anonymous users.
047: */
048: public void afterPropertiesSet() throws Exception {
049: super .afterPropertiesSet();
050: this .initRoles();
051: }
052:
053: /**
054: * Override the parent method to ensure that default roles for anonymous
055: * users have been retrieved.
056: */
057: public void doFilter(ServletRequest request,
058: ServletResponse response, FilterChain chain)
059: throws IOException, ServletException {
060: if (groupRoles == null) {
061: // during setup and upgrade roles would not have been initialized, so
062: // initialize now.
063: this .initRoles();
064: }
065: super .doFilter(request, response, chain);
066: }
067:
068: /**
069: * Retrieve the default roles for anonymous users.
070: */
071: private void initRoles() {
072: try {
073: if (WikiUtil.isFirstUse() || WikiUtil.isUpgrade()) {
074: // wiki is not yet setup
075: return;
076: }
077: } catch (Exception e) {
078: logger
079: .info(
080: "Failure while determining first use / upgrade status of the wiki",
081: e);
082: }
083: UserAttribute user = this .getUserAttribute();
084: if (user == null) {
085: logger
086: .warning("No user attribute available in JAMWikiAnonymousProcessingFilter. Please verify the Acegi configuration settings.");
087: }
088: groupRoles = new Role[0];
089: try {
090: groupRoles = WikiBase.getDataHandler().getRoleMapGroup(
091: WikiGroup.GROUP_ANONYMOUS);
092: } catch (Exception e) {
093: // FIXME - without default roles bad things happen, so should this throw the
094: // error to the calling method?
095: logger.severe("Unable to retrieve default roles for "
096: + WikiGroup.GROUP_ANONYMOUS, e);
097: }
098: ArrayList anonymousRoles = new ArrayList();
099: anonymousRoles.add(Role.ROLE_ANONYMOUS);
100: for (int i = 0; i < groupRoles.length; i++) {
101: anonymousRoles.add(groupRoles[i]);
102: }
103: user.setAuthorities(anonymousRoles);
104: }
105:
106: /**
107: * Force a reset of the default role object. This method should be called
108: * if the roles allowed to anonymous users are changed.
109: */
110: public static void reset() {
111: JAMWikiAnonymousProcessingFilter.groupRoles = null;
112: }
113: }
|