001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.exception;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.util.Date;
020    import java.util.List;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Preston Crary
025     */
026    public class UserPasswordException extends PortalException {
027    
028            @Deprecated
029            public static final int PASSWORD_ALREADY_USED = 1;
030    
031            @Deprecated
032            public static final int PASSWORD_CONTAINS_TRIVIAL_WORDS = 2;
033    
034            @Deprecated
035            public static final int PASSWORD_INVALID = 3;
036    
037            @Deprecated
038            public static final int PASSWORD_LENGTH = 4;
039    
040            @Deprecated
041            public static final int PASSWORD_NOT_CHANGEABLE = 5;
042    
043            @Deprecated
044            public static final int PASSWORD_SAME_AS_CURRENT = 6;
045    
046            @Deprecated
047            public static final int PASSWORD_TOO_TRIVIAL = 8;
048    
049            @Deprecated
050            public static final int PASSWORD_TOO_YOUNG = 9;
051    
052            @Deprecated
053            public static final int PASSWORDS_DO_NOT_MATCH = 10;
054    
055            /**
056             * @deprecated As of 7.0.0, replaced by the inner classes
057             */
058            @Deprecated
059            public UserPasswordException(int type) {
060                    _type = type;
061            }
062    
063            /**
064             * @deprecated As of 7.0.0, replaced by the inner classes
065             */
066            @Deprecated
067            public int getType() {
068                    return _type;
069            }
070    
071            public static class MustBeLonger extends UserPasswordException {
072    
073                    public MustBeLonger(long userId, int minLength) {
074                            super(
075                                    String.format(
076                                            "Password for user %s must be at least %s characters",
077                                            userId, minLength),
078                                    PASSWORD_LENGTH);
079    
080                            this.minLength = minLength;
081                            this.userId = userId;
082                    }
083    
084                    public final int minLength;
085                    public final long userId;
086    
087            }
088    
089            public static class MustComplyWithModelListeners
090                    extends UserPasswordException {
091    
092                    public MustComplyWithModelListeners(
093                            long userId, ModelListenerException modelListenerException) {
094    
095                            super(
096                                    String.format(
097                                            "Password must comply with model listeners: " +
098                                                    modelListenerException.getMessage()),
099                                    PASSWORD_INVALID);
100    
101                            this.userId = userId;
102                            this.modelListenerException = modelListenerException;
103                    }
104    
105                    public final ModelListenerException modelListenerException;
106                    public final long userId;
107    
108            }
109    
110            public static class MustComplyWithRegex extends UserPasswordException {
111    
112                    public MustComplyWithRegex(long userId, String regex) {
113                            super(
114                                    String.format("Password must comply with regex: " + regex),
115                                    PASSWORD_INVALID);
116    
117                            this.regex = regex;
118                            this.userId = userId;
119                    }
120    
121                    public final String regex;
122                    public final long userId;
123    
124            }
125    
126            public static class MustMatch extends UserPasswordException {
127    
128                    public MustMatch(long userId) {
129                            super(
130                                    String.format("Passwords for user %s must match", userId),
131                                    PASSWORDS_DO_NOT_MATCH);
132    
133                            this.userId = userId;
134                    }
135    
136                    public final long userId;
137    
138            }
139    
140            public static class MustMatchCurrentPassword extends UserPasswordException {
141    
142                    public MustMatchCurrentPassword(long userId) {
143                            super(
144                                    String.format(
145                                            "Password for user %s does not match the current password",
146                                            userId),
147                                    PASSWORD_INVALID);
148    
149                            this.userId = userId;
150                    }
151    
152                    public final long userId;
153    
154            }
155    
156            public static class MustNotBeChanged extends UserPasswordException {
157    
158                    public MustNotBeChanged(long userId) {
159                            super(
160                                    String.format(
161                                            "Password for user %s must not be changed under the " +
162                                                    "current password policy",
163                                            userId),
164                                    PASSWORD_NOT_CHANGEABLE);
165    
166                            this.userId = userId;
167                    }
168    
169                    public final long userId;
170    
171            }
172    
173            public static class MustNotBeChangedYet extends UserPasswordException {
174    
175                    public MustNotBeChangedYet(long userId, Date changeableDate) {
176                            super(
177                                    String.format(
178                                            "Password for user %s must not be changed until %s", userId,
179                                            changeableDate),
180                                    PASSWORD_TOO_YOUNG);
181    
182                            this.userId = userId;
183                            this.changeableDate = changeableDate;
184                    }
185    
186                    public final Date changeableDate;
187                    public long userId;
188    
189            }
190    
191            public static class MustNotBeEqualToCurrent extends UserPasswordException {
192    
193                    public MustNotBeEqualToCurrent(long userId) {
194                            super(
195                                    String.format(
196                                            "Password for user %s must not be equal to their current " +
197                                                    "password",
198                                            userId),
199                                    PASSWORD_SAME_AS_CURRENT);
200    
201                            this.userId = userId;
202                    }
203    
204                    public final long userId;
205    
206            }
207    
208            public static class MustNotBeNull extends UserPasswordException {
209    
210                    public MustNotBeNull(long userId) {
211                            super(
212                                    String.format("Password for user %s must not be null", userId),
213                                    PASSWORD_INVALID);
214    
215                            this.userId = userId;
216                    }
217    
218                    public long userId;
219    
220            }
221    
222            public static class MustNotBeRecentlyUsed extends UserPasswordException {
223    
224                    public MustNotBeRecentlyUsed(long userId) {
225                            super(
226                                    String.format(
227                                            "Password for user %s was used too recently", userId),
228                                    PASSWORD_ALREADY_USED);
229    
230                            this.userId = userId;
231                    }
232    
233                    public long userId;
234    
235            }
236    
237            public static class MustNotBeTrivial extends UserPasswordException {
238    
239                    public MustNotBeTrivial(long userId) {
240                            super(
241                                    String.format(
242                                            "Password for user %s must not be too trivial", userId),
243                                    PASSWORD_TOO_TRIVIAL);
244    
245                            this.userId = userId;
246                    }
247    
248                    public long userId;
249    
250            }
251    
252            public static class MustNotContainDictionaryWords
253                    extends UserPasswordException {
254    
255                    public MustNotContainDictionaryWords(
256                            long userId, List<String> dictionaryWords) {
257    
258                            super(
259                                    String.format(
260                                            "Password for user %s must not contain dictionary words " +
261                                                    "such as: %s",
262                                            userId, _getDictionaryWordsString(dictionaryWords)),
263                                    PASSWORD_CONTAINS_TRIVIAL_WORDS);
264    
265                            this.userId = userId;
266                            this.dictionaryWords = dictionaryWords;
267                    }
268    
269                    public final List<String> dictionaryWords;
270                    public long userId;
271    
272            }
273    
274            private static String _getDictionaryWordsString(
275                    List<String> dictionaryWords) {
276    
277                    if (dictionaryWords.size() <= 10) {
278                            return dictionaryWords.toString();
279                    }
280    
281                    List<String> sampleDictionaryWords = dictionaryWords.subList(0, 10);
282    
283                    return sampleDictionaryWords.toString() + StringPool.SPACE +
284                            StringPool.TRIPLE_PERIOD;
285            }
286    
287            private UserPasswordException(String message, int type) {
288                    super(message);
289    
290                    _type = type;
291            }
292    
293            private final int _type;
294    
295    }