001/*
002 *  Copyright 2025 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.contentio.csv;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.core.ui.Callable;
027import org.ametys.core.user.CurrentUserProvider;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.runtime.authentication.AccessDeniedException;
030
031/**
032 * Helper handling CSV import of reference table entries
033 */
034public class ImportReferenceTablesCSVFileHelper extends ImportCSVFileHelper
035{
036    private CurrentUserProvider _currentUser;
037
038    @Override
039    public void service(ServiceManager serviceManager) throws ServiceException
040    {
041        super.service(serviceManager);
042        _currentUser = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
043    }
044    
045    // Override to change the rights to check for the {@link Callable}
046    @Override
047    @Callable (rights = "CMS_Rights_ReferenceTables_Import", context = "/cms")
048    public Map<String, Object> importContents(Map<String, Object> config, Map<String, Object> formValues, List<Map<String, Object>> mappingValues) throws IOException
049    {
050        String contentTypeId = (String) config.get("contentType");
051        ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
052        if (contentType.isReferenceTable())
053        {
054            return super.importContents(config, formValues, mappingValues);
055        }
056        else
057        {
058            UserIdentity user = _currentUser.getUser();
059            throw new AccessDeniedException("User" + UserIdentity.userIdentityToString(user) + " tried to import reference table entries but this is not allowed for content type '" + contentTypeId + "'");
060        }
061    }
062}