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.Set;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.core.group.GroupIdentity;
024import org.ametys.core.ui.ClientSideElement;
025import org.ametys.core.ui.right.ProfileAssignmentsToolClientSideElement;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.forms.dao.FormDAO;
028import org.ametys.plugins.forms.dao.FormDirectoryDAO;
029import org.ametys.plugins.forms.repository.Form;
030import org.ametys.plugins.forms.repository.FormDirectory;
031
032/**
033 * {@link ClientSideElement} for the tool displaying the profile assignments for forms
034 */
035public class FormsProfileAssignmentsToolClientSideElement extends ProfileAssignmentsToolClientSideElement
036{
037    /** The form DAO */
038    private FormDAO _formDAO;
039    
040    /** The form directory DAO */
041    private FormDirectoryDAO _formDirectoryDAO;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _formDAO = (FormDAO) smanager.lookup(FormDAO.ROLE);
048        _formDirectoryDAO = (FormDirectoryDAO) smanager.lookup(FormDirectoryDAO.ROLE);
049    }
050
051    @Override
052    protected boolean canDelegateRights(UserIdentity user, Set<GroupIdentity> groups, String profileId, String assignment , Object context)
053    {
054        // Any user having affectation rights on a form or a form directory should be able to affect profiles, even if he don't already have it
055        if (context instanceof Form)
056        {
057            return _formDAO.hasRightAffectationRightOnForm(user, (Form) context);
058        }
059        else if (context instanceof FormDirectory)
060        {
061            return _formDirectoryDAO.hasRightAffectationRightOnFormDirectory(user, (FormDirectory) context);
062        }
063        return super.canDelegateRights(user, groups, profileId, assignment, context);
064    }
065}