001/*
002 *  Copyright 2023 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.content.consistency;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.cms.content.consistency.ContentConsistencyModel;
032import org.ametys.core.right.RightManager;
033import org.ametys.core.right.RightManager.RightResult;
034import org.ametys.core.ui.Callable;
035import org.ametys.plugins.repository.query.expression.Expression;
036import org.ametys.plugins.repository.query.expression.Expression.Operator;
037import org.ametys.plugins.repository.query.expression.MetadataExpression;
038import org.ametys.plugins.repository.query.expression.NotExpression;
039import org.ametys.plugins.repository.query.expression.OrExpression;
040import org.ametys.plugins.repository.query.expression.StringExpression;
041import org.ametys.web.WebHelper;
042
043/**
044 * Override to restrict the search to the current site or outside
045 */
046public class ContentConsistencySearcher extends org.ametys.cms.content.consistency.ContentConsistencySearcher implements Contextualizable
047{
048    /** right id to access consistency result for out of site content */
049    public static final String WEB_RIGHTS_TOOLS_GLOBAL_CONSISTENCY_NO_SITE = "Web_Rights_Tools_GlobalConsistency_No_Site";
050    private Context _context;
051    private RightManager _rightManager;
052
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
058    }
059    
060    public void contextualize(Context context) throws ContextException
061    {
062        _context = context;
063    }
064    
065    // Override to add the no site right to the callable
066    @Override
067    @Callable(rights = {CMS_RIGHTS_TOOLS_GLOBAL_CONSISTENCY, WEB_RIGHTS_TOOLS_GLOBAL_CONSISTENCY_NO_SITE})
068    public Map<String, Object> getModel() throws ProcessingException
069    {
070        return super.getModel();
071    }
072    
073    // Override to add the no site right to the callable
074    @Override
075    @Callable(rights = {CMS_RIGHTS_TOOLS_GLOBAL_CONSISTENCY, WEB_RIGHTS_TOOLS_GLOBAL_CONSISTENCY_NO_SITE})
076    public Map<String, Object> searchResults(Map<String, Object> jsonParams) throws ProcessingException
077    {
078        return super.searchResults(jsonParams);
079    }
080    
081    @Override
082    protected Expression getExpression(List<Expression> criteriaExpressions)
083    {
084        // Filter result based on allowed context for user
085        Request request = ContextHelper.getRequest(_context);
086        String siteName = WebHelper.getSiteName(request);
087        List<Expression> contextExpression = new ArrayList<>();
088        if (siteName != null && _rightManager.currentUserHasRight(CMS_RIGHTS_TOOLS_GLOBAL_CONSISTENCY, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW)
089        {
090            contextExpression.add(new StringExpression(ContentConsistencyModel.CONTEXT, Operator.EQ, siteName));
091        }
092        
093        if (_rightManager.currentUserHasRight(WEB_RIGHTS_TOOLS_GLOBAL_CONSISTENCY_NO_SITE, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW)
094        {
095            contextExpression.add(new NotExpression(new MetadataExpression(ContentConsistencyModel.CONTEXT)));
096        }
097        
098        criteriaExpressions.add(new OrExpression(contextExpression.toArray(size -> new Expression[size])));
099        
100        return super.getExpression(criteriaExpressions);
101    }
102}