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