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-2016 ForgeRock AS.
015 */
016
017package org.forgerock.openam.selfservice.config.beans;
018
019import java.util.HashMap;
020import java.util.Locale;
021import java.util.Map;
022
023import org.forgerock.openam.sm.config.ConfigAttribute;
024import org.forgerock.openam.sm.config.ConfigSource;
025import org.forgerock.util.Reject;
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    private final RegistrationDestination userRegistrationDestination;
046
047    private UserRegistrationConsoleConfig(UserRegistrationBuilder builder) {
048        super(builder);
049        emailVerificationUrl = builder.emailVerificationUrl;
050        minimumAnswersToDefine = builder.minimumAnswersToDefine;
051        configProviderClass = builder.configProviderClass;
052        enabled = builder.enabled;
053        emailEnabled = builder.emailEnabled;
054        tokenExpiry = builder.tokenExpiry;
055        captchaEnabled = builder.captchaEnabled;
056        kbaEnabled = builder.kbaEnabled;
057        subjectTranslations = builder.subjectTranslations;
058        messageTranslations = builder.messageTranslations;
059        userRegistrationDestination = builder.userRegistrationDestination;
060    }
061
062    @Override
063    public String getConfigProviderClass() {
064        return configProviderClass;
065    }
066
067    /**
068     * Whether the service is enabled.
069     *
070     * @return whether the service is enabled
071     */
072    public boolean isEnabled() {
073        return enabled;
074    }
075
076    /**
077     * Whether email verification is enabled.
078     *
079     * @return whether email verification is enabled
080     */
081    public boolean isEmailEnabled() {
082        return emailEnabled;
083    }
084
085    /**
086     * Gets the token expiry time in seconds.
087     *
088     * @return the token expiry time
089     */
090    public long getTokenExpiry() {
091        return tokenExpiry;
092    }
093
094    /**
095     * Whether the captcha stage is enabled.
096     *
097     * @return whether the captcha stage is enabled
098     */
099    public boolean isCaptchaEnabled() {
100        return captchaEnabled;
101    }
102
103    /**
104     * Whether the KBA stage is enabled.
105     *
106     * @return whether the KBA stage is enabled
107     */
108    public boolean isKbaEnabled() {
109        return kbaEnabled;
110    }
111
112    /**
113     * Gets the map of locales to subject strings.
114     *
115     * @return the map of locales to subject text strings.
116     */
117    public Map<Locale, String> getSubjectTranslations() {
118        return subjectTranslations;
119    }
120
121    /**
122     * Gets the map of locales to email body text strings.
123     *
124     * @return the map of locales to email body text strings.
125     */
126    public Map<Locale, String> getMessageTranslations() {
127        return messageTranslations;
128    }
129
130    /**
131     * Gets the verification Url to be sent with the email body.
132     *
133     * @return email verification Url
134     */
135    public String getEmailVerificationUrl() {
136        return emailVerificationUrl;
137    }
138
139    /**
140     * Get the minimum count of answers to define.
141     *
142     * @return minimum count
143     */
144    public int getMinimumAnswersToDefine() {
145        return minimumAnswersToDefine;
146    }
147
148    /**
149     * Gets the user registration destination.
150     *
151     * @return user registration destination
152     */
153    public RegistrationDestination getUserRegistrationDestination() {
154        return userRegistrationDestination;
155    }
156
157    /**
158     * Builder for {@link UserRegistrationConsoleConfig}.
159     */
160    @ConfigSource({"MailServer", "selfService"})
161    public static final class UserRegistrationBuilder
162            extends CommonConsoleConfigBuilder<UserRegistrationConsoleConfig> {
163
164        private String emailVerificationUrl;
165        private int minimumAnswersToDefine;
166        private boolean enabled;
167        private String configProviderClass;
168        private long tokenExpiry;
169        private boolean emailEnabled;
170        private final Map<Locale, String> subjectTranslations;
171        private final Map<Locale, String> messageTranslations;
172        private boolean captchaEnabled;
173        private boolean kbaEnabled;
174        private RegistrationDestination userRegistrationDestination;
175
176        /**
177         * Constructs a new builder.
178         */
179        public UserRegistrationBuilder() {
180            subjectTranslations = new HashMap<>();
181            messageTranslations = new HashMap<>();
182        }
183
184        /**
185         * Sets whether the service is enabled.
186         *
187         * @param enabled
188         *         whether the service is enabled
189         */
190        @ConfigAttribute("selfServiceUserRegistrationEnabled")
191        public void setEnabled(boolean enabled) {
192            this.enabled = enabled;
193        }
194
195        /**
196         * Sets the config provider class.
197         *
198         * @param configProviderClass
199         *         config provider class
200         */
201        @ConfigAttribute("selfServiceUserRegistrationServiceConfigClass")
202        public void setConfigProviderClass(String configProviderClass) {
203            this.configProviderClass = configProviderClass;
204        }
205
206        /**
207         * Sets the token expiry time.
208         *
209         * @param tokenExpiry
210         *         token expiry time
211         */
212        @ConfigAttribute("selfServiceUserRegistrationTokenTTL")
213        public void setTokenExpiry(long tokenExpiry) {
214            this.tokenExpiry = tokenExpiry;
215        }
216
217        /**
218         * Sets whether email is enabled.
219         *
220         * @param emailEnabled
221         *         whether email is enabled
222         */
223        @ConfigAttribute("selfServiceUserRegistrationEmailVerificationEnabled")
224        public void setEmailEnabled(boolean emailEnabled) {
225            this.emailEnabled = emailEnabled;
226        }
227
228        /**
229         * Sets the email subject translations.
230         *
231         * @param subjectTranslations
232         *         email subject translations
233         */
234        @ConfigAttribute(value = "selfServiceUserRegistrationEmailSubject",
235                transformer = LocaleMessageTransformer.class)
236        public void setSubjectTranslations(Map<Locale, String> subjectTranslations) {
237            this.subjectTranslations.putAll(subjectTranslations);
238        }
239
240        /**
241         * Sets the email body translations.
242         *
243         * @param messageTranslations
244         *         email body translations
245         */
246        @ConfigAttribute(value = "selfServiceUserRegistrationEmailBody",
247                transformer = LocaleMessageTransformer.class)
248        public void setMessageTranslations(Map<Locale, String> messageTranslations) {
249            this.messageTranslations.putAll(messageTranslations);
250        }
251
252        /**
253         * Sets whether captcha is enabled.
254         *
255         * @param captchaEnabled
256         *         whether captcha is enabled
257         */
258        @ConfigAttribute("selfServiceUserRegistrationCaptchaEnabled")
259        public void setCaptchaEnabled(boolean captchaEnabled) {
260            this.captchaEnabled = captchaEnabled;
261        }
262
263        /**
264         * Sets whether KBA is enabled.
265         *
266         * @param kbaEnabled
267         *         whether KBA is enabled
268         */
269        @ConfigAttribute("selfServiceUserRegistrationKbaEnabled")
270        public void setKbaEnabled(boolean kbaEnabled) {
271            this.kbaEnabled = kbaEnabled;
272        }
273
274        /**
275         * Sets the email verification URL.
276         *
277         * @param emailVerificationUrl
278         *         email verification URL
279         */
280        @ConfigAttribute("selfServiceUserRegistrationConfirmationUrl")
281        public void setEmailVerificationUrl(String emailVerificationUrl) {
282            this.emailVerificationUrl = emailVerificationUrl;
283        }
284
285        /**
286         * Sets the minimum number of answers to be defined.
287         *
288         * @param minimumAnswersToDefine
289         *         minimum number of answers to be defined
290         */
291        @ConfigAttribute("selfServiceMinimumAnswersToDefine")
292        public void setMinimumAnswersToDefine(int minimumAnswersToDefine) {
293            this.minimumAnswersToDefine = minimumAnswersToDefine;
294        }
295
296        /**
297         * Sets the user registration destination.
298         *
299         * @param userRegistrationDestination
300         *         user registration destination
301         */
302        @ConfigAttribute(value = "selfServiceUserRegistrationDestination", transformer = DestinationTransformer.class)
303        public void setUserRegistrationDestination(RegistrationDestination userRegistrationDestination) {
304            this.userRegistrationDestination = userRegistrationDestination;
305        }
306
307        @Override
308        boolean isCaptchaEnabled() {
309            return captchaEnabled;
310        }
311
312        @Override
313        boolean isKbaEnabled() {
314            return kbaEnabled;
315        }
316
317        @Override
318        UserRegistrationConsoleConfig internalBuild() {
319            Reject.ifNull(configProviderClass, "Config provider class name required");
320            Reject.ifFalse(tokenExpiry > 0, "Token expiry must be greater than zero");
321
322            if (emailEnabled) {
323                Reject.ifNull(emailVerificationUrl, "Email verification Url is required");
324                Reject.ifTrue(subjectTranslations.isEmpty(), "Subject translations are required");
325                Reject.ifTrue(messageTranslations.isEmpty(), "Message translations are required");
326            }
327
328            if (kbaEnabled) {
329                Reject.ifFalse(minimumAnswersToDefine > 0, "Minimum answers to be defined must be greater than 0");
330            }
331
332            return new UserRegistrationConsoleConfig(this);
333        }
334
335    }
336
337}