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