19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2008 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.fake.user
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Represents a single user account on the server, including the username, password and home
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * directory.
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <p>
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * The <code>isValidPassword()</code> method returns true if the specified password matches
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * the password value configured for this user account. This implementation uses the
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <code>isEquals()</code> method to compare passwords.
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <p>
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * If you want to provide a custom comparison, for instance using encrypted passwords, you can
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * override the <code>comparePassword()</code> method to provide your own custom implementation.
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <p>
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * If the <code>passwordCheckedDuringValidation</code> property is set to false, then the password
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * value is ignored, and the <code>isValidPassword()</code> method just returns <code<true</code>.
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairclass UserAccount {
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    String username
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    String password
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    String homeDirectory
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    boolean passwordRequiredForLogin = true
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    boolean passwordCheckedDuringValidation = true
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Return true if the specified password is the correct, valid password for this user account.
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * This implementation uses standard (case-sensitive) String comparison. Subclasses can provide
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * custom comparison behavior, for instance using encrypted password values, by overriding this
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * method.
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     *
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param password - the password to compare against the configured value
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @return true if the password is correct and valid
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     *
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @throws AssertionError - if the username property is null
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    boolean isValidPassword(String password) {
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        assert username
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        return passwordCheckedDuringValidation ? comparePassword(password) : true
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @return the String representation of this object
589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    String toString() {
609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        "UserAccount[username=$username; password=$password; homeDirectory=$homeDirectory; " +
619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            "passwordRequiredForLogin=$passwordRequiredForLogin]"
629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    /**
659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * Return true if the specified password matches the password configured for this user account.
669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * This implementation uses standard (case-sensitive) String comparison. Subclasses can provide
679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * custom comparison behavior, for instance using encrypted password values, by overriding this
689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * method.
699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     *
709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @param password - the password to compare against the configured value
719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     * @return true if the passwords match
729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair     */
739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    protected boolean comparePassword(String password) {
749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        return password == this.password
759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    }
769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair}