001/*
002 *  Copyright 2018 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.frontoffice.search.requesttime.impl;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021
022import org.ametys.cms.search.solr.SearcherFactory.Searcher;
023import org.ametys.core.right.AllowedUsers;
024import org.ametys.core.right.RightManager;
025import org.ametys.web.frontoffice.search.instance.model.RightCheckingMode;
026import org.ametys.web.frontoffice.search.requesttime.AbstractSearchComponent;
027import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
028import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
029
030/**
031 * {@link SearchComponent} for enabling correct {@link RightCheckingMode right-checking}
032 */
033public class CheckRightSearchComponent extends AbstractSearchComponent implements Serviceable
034{
035    /** The right manager */
036    protected RightManager _rightManager;
037
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
042    }
043    
044    @Override
045    public int getPriority()
046    {
047        return SEARCH_PRIORITY - 2000;
048    }
049
050    @Override
051    public boolean supports(SearchComponentArguments args)
052    {
053        return true;
054    }
055
056    @Override
057    public void execute(SearchComponentArguments args) throws Exception
058    {
059        Searcher searcher = args.searcher();
060        RightCheckingMode rightCheckingMode = args.serviceInstance().getRightCheckingMode();
061        switch (rightCheckingMode)
062        {
063            case EXACT:
064                searcher.setCheckRights(true);
065                break;
066            case FAST:
067                AllowedUsers allowedUsersOnPage = _rightManager.getReadAccessAllowedUsers(args.currentPage());
068                searcher.checkRightsComparingTo(allowedUsersOnPage);
069                break;
070            case NONE:
071                searcher.setCheckRights(false);
072                break;
073            default:
074                throw new IllegalStateException("Unhandled right checking mode: " + rightCheckingMode);
075        }
076    }
077}