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.odf.rights;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.search.model.impl.AbstractStaticSearchModelCriterionDefinition;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.core.user.CurrentUserProvider;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.runtime.model.type.ModelItemTypeConstants;
029
030/**
031 * UI criteria for program item context
032 */
033public class ODFContributorOrManagerCriterionDefinition extends AbstractStaticSearchModelCriterionDefinition<String>
034{
035    private ODFRightHelper _odfRightHelper;
036    private CurrentUserProvider _currentUserProvider;
037
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        
043        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
044        _odfRightHelper = (ODFRightHelper) smanager.lookup(org.ametys.odf.rights.ODFRightHelper.ROLE);
045    }
046    
047    @Override
048    public Operator getOperator()
049    {
050        // Exists allow to always include the query in QueryBuilder
051        return Operator.EXISTS;
052    }
053    
054    @Override
055    protected String getTypeId()
056    {
057        return ModelItemTypeConstants.STRING_TYPE_ID;
058    }
059    
060    @Override
061    public Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
062    {
063        UserIdentity currentUser = _currentUserProvider.getUser();
064        return _odfRightHelper.getRolesQuery(currentUser);
065    }
066}