1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.user
17
18/**
19 * Represents a single user account on the server, including the username, password and home
20 * directory.
21 * <p>
22 * The <code>isValidPassword()</code> method returns true if the specified password matches
23 * the password value configured for this user account. This implementation uses the
24 * <code>isEquals()</code> method to compare passwords.
25 * <p>
26 * If you want to provide a custom comparison, for instance using encrypted passwords, you can
27 * override the <code>comparePassword()</code> method to provide your own custom implementation.
28 * <p>
29 * If the <code>passwordCheckedDuringValidation</code> property is set to false, then the password
30 * value is ignored, and the <code>isValidPassword()</code> method just returns <code<true</code>.
31 */
32class UserAccount {
33
34    String username
35    String password
36    String homeDirectory
37    boolean passwordRequiredForLogin = true
38    boolean passwordCheckedDuringValidation = true
39
40    /**
41     * Return true if the specified password is the correct, valid password for this user account.
42     * This implementation uses standard (case-sensitive) String comparison. Subclasses can provide
43     * custom comparison behavior, for instance using encrypted password values, by overriding this
44     * method.
45     *
46     * @param password - the password to compare against the configured value
47     * @return true if the password is correct and valid
48     *
49     * @throws AssertionError - if the username property is null
50     */
51    boolean isValidPassword(String password) {
52        assert username
53        return passwordCheckedDuringValidation ? comparePassword(password) : true
54    }
55
56    /**
57     * @return the String representation of this object
58     */
59    String toString() {
60        "UserAccount[username=$username; password=$password; homeDirectory=$homeDirectory; " +
61            "passwordRequiredForLogin=$passwordRequiredForLogin]"
62    }
63
64    /**
65     * Return true if the specified password matches the password configured for this user account.
66     * This implementation uses standard (case-sensitive) String comparison. Subclasses can provide
67     * custom comparison behavior, for instance using encrypted password values, by overriding this
68     * method.
69     *
70     * @param password - the password to compare against the configured value
71     * @return true if the passwords match
72     */
73    protected boolean comparePassword(String password) {
74        return password == this.password
75    }
76}