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.configuration.Configurable;
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.core.util.I18nUtils;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * The default user sign up configuration
033 */
034public class DefaultUserSignUpConfiguration implements UserSignUpConfiguration, Configurable, Serviceable
035{
036    private int _tokenValidity;
037    private String _subjectKeyForSignupEmail;
038    private String _textBodyKeyForSignupEmail;
039    private String _htmlBodyKeyForSignupEmail;
040    private String _subjectKeyForResetPwd;
041    private String _textBodyKeyForResetPwd;
042    private String _htmlBodyKeyForResetPwd;
043    
044    private I18nUtils _i18nUtils;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);        
050    }
051    
052    @Override
053    public void configure(Configuration configuration) throws ConfigurationException
054    {
055        _tokenValidity = Integer.parseInt(configuration.getChild("token-validity").getValue(null));
056        _subjectKeyForSignupEmail = configuration.getChild("signup-email-subject").getValue(null);
057        _textBodyKeyForSignupEmail = configuration.getChild("signup-email-text-body").getValue(null);
058        _htmlBodyKeyForSignupEmail = configuration.getChild("signup-email-html-body").getValue(null);
059        _subjectKeyForResetPwd = configuration.getChild("reset-pwd-email-subject").getValue(null);
060        _textBodyKeyForResetPwd = configuration.getChild("reset-pwd-email-text-body").getValue(null);
061        _htmlBodyKeyForResetPwd = configuration.getChild("reset-pwd-email-html-body").getValue(null);
062    }
063    
064    @Override
065    public int getTokenValidity()
066    {
067        return _tokenValidity;
068    }
069
070    @Override
071    public String getSubjectForSignUpEmail (Map<String, I18nizableText> defaultI18nParams , String language)
072    {
073        if (StringUtils.isNotBlank(_subjectKeyForSignupEmail))
074        {
075            I18nizableText i18nSubject = new I18nizableText("plugin.web", _subjectKeyForSignupEmail, defaultI18nParams);
076            return _i18nUtils.translate(i18nSubject, language);
077        }
078        
079        return null;
080    }
081
082    @Override
083    public String getTextBodyForSignUpEmail(Map<String, I18nizableText> defaultI18nParams, String language)
084    {
085        if (StringUtils.isNotBlank(_textBodyKeyForSignupEmail))
086        {
087            I18nizableText i18nSubject = new I18nizableText("plugin.web", _textBodyKeyForSignupEmail, defaultI18nParams);
088            return _i18nUtils.translate(i18nSubject, language);
089        }
090        
091        return null;
092    }
093
094    @Override
095    public String getHtmlBodyForSignUpEmail(Map<String, I18nizableText> defaultI18nParams, String language)
096    {
097        if (StringUtils.isNotBlank(_htmlBodyKeyForSignupEmail))
098        {
099            I18nizableText i18nSubject = new I18nizableText("plugin.web", _htmlBodyKeyForSignupEmail, defaultI18nParams);
100            return _i18nUtils.translate(i18nSubject, language);
101        }
102        
103        return null;
104    }
105    
106    @Override
107    public String getSubjectForResetPwdEmail(Map<String, I18nizableText> defaultI18nParams, String language)
108    {
109        if (StringUtils.isNotBlank(_subjectKeyForResetPwd))
110        {
111            I18nizableText i18nSubject = new I18nizableText("plugin.web", _subjectKeyForResetPwd, defaultI18nParams);
112            return _i18nUtils.translate(i18nSubject, language);
113        }
114        
115        return null;
116    }
117
118    @Override
119    public String getTextBodyForResetPwdEmail(Map<String, I18nizableText> defaultI18nParams, String language)
120    {
121        if (StringUtils.isNotBlank(_textBodyKeyForResetPwd))
122        {
123            I18nizableText i18nSubject = new I18nizableText("plugin.web", _textBodyKeyForResetPwd, defaultI18nParams);
124            return _i18nUtils.translate(i18nSubject, language);
125        }
126        
127        return null;
128    }
129
130    @Override
131    public String getHtmlBodyForResetPwdEmail(Map<String, I18nizableText> defaultI18nParams, String language)
132    {
133        if (StringUtils.isNotBlank(_htmlBodyKeyForResetPwd))
134        {
135            I18nizableText i18nSubject = new I18nizableText("plugin.web", _htmlBodyKeyForResetPwd, defaultI18nParams);
136            return _i18nUtils.translate(i18nSubject, language);
137        }
138        
139        return null;
140    }
141}