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