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