001/*
002 *  Copyright 2016 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.web.usermanagement;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.core.util.I18nUtils;
030import org.ametys.runtime.i18n.I18nizableTextParameter;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.plugin.component.PluginAware;
033
034/**
035 * The default user sign up configuration
036 */
037public class DefaultUserSignUpConfiguration implements UserSignUpConfiguration, Configurable, Serviceable, PluginAware, Component
038{
039    /** The i18n utils */
040    protected I18nUtils _i18nUtils;
041    
042    private int _tokenValidity;
043    private String _subjectKeyForSignupEmail;
044    private String _textBodyKeyForSignupEmail;
045    private String _htmlBodyKeyForSignupEmail;
046    private String _subjectKeyForInvitToSignupEmail;
047    private String _textBodyKeyForInvitToSignupEmail;
048    private String _htmlBodyKeyForInvitToSignupEmail;
049    private String _subjectKeyForSignupValidationEmail;
050    private String _textBodyKeyForSignupValidationEmail;
051    private String _htmlBodyKeyForSignupValidationEmail;
052    private String _subjectKeyForResetPwd;
053    private String _textBodyKeyForResetPwd;
054    private String _htmlBodyKeyForResetPwd;
055
056    private String _pluginName;
057
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);        
063    }
064    
065    @Override
066    public void setPluginInfo(String pluginName, String featureName, String id)
067    {
068        _pluginName = pluginName;
069    }
070    
071    @Override
072    public void configure(Configuration configuration) throws ConfigurationException
073    {
074        _tokenValidity = Integer.parseInt(configuration.getChild("token-validity").getValue(null));
075        _subjectKeyForSignupEmail = configuration.getChild("signup-email-subject").getValue(null);
076        _textBodyKeyForSignupEmail = configuration.getChild("signup-email-text-body").getValue(null);
077        _htmlBodyKeyForSignupEmail = configuration.getChild("signup-email-html-body").getValue(null);
078        _subjectKeyForInvitToSignupEmail = configuration.getChild("invit-to-signup-email-subject").getValue(null);
079        _textBodyKeyForInvitToSignupEmail = configuration.getChild("invit-to-signup-email-text-body").getValue(null);
080        _htmlBodyKeyForInvitToSignupEmail = configuration.getChild("invit-to-signup-email-html-body").getValue(null);
081        _subjectKeyForSignupValidationEmail = configuration.getChild("signup-validation-email-subject").getValue(null);
082        _textBodyKeyForSignupValidationEmail = configuration.getChild("signup-validation-email-text-body").getValue(null);
083        _htmlBodyKeyForSignupValidationEmail = configuration.getChild("signup-validation-email-html-body").getValue(null);
084        _subjectKeyForResetPwd = configuration.getChild("reset-pwd-email-subject").getValue(null);
085        _textBodyKeyForResetPwd = configuration.getChild("reset-pwd-email-text-body").getValue(null);
086        _htmlBodyKeyForResetPwd = configuration.getChild("reset-pwd-email-html-body").getValue(null);
087    }
088    
089    @Override
090    public int getTokenValidity()
091    {
092        return _tokenValidity;
093    }
094
095    @Override
096    public String getSubjectForSignUpEmail (Map<String, I18nizableTextParameter> defaultI18nParams , String language)
097    {
098        if (StringUtils.isNotBlank(_subjectKeyForSignupEmail))
099        {
100            return _i18nUtils.translate(_getI18nizableText(_subjectKeyForSignupEmail, defaultI18nParams), language);
101        }
102        
103        return null;
104    }
105
106    @Override
107    public String getTextBodyForSignUpEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
108    {
109        if (StringUtils.isNotBlank(_textBodyKeyForSignupEmail))
110        {
111            return _i18nUtils.translate(_getI18nizableText(_textBodyKeyForSignupEmail, defaultI18nParams), language);
112        }
113        
114        return null;
115    }
116
117    @Override
118    public String getHtmlBodyForSignUpEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
119    {
120        if (StringUtils.isNotBlank(_htmlBodyKeyForSignupEmail))
121        {
122            return _i18nUtils.translate(_getI18nizableText(_htmlBodyKeyForSignupEmail, defaultI18nParams), language);
123        }
124        
125        return null;
126    }
127    
128    public String getSubjectForInvitationToSignUpEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
129    {
130        if (StringUtils.isNotBlank(_subjectKeyForInvitToSignupEmail))
131        {
132            return _i18nUtils.translate(_getI18nizableText(_subjectKeyForInvitToSignupEmail, defaultI18nParams), language);
133        }
134        
135        return null;
136    }
137    
138    public String getTextBodyForInvitationToSignUpEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
139    {
140        if (StringUtils.isNotBlank(_textBodyKeyForInvitToSignupEmail))
141        {
142            return _i18nUtils.translate(_getI18nizableText(_textBodyKeyForInvitToSignupEmail, defaultI18nParams), language);
143        }
144        
145        return null;
146    }
147    
148    public String getHtmlBodyForInvitationToSignUpEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
149    {
150        if (StringUtils.isNotBlank(_htmlBodyKeyForInvitToSignupEmail))
151        {
152            return _i18nUtils.translate(_getI18nizableText(_htmlBodyKeyForInvitToSignupEmail, defaultI18nParams), language);
153        }
154        
155        return null;
156    }
157    
158    @Override
159    public String getSubjectForSignUpValidatedEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
160    {
161        if (StringUtils.isNotBlank(_subjectKeyForSignupValidationEmail))
162        {
163            return _i18nUtils.translate(_getI18nizableText(_subjectKeyForSignupValidationEmail, defaultI18nParams), language);
164        }
165        
166        return null;
167    }
168    
169    @Override
170    public String getTextBodyForSignUpValidatedEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
171    {
172        if (StringUtils.isNotBlank(_textBodyKeyForSignupValidationEmail))
173        {
174            return _i18nUtils.translate(_getI18nizableText(_textBodyKeyForSignupValidationEmail, defaultI18nParams), language);
175        }
176        
177        return null;
178    }
179    
180    @Override
181    public String getHtmlBodyForSignUpValidatedEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
182    {
183        if (StringUtils.isNotBlank(_htmlBodyKeyForSignupValidationEmail))
184        {
185            return _i18nUtils.translate(_getI18nizableText(_htmlBodyKeyForSignupValidationEmail, defaultI18nParams), language);
186        }
187        
188        return null;
189    }
190    
191    @Override
192    public String getSubjectForResetPwdEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
193    {
194        if (StringUtils.isNotBlank(_subjectKeyForResetPwd))
195        {
196            return _i18nUtils.translate(_getI18nizableText(_subjectKeyForResetPwd, defaultI18nParams), language);
197        }
198        
199        return null;
200    }
201
202    @Override
203    public String getTextBodyForResetPwdEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
204    {
205        if (StringUtils.isNotBlank(_textBodyKeyForResetPwd))
206        {
207            return _i18nUtils.translate(_getI18nizableText(_textBodyKeyForResetPwd, defaultI18nParams), language);
208        }
209        
210        return null;
211    }
212
213    @Override
214    public String getHtmlBodyForResetPwdEmail(Map<String, I18nizableTextParameter> defaultI18nParams, String language)
215    {
216        if (StringUtils.isNotBlank(_htmlBodyKeyForResetPwd))
217        {
218            return _i18nUtils.translate(_getI18nizableText(_htmlBodyKeyForResetPwd, defaultI18nParams), language);
219        }
220        
221        return null;
222    }
223    
224    /**
225     * Get the {@link I18nizableText} from the configured key and i18n parameters
226     * @param fullI18nKey the configured i18n key
227     * @param i18nParams the i18n parameters
228     * @return the i18nizable text
229     */
230    protected I18nizableText _getI18nizableText(String fullI18nKey, Map<String, I18nizableTextParameter> i18nParams)
231    {
232        String catalogue = StringUtils.contains(fullI18nKey, ":") ? StringUtils.substringBefore(fullI18nKey, ":") : "plugin." + _pluginName;
233        String i18nKey = StringUtils.contains(fullI18nKey, ":") ? StringUtils.substringAfter(fullI18nKey, ":") : fullI18nKey;
234        
235        return new I18nizableText(catalogue, i18nKey, i18nParams);
236    }
237}