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.HashMap; 019import java.util.Map; 020import java.util.Optional; 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024import org.apache.avalon.framework.component.Component; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028 029import org.ametys.core.ui.Callable; 030import org.ametys.core.user.CurrentUserProvider; 031import org.ametys.plugins.forms.dao.FormDAO; 032import org.ametys.plugins.forms.repository.Form; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.runtime.config.Config; 035 036/** 037 * The helper for acknowledgement of receipts 038 */ 039public class AcknowledgementOfReceiptsHelper implements Serviceable, Component 040{ 041 /** Avalon ROLE. */ 042 public static final String ROLE = AcknowledgementOfReceiptsHelper.class.getName(); 043 044 /** Pattern for senders mail */ 045 public static final String REGEX_SENDER_MAIL = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,})$"; 046 047 /** Ametys object resolver. */ 048 protected AmetysObjectResolver _resolver; 049 050 /** The form DAO */ 051 protected FormDAO _formDAO; 052 053 /** The current user provider */ 054 protected CurrentUserProvider _currentUserProvider; 055 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 059 _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE); 060 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 061 } 062 063 /** 064 * Get the form properties relative to the acknowledgement of receipt 065 * @param formId The id of the form 066 * @return a map of the form acknowledgement of receipt properties 067 */ 068 @Callable 069 public Map<String, Object> getAcknowledgementReceiptProperties(String formId) 070 { 071 Map<String, Object> receiptProperties = new HashMap<> (); 072 073 Form form = _resolver.resolveById(formId); 074 _addOptionalProperty(Form.RECEIPT_SENDER, form.getReceiptSender(), receiptProperties, Config.getInstance().getValue("smtp.mail.from")); 075 _addOptionalProperty(Form.RECEIPT_RECEIVER, form.getReceiptReceiver(), receiptProperties, FormMailHelper.RECEIVER_COMBOBOX_ENTRY_USER_VALUE); 076 _addOptionalProperty(Form.RECEIPT_SUBJECT, form.getReceiptSubject(), receiptProperties, null); 077 _addOptionalProperty(Form.RECEIPT_BODY, form.getReceiptBody(), receiptProperties, null); 078 079 return receiptProperties; 080 } 081 082 private void _addOptionalProperty(String propertyName, Optional<? extends Object> value, Map<String, Object> limitProperties, Object defaultValue) 083 { 084 if (value.isPresent()) 085 { 086 limitProperties.put(propertyName, value.get()); 087 } 088 else if (defaultValue != null) 089 { 090 limitProperties.put(propertyName, defaultValue); 091 } 092 } 093 094 /** 095 * Set the properties relative to the acknowledgement of receipt 096 * @param formId id of the form 097 * @param sender the sender email address 098 * @param receiver name for form of question where the receiver's email address will be entered 099 * @param subject The subject for the email 100 * @param body The body of the email 101 * @return the map of results 102 */ 103 @Callable 104 public Map<String, Object> setAcknowledgementReceiptProperties(String formId, String sender, String receiver, String subject, String body) 105 { 106 Map<String, Object> result = new HashMap<>(); 107 108 Form form = _resolver.resolveById(formId); 109 if (!_formDAO.hasWriteRightOnForm(_currentUserProvider.getUser(), form)) 110 { 111 result.put("message", "not-allowed"); 112 return result; 113 } 114 115 Pattern pattern = Pattern.compile(REGEX_SENDER_MAIL); 116 Matcher matcher = pattern.matcher(sender); 117 if (matcher.matches()) 118 { 119 form.setReceiptSender(sender); 120 form.setReceiptReceiver(receiver); 121 form.setReceiptSubject(subject); 122 form.setReceiptBody(body); 123 124 form.saveChanges(); 125 } 126 127 return result; 128 } 129 130 /** 131 * Unset the properties relative to the acknowledgement of receipt 132 * @param formId The id ofthe current form 133 * @return the map of results 134 */ 135 @Callable 136 public Map<String, Object> removeEntriesReceipt(String formId) 137 { 138 Map<String, Object> result = new HashMap<>(); 139 140 Form form = _resolver.resolveById(formId); 141 if (!_formDAO.hasWriteRightOnForm(_currentUserProvider.getUser(), form)) 142 { 143 result.put("message", "not-allowed"); 144 return result; 145 } 146 147 if (form.getReceiptReceiver().isPresent()) 148 { 149 form.removeValue(Form.RECEIPT_SENDER); 150 form.removeValue(Form.RECEIPT_RECEIVER); 151 form.removeValue(Form.RECEIPT_SUBJECT); 152 form.removeValue(Form.RECEIPT_BODY); 153 154 form.saveChanges(); 155 } 156 157 return result; 158 } 159}