001/*
002 *  Copyright 2022 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.forms.rights;
017
018import java.util.Collections;
019import java.util.Set;
020
021import org.ametys.core.group.GroupIdentity;
022import org.ametys.core.right.AccessController;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
025import org.ametys.plugins.forms.repository.Form;
026import org.ametys.plugins.forms.repository.FormDirectory;
027import org.ametys.plugins.repository.AmetysObject;
028
029/**
030 * {@link AccessController} for a {@link Form}
031 */
032public class FormAccessController extends AbstractHierarchicalAccessController<AmetysObject>
033{
034    @Override
035    public boolean isSupported(Object object)
036    {
037        return object instanceof Form || object instanceof FormDirectory;
038    }
039
040    @Override
041    protected Set<AmetysObject> _getParents(AmetysObject object)
042    {
043        AmetysObject parent = object.getParent();
044        if (isSupported(parent))
045        {
046            return Collections.singleton(parent);
047        }
048        else
049        {
050            return null;
051        }
052    }
053    
054    @Override
055    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
056    {
057        return null;
058    }
059    
060    @Override
061    protected AccessResult _getPermission(UserIdentity user, Set<GroupIdentity> userGroups, Set<String> profilesIds, Object object, Object convertedObject)
062    {
063        if (object instanceof Form form && form.getAuthor().equals(user))
064        {
065            return AccessResult.USER_ALLOWED;
066        }
067        
068        return super._getPermission(user, userGroups, profilesIds, object, convertedObject);
069    }
070}