Announcement Announcement Module
Collapse
No announcement yet.
A stateful guest user Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • A stateful guest user

    we have been using user.properties to store data related to a certain user session. this works when a user is signed in, but not for "guest" users. I've created an extension to the AlfrescoUserFactory that seems to work. It may be helpful to someone else looking to do this.

    Code:
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.extensions.surf.RequestContext;
    import org.springframework.extensions.surf.exception.UserFactoryException;
    import org.springframework.extensions.surf.support.AlfrescoUserFactory;
    import org.springframework.extensions.webscripts.connector.User;
    
    /**
     * override methods in AlfrescoUserFactory and AbstractUserFactory
     * to create unique guest users and add them to the session
     */
    public class SpecialUserFactory extends AlfrescoUserFactory
    {  
        public User initialiseUser(RequestContext context, HttpServletRequest request, String endpoint, boolean force)
        throws UserFactoryException {
        	
        	User u = super.initialiseUser(context,request,endpoint,force);
        	if (u.isGuest() && request.getSession().getAttribute(SESSION_ATTRIBUTE_KEY_USER_ID) == null) {
        		// add to the session if not there
        		request.getSession().setAttribute(SESSION_ATTRIBUTE_KEY_USER_OBJECT, u);
        		request.getSession().setAttribute(SESSION_ATTRIBUTE_KEY_USER_ID, u.getId());
        	}
        	return u;
        }
        
        protected User getGuestUser(RequestContext context) throws UserFactoryException
        {
        	Map<String, Boolean> capabilities = new HashMap<String, Boolean>(4);
        	capabilities.put(User.CAPABILITY_ADMIN, false);
        	capabilities.put(User.CAPABILITY_GUEST, true);
        	capabilities.put(User.CAPABILITY_MUTABLE, false);
        	User user = new User(USER_GUEST, capabilities);
        	user.setFirstName("Guest");
        	
        	return user;
        }
    }
    create a definition in a bean file
    Code:
    <bean id="webframework.factory.user.special" class="SpecialUserFactory" parent="webframework.factory.base" />
    and add the reference to surf.xml
    Code:
    <web-framework>
    ....
        <user-factory>webframework.factory.user.special</user-factory>
      </defaults>
    </web-framework>
    I'm interested to know if anyone is aware of problems this may cause.
Working...
X