001/*
002 *  Copyright 2019 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.cms.indexing.solr;
017
018import org.apache.commons.lang3.StringUtils;
019
020import org.ametys.core.right.RightManager;
021import org.ametys.runtime.util.Annotations;
022import org.ametys.runtime.workspace.WorkspaceManager;
023
024/**
025 * Methods for handling influence of objects on Solr Acl cache.
026 * <br>See {@link #isUninfluent}.
027 */
028public final class SolrAclCacheInfluence
029{
030    private SolrAclCacheInfluence()
031    {
032    }
033    
034    /**
035     * Determines if any change of profile assignment on the given context object ensures that there is no indexed object
036     * (neither the given object nor any other object in the whole application) that is affected in 
037     * its own {@link RightManager#getReadAccessAllowedUsers read access} computing.
038     * <br>i.e. the given object has no influence at all on the whole Solr Acl cache.
039     * <br>
040     * <br>See {@link SolrAclCacheUninfluentialObject}.
041     * @param object The object
042     * @return <code>true</code> if the given object is uninfluent on the Solr Acl cache.
043     */
044    public static boolean isUninfluent(Object object)
045    {
046        return _isWorkspaceContext(object)
047                || Annotations.isAnnotationPresent(object.getClass(), SolrAclCacheUninfluentialObject.class);
048    }
049    
050    private static boolean _isWorkspaceContext(Object object)
051    {
052        return object instanceof String
053                && StringUtils.startsWithAny((String) object, _workspacesContexts());
054    }
055    
056    private static String[] _workspacesContexts()
057    {
058        return WorkspaceManager.getInstance()
059                .getWorkspaceNames()
060                .stream()
061                .map("/"::concat)
062                .toArray(String[]::new);
063    }
064}
065