001/*
002 *  Copyright 2016 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.administration.welcome;
017
018import java.util.Set;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028import org.slf4j.Logger;
029
030import org.ametys.core.right.AllowedUsers;
031import org.ametys.core.right.RightManager;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.core.user.population.PopulationContextHelper;
034import org.ametys.runtime.plugin.component.LogEnabled;
035import org.ametys.web.repository.site.SiteManager;
036import org.ametys.web.site.SiteConfigurationManager;
037
038/**
039 * This step indicates that there is at least one well configured site with a user who has right to give its rights.
040 */
041public class SiteWithUserStep extends AbstractWelcomeStep implements Serviceable, Contextualizable, LogEnabled
042{
043    private ServiceManager _manager;
044    private SiteManager _siteManager;
045    private SiteConfigurationManager _siteConfigurationManager;
046    private RightManager _rightManager;
047    private PopulationContextHelper _populationContextHelper;
048    private Context _context;
049    private Logger _logger;
050    
051    @Override
052    public void contextualize(Context context) throws ContextException
053    {
054        _context = context;
055    }
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _manager = manager;
061        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
062    }
063    
064    public void setLogger(Logger logger)
065    {
066        _logger = logger;
067    }
068    
069    private Logger getLogger()
070    {
071        return _logger;
072    }
073    
074    private SiteManager _getSiteManager()
075    {
076        if (_siteManager == null)
077        {
078            try
079            {
080                _siteManager = (SiteManager) _manager.lookup(SiteManager.ROLE);
081            }
082            catch (ServiceException e)
083            {
084                // The component cannot be looked up in safe mode
085            }
086        }
087        return _siteManager;
088    }
089    
090    private SiteConfigurationManager _getSiteConfigurationManager()
091    {
092        if (_siteConfigurationManager == null)
093        {
094            try
095            {
096                _siteConfigurationManager = (SiteConfigurationManager) _manager.lookup(SiteConfigurationManager.ROLE);
097            }
098            catch (ServiceException e)
099            {
100                // The component cannot be looked up in safe mode
101            }
102        }
103        return _siteConfigurationManager;
104    }
105    
106    private PopulationContextHelper getPopulationContextHelper()
107    {
108        if (_populationContextHelper == null)
109        {
110            try
111            {
112                _populationContextHelper = (PopulationContextHelper) _manager.lookup(PopulationContextHelper.ROLE);
113            }
114            catch (ServiceException e)
115            {
116                // The component cannot be looked up in safe mode
117            }
118        }
119        return _populationContextHelper;
120    }
121    
122    @Override
123    public boolean isPerformed()
124    {
125        SiteManager siteManager = _getSiteManager();
126        if (siteManager == null)
127        {
128            return false;
129        }
130        
131        SiteConfigurationManager siteConfigurationManager = _getSiteConfigurationManager();
132        if (siteConfigurationManager == null)
133        {
134            return false;
135        }
136        
137        long t0 = System.currentTimeMillis();
138        getLogger().debug("Starting isPerformed...");
139        int index = 0;
140
141        Request request = ContextHelper.getRequest(_context);
142        for (String siteName : _siteManager.getSiteNames())
143        {
144            long t1 = System.currentTimeMillis();
145            index++;
146            if (siteConfigurationManager.isSiteConfigurationValid(siteName))
147            {
148                getLogger().debug("Site {} is valid", siteName);
149                try
150                {
151                    request.setAttribute("siteName", siteName); // Setting temporarily this attribute to check user right on '/cms' on this site
152                    
153                    AllowedUsers allowedUsers = _rightManager.getAllowedUsers("CMS_Rights_Delegate_Rights", "/cms");
154                    Set<UserIdentity> resolveAllowedUsers = allowedUsers.resolveAllowedUsers(true);
155
156                    long t2 = System.currentTimeMillis();
157                    getLogger().debug("getUsersByContext done in {}ms for site {}", t2 - t1, siteName);
158                    
159                    if (_hasOneCorrectUser(siteName, resolveAllowedUsers))
160                    {
161                        long t3 = System.currentTimeMillis();
162                        getLogger().info("isPerformed 'true' done in {}ms. Tested {} site(s)", t3 - t0, index);
163                        return true;
164                    }
165                }
166                finally
167                {
168                    request.setAttribute("siteName", null);
169                }
170            }
171            else
172            {
173                getLogger().debug("Site {} is invalid", siteName);
174            }
175        }
176        
177        long t3 = System.currentTimeMillis();
178        getLogger().info("isPerformed 'false' done in {}ms. Tested {} site(s)", t3 - t0, index);
179        return false;
180    }
181
182    private boolean _hasOneCorrectUser(String siteName, Set<UserIdentity> resolveAllowedUsers)
183    {
184        Set<String> userPopulationsOnContext = getPopulationContextHelper().getUserPopulationsOnContext("/sites/" + siteName, false);
185        if (userPopulationsOnContext.isEmpty())
186        {
187            return false;
188        }
189        
190        for (UserIdentity userIdentity : resolveAllowedUsers)
191        {
192            if (userPopulationsOnContext.contains(userIdentity.getPopulationId()))
193            {
194                return true;
195            }
196        }
197        
198        return false;
199    }
200}