001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016
017package org.forgerock.openam.selfservice.config.beans;
018
019import org.forgerock.openam.sm.config.ConfigAttribute;
020import org.forgerock.openam.sm.config.ConfigSource;
021import org.forgerock.util.Reject;
022
023import java.util.HashMap;
024import java.util.Locale;
025import java.util.Map;
026
027/**
028 * Represents forgotten password console configuration.
029 *
030 * @supported.all.api
031 * @since 13.0.0
032 */
033public final class UserRegistrationConsoleConfig extends CommonConsoleConfig {
034
035    private final String emailVerificationUrl;
036    private final int minimumAnswersToDefine;
037    private final boolean enabled;
038    private final String configProviderClass;
039    private final long tokenExpiry;
040    private final boolean emailEnabled;
041    private final Map<Locale, String> subjectTranslations;
042    private final Map<Locale, String> messageTranslations;
043    private final boolean captchaEnabled;
044    private final boolean kbaEnabled;
045
046    private UserRegistrationConsoleConfig(UserRegistrationBuilder builder) {
047        super(builder);
048        emailVerificationUrl = builder.emailVerificationUrl;
049        minimumAnswersToDefine = builder.minimumAnswersToDefine;
050        configProviderClass = builder.configProviderClass;
051        enabled = builder.enabled;
052        emailEnabled = builder.emailEnabled;
053        tokenExpiry = builder.tokenExpiry;
054        captchaEnabled = builder.captchaEnabled;
055        kbaEnabled = builder.kbaEnabled;
056        subjectTranslations = builder.subjectTranslations;
057        messageTranslations = builder.messageTranslations;
058    }
059
060    @Override
061    public String getConfigProviderClass() {
062        return configProviderClass;
063    }
064
065    /**
066     * Whether the service is enabled.
067     *
068     * @return whether the service is enabled
069     */
070    public boolean isEnabled() {
071        return enabled;
072    }
073
074    /**
075     * Whether email verification is enabled.
076     *
077     * @return whether email verification is enabled
078     */
079    public boolean isEmailEnabled() {
080        return emailEnabled;
081    }
082
083    /**
084     * Gets the token expiry time in seconds.
085     *
086     * @return the token expiry time
087     */
088    public long getTokenExpiry() {
089        return tokenExpiry;
090    }
091
092    /**
093     * Whether the captcha stage is enabled.
094     *
095     * @return whether the captcha stage is enabled
096     */
097    public boolean isCaptchaEnabled() {
098        return captchaEnabled;
099    }
100
101    /**
102     * Whether the KBA stage is enabled.
103     *
104     * @return whether the KBA stage is enabled
105     */
106    public boolean isKbaEnabled() {
107        return kbaEnabled;
108    }
109
110    /**
111     * Gets the map of locales to subject strings.
112     *
113     * @return the map of locales to subject text strings.
114     */
115    public Map<Locale, String> getSubjectTranslations() {
116        return subjectTranslations;
117    }
118
119    /**
120     * Gets the map of locales to email body text strings.
121     *
122     * @return the map of locales to email body text strings.
123     */
124    public Map<Locale, String> getMessageTranslations() {
125        return messageTranslations;
126    }
127
128    /**
129     * Gets the verification Url to be sent with the email body.
130     *
131     * @return email verification Url
132     */
133    public String getEmailVerificationUrl() {
134        return emailVerificationUrl;
135    }
136
137    /**
138     * Get the minimum count of answers to define.
139     *
140     * @return minimum count
141     */
142    public int getMinimumAnswersToDefine() {
143        return minimumAnswersToDefine;
144    }
145
146    /**
147     * Builder for {@link UserRegistrationConsoleConfig}.
148     */
149    @ConfigSource({"MailServer", "selfService"})
150    public static final class UserRegistrationBuilder
151            extends CommonConsoleConfigBuilder<UserRegistrationConsoleConfig> {
152
153        private String emailVerificationUrl;
154        private int minimumAnswersToDefine;
155        private boolean enabled;
156        private String configProviderClass;
157        private long tokenExpiry;
158        private boolean emailEnabled;
159        private final Map<Locale, String> subjectTranslations;
160        private final Map<Locale, String> messageTranslations;
161        private boolean captchaEnabled;
162        private boolean kbaEnabled;
163
164        /**
165         * Constructs a new builder.
166         */
167        public UserRegistrationBuilder() {
168            subjectTranslations = new HashMap<>();
169            messageTranslations = new HashMap<>();
170        }
171
172        /**
173         * Sets whether the service is enabled.
174         *
175         * @param enabled
176         *         whether the service is enabled
177         */
178        @ConfigAttribute("selfServiceUserRegistrationEnabled")
179        public void setEnabled(boolean enabled) {
180            this.enabled = enabled;
181        }
182
183        /**
184         * Sets the config provider class.
185         *
186         * @param configProviderClass
187         *         config provider class
188         */
189        @ConfigAttribute("selfServiceUserRegistrationServiceConfigClass")
190        public void setConfigProviderClass(String configProviderClass) {
191            this.configProviderClass = configProviderClass;
192        }
193
194        /**
195         * Sets the token expiry time.
196         *
197         * @param tokenExpiry
198         *         token expiry time
199         */
200        @ConfigAttribute("selfServiceUserRegistrationTokenTTL")
201        public void setTokenExpiry(long tokenExpiry) {
202            this.tokenExpiry = tokenExpiry;
203        }
204
205        /**
206         * Sets whether email is enabled.
207         *
208         * @param emailEnabled
209         *         whether email is enabled
210         */
211        @ConfigAttribute("selfServiceUserRegistrationEmailVerificationEnabled")
212        public void setEmailEnabled(boolean emailEnabled) {
213            this.emailEnabled = emailEnabled;
214        }
215
216        /**
217         * Sets the email subject translations.
218         *
219         * @param subjectTranslations
220         *         email subject translations
221         */
222        @ConfigAttribute(value = "selfServiceUserRegistrationEmailSubject",
223                transformer = LocaleMessageTransformer.class)
224        public void setSubjectTranslations(Map<Locale, String> subjectTranslations) {
225            this.subjectTranslations.putAll(subjectTranslations);
226        }
227
228        /**
229         * Sets the email body translations.
230         *
231         * @param messageTranslations
232         *         email body translations
233         */
234        @ConfigAttribute(value = "selfServiceUserRegistrationEmailBody",
235                transformer = LocaleMessageTransformer.class)
236        public void setMessageTranslations(Map<Locale, String> messageTranslations) {
237            this.messageTranslations.putAll(messageTranslations);
238        }
239
240        /**
241         * Sets whether captcha is enabled.
242         *
243         * @param captchaEnabled
244         *         whether captcha is enabled
245         */
246        @ConfigAttribute("selfServiceUserRegistrationCaptchaEnabled")
247        public void setCaptchaEnabled(boolean captchaEnabled) {
248            this.captchaEnabled = captchaEnabled;
249        }
250
251        /**
252         * Sets whether KBA is enabled.
253         *
254         * @param kbaEnabled
255         *         whether KBA is enabled
256         */
257        @ConfigAttribute("selfServiceUserRegistrationKbaEnabled")
258        public void setKbaEnabled(boolean kbaEnabled) {
259            this.kbaEnabled = kbaEnabled;
260        }
261
262        /**
263         * Sets the email verification URL.
264         *
265         * @param emailVerificationUrl
266         *         email verification URL
267         */
268        @ConfigAttribute("selfServiceUserRegistrationConfirmationUrl")
269        public void setEmailVerificationUrl(String emailVerificationUrl) {
270            this.emailVerificationUrl = emailVerificationUrl;
271        }
272
273        /**
274         * Sets the minimum number of answers to be defined.
275         *
276         * @param minimumAnswersToDefine
277         *         minimum number of answers to be defined
278         */
279        @ConfigAttribute("selfServiceMinimumAnswersToDefine")
280        public void setMinimumAnswersToDefine(int minimumAnswersToDefine) {
281            this.minimumAnswersToDefine = minimumAnswersToDefine;
282        }
283
284        @Override
285        boolean isCaptchaEnabled() {
286            return captchaEnabled;
287        }
288
289        @Override
290        boolean isKbaEnabled() {
291            return kbaEnabled;
292        }
293
294        @Override
295        UserRegistrationConsoleConfig internalBuild() {
296            Reject.ifNull(configProviderClass, "Config provider class name required");
297            Reject.ifFalse(tokenExpiry > 0, "Token expiry must be greater than zero");
298
299            if (emailEnabled) {
300                Reject.ifNull(emailVerificationUrl, "Email verification Url is required");
301                Reject.ifTrue(subjectTranslations.isEmpty(), "Subject translations are required");
302                Reject.ifTrue(messageTranslations.isEmpty(), "Message translations are required");
303            }
304
305            if (kbaEnabled) {
306                Reject.ifFalse(minimumAnswersToDefine > 0, "Minimum answers to be defined must be greater than 0");
307            }
308
309            return new UserRegistrationConsoleConfig(this);
310        }
311
312    }
313
314}