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;
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", userId),
163                                    PASSWORD_NOT_CHANGEABLE);
164    
165                            this.userId = userId;
166                    }
167    
168                    public final long userId;
169    
170            }
171    
172            public static class MustNotBeChangedYet extends UserPasswordException {
173    
174                    public MustNotBeChangedYet(long userId, Date changeableDate) {
175                            super(
176                                    String.format(
177                                            "Password for user %s must not be changed until %s", userId,
178                                            changeableDate),
179                                    PASSWORD_TOO_YOUNG);
180    
181                            this.userId = userId;
182                            this.changeableDate = changeableDate;
183                    }
184    
185                    public final Date changeableDate;
186                    public long userId;
187    
188            }
189    
190            public static class MustNotBeEqualToCurrent extends UserPasswordException {
191    
192                    public MustNotBeEqualToCurrent(long userId) {
193                            super(
194                                    String.format(
195                                            "Password for user %s must not be equal to their current " +
196                                                    "password",
197                                            userId),
198                                    PASSWORD_SAME_AS_CURRENT);
199    
200                            this.userId = userId;
201                    }
202    
203                    public final long userId;
204    
205            }
206    
207            public static class MustNotBeNull extends UserPasswordException {
208    
209                    public MustNotBeNull(long userId) {
210                            super(
211                                    String.format("Password for user %s must not be null", userId),
212                                    PASSWORD_INVALID);
213    
214                            this.userId = userId;
215                    }
216    
217                    public long userId;
218    
219            }
220    
221            public static class MustNotBeRecentlyUsed extends UserPasswordException {
222    
223                    public MustNotBeRecentlyUsed(long userId) {
224                            super(
225                                    String.format(
226                                            "Password for user %s was used too recently",
227                                                    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    }