001/*
002 *  Copyright 2014 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.inputdata;
017
018import org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.ContentHandler;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.right.RightManager;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.User;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.core.user.UserManager;
036import org.ametys.plugins.core.user.UserHelper;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.site.Site;
039
040/**
041 * {@link InputData} for SAXing events about FO user
042 */
043public class UserInputData implements InputData, Serviceable, Contextualizable
044{
045    private RightManager _rightManager;
046    private UserManager _userManagerFO;
047    private Context _context;
048    private UserHelper _userHelper;
049    private CurrentUserProvider _currentUserProvider;
050    
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
055        _userManagerFO = (UserManager) manager.lookup(UserManager.ROLE);
056        _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE);
057        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
058    }
059
060    @Override
061    public void contextualize(Context context) throws ContextException
062    {
063        _context = context;
064    }
065
066    @Override
067    public boolean isCacheable(Site site, Page page)
068    {
069        if (page != null)
070        {
071            return _rightManager.hasAnonymousReadAccess(page);
072        }
073        else
074        {
075            return true;
076        }
077    }
078
079    @Override
080    public void toSAX(ContentHandler handler) throws SAXException, ProcessingException
081    {
082        Request request = ContextHelper.getRequest(_context);
083        
084        Page page = (Page) request.getAttribute(Page.class.getName());
085        
086        handler.startDocument();
087        XMLUtils.startElement(handler, "User");
088        
089        UserIdentity userIdentity = _currentUserProvider.getUser();
090        
091        if (page != null && !_rightManager.hasAnonymousReadAccess(page) && userIdentity != null)
092        {
093            User user = _userManagerFO.getUser(userIdentity);
094            if (user != null)
095            {
096                _userHelper.saxUser(user, handler);
097            }
098        }
099        
100        XMLUtils.endElement(handler, "User");
101
102        handler.endDocument();
103    }
104
105}