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.helper;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Optional;
023import java.util.regex.Pattern;
024
025import org.apache.avalon.framework.component.Component;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.commons.lang3.ArrayUtils;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.core.ui.Callable;
033import org.ametys.plugins.forms.dao.FormDAO;
034import org.ametys.plugins.forms.repository.Form;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038/**
039 * The helper to handle admin emails
040 */
041public class FormAdminMailsHelper extends AbstractLogEnabled implements Serviceable, Component
042{
043    /** Avalon ROLE. */
044    public static final String ROLE = FormAdminMailsHelper.class.getName();
045    
046    /** Pattern for admin mails */
047    public static final String REGEX_MAIL = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,})$";
048    
049    /** Ametys object resolver. */
050    protected AmetysObjectResolver _resolver;
051    
052    /** The form DAO */
053    protected FormDAO _formDAO;
054    
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
058        _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE);
059    }
060    
061    /**
062     * Get the entries admin emails
063     * @param formId Id of the form
064     * @return the array of admin emails
065     */
066    @Callable
067    public String[] getAdminEmails(String formId)
068    {
069        Form form = _resolver.resolveById(formId);
070        _formDAO.checkHandleFormRight(form);
071        
072        Optional<String[]> adminMails = form.getAdminEmails();
073        return adminMails.isPresent() ? adminMails.get() : ArrayUtils.EMPTY_STRING_ARRAY;
074    }
075    
076    /**
077     * Get the entries admin emails
078     * @param formId Id of the form
079     * @param emails mails of the admin
080     * @return the map of results
081     */
082    @Callable
083    public Map<String, Object> setAdminEmails(String formId, String emails)
084    {
085        Map<String, Object> result = new HashMap<>();
086        
087        Form form = _resolver.resolveById(formId);
088        _formDAO.checkHandleFormRight(form);
089        
090        //Check regex
091        String[] emailsTab = emails.split("[ ,;\r\n]");
092        Pattern pattern = Pattern.compile(REGEX_MAIL);
093        boolean error = false;
094        List<String> computedEmails = new ArrayList<>();
095        for (String mail : emailsTab)
096        {
097            String trimMail = mail.trim();
098            boolean isMail = StringUtils.isNotBlank(trimMail) && pattern.matcher(trimMail).matches();
099            if (isMail)
100            {
101                computedEmails.add(trimMail);
102            }
103            else if (StringUtils.isNotBlank(trimMail))
104            {
105                error = true;
106            }
107        }
108        
109        if (!error)
110        {
111            form.setAdminEmails(computedEmails.toArray(new String[computedEmails.size()]));
112            form.saveChanges();
113        }
114        else
115        {
116            getLogger().error("Mails addresses " + emails + " did not match regex");
117            result.put("message", "invalid-address");
118        }
119        
120        return result;
121    }
122}