001/*
002 *  Copyright 2020 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.plugins.ugc.accesscontroller;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.collections.MapUtils;
027
028import org.ametys.cms.contenttype.ContentTypesHelper;
029import org.ametys.cms.repository.Content;
030import org.ametys.core.group.GroupIdentity;
031import org.ametys.core.right.AccessController;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.ugc.UGCConstants;
034
035/**
036 * {@link AccessController} so creator of a UGC content types receive edit/delete rights on it
037 *
038 */
039public class UGCCreatorContentAccessController implements AccessController, Serviceable
040{
041    private static final List<String> __CREATOR_RIGHTS = List.of(
042            "Front_Edition_Access_Right", 
043            "Workflow_Rights_Edition_Online",
044            "CMS_Rights_DeleteContent"
045    );
046    
047    /** ContentTypes Helper */
048    protected ContentTypesHelper _cTypeHelper;
049    
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
053    }
054    
055    public boolean isSupported(Object object)
056    {
057        return object instanceof Content && _cTypeHelper.isInstanceOf((Content) object, UGCConstants.UGC_MIXIN_TYPE);
058    }
059    
060    public AccessResult getPermission(UserIdentity user, Set<GroupIdentity> userGroups, String rightId, Object object)
061    {
062        if (object instanceof Content && ((Content) object).getCreator().equals(user))
063        {
064            return __CREATOR_RIGHTS.contains(rightId) ? AccessResult.USER_ALLOWED : AccessResult.UNKNOWN;
065        }
066        
067        return AccessResult.UNKNOWN;
068    }
069
070    public AccessResult getReadAccessPermission(UserIdentity user, Set<GroupIdentity> userGroups, Object object)
071    {
072        return AccessResult.UNKNOWN;
073    }
074
075    /**
076     * If creator, access to a list of rights
077     */
078    public Map<String, AccessResult> getPermissionByRight(UserIdentity user, Set<GroupIdentity> userGroups, Object object)
079    {
080        Map<String, AccessResult> permissionByRight = new HashMap<>();
081        
082        if (((Content) object).getCreator().equals(user))
083        {
084            for (String rightId : __CREATOR_RIGHTS)
085            {
086                permissionByRight.put(rightId, AccessResult.USER_ALLOWED);
087            }
088        }
089        
090        return permissionByRight;
091    }
092
093    public AccessResult getPermissionForAnonymous(String rightId, Object object)
094    {
095        return AccessResult.UNKNOWN;
096    }
097
098    public AccessResult getReadAccessPermissionForAnonymous(Object object)
099    {
100        return AccessResult.UNKNOWN;
101    }
102
103    public AccessResult getPermissionForAnyConnectedUser(String rightId, Object object)
104    {
105        return AccessResult.UNKNOWN;
106    }
107
108    public AccessResult getReadAccessPermissionForAnyConnectedUser(Object object)
109    {
110        return AccessResult.UNKNOWN;
111    }
112
113    /**
114     * If right requested is in the list, the creator is added the list of USER_ALLOWED
115     */
116    public Map<UserIdentity, AccessResult> getPermissionByUser(String rightId, Object object)
117    {
118        Map<UserIdentity, AccessResult> permissionByUser = new HashMap<>();
119        
120        if (__CREATOR_RIGHTS.contains(rightId))
121        {
122            permissionByUser.put(((Content) object).getCreator(), AccessResult.USER_ALLOWED);
123        }
124        return permissionByUser;
125    }
126
127    public Map<UserIdentity, AccessResult> getReadAccessPermissionByUser(Object object)
128    {
129        return MapUtils.EMPTY_MAP;
130    }
131
132    public Map<GroupIdentity, AccessResult> getPermissionByGroup(String rightId, Object object)
133    {
134        return MapUtils.EMPTY_MAP;
135    }
136
137    public Map<GroupIdentity, AccessResult> getReadAccessPermissionByGroup(Object object)
138    {
139        return MapUtils.EMPTY_MAP;
140    }
141
142    public boolean hasUserAnyPermissionOnWorkspace(Set<Object> workspacesContexts, UserIdentity user, Set<GroupIdentity> userGroups, String rightId)
143    {
144        return false;
145    }
146
147    public boolean hasUserAnyReadAccessPermissionOnWorkspace(Set<Object> workspacesContexts, UserIdentity user, Set<GroupIdentity> userGroups)
148    {
149        return false;
150    }
151
152    public boolean hasAnonymousAnyPermissionOnWorkspace(Set<Object> workspacesContexts, String rightId)
153    {
154        return false;
155    }
156
157    public boolean hasAnonymousAnyReadAccessPermissionOnWorkspace(Set<Object> workspacesContexts)
158    {
159        return false;
160    }
161
162    public boolean hasAnyConnectedUserAnyPermissionOnWorkspace(Set<Object> workspacesContexts, String rightId)
163    {
164        return false;
165    }
166
167    public boolean hasAnyConnectedUserAnyReadAccessPermissionOnWorkspace(Set<Object> workspacesContexts)
168    {
169        return false;
170    }
171}