View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.simpleimpl.file;
19  
20  import org.waarp.common.logging.WaarpLogger;
21  import org.waarp.common.logging.WaarpLoggerFactory;
22  
23  /**
24   * Simple Authentication based on a previously load XML file. Not to be used in production!
25   * 
26   * @author Frederic Bregier
27   * 
28   */
29  public class SimpleAuth {
30      /**
31       * Internal Logger
32       */
33      private static final WaarpLogger logger = WaarpLoggerFactory
34              .getLogger(SimpleAuth.class);
35  
36      /**
37       * User name
38       */
39      private String user = null;
40  
41      /**
42       * Password
43       */
44      private String password = null;
45  
46      /**
47       * Multiple accounts
48       */
49      private String[] accounts = null;
50  
51      /**
52       * Is the current user an administrator (which can shutdown or change bandwidth limitation)
53       */
54      private boolean isAdmin = false;
55  
56      /**
57       * @param user
58       * @param password
59       * @param accounts
60       */
61      public SimpleAuth(String user, String password, String[] accounts) {
62          this.user = user;
63          this.password = password;
64          this.accounts = accounts;
65      }
66  
67      /**
68       * Is the given password a valid one
69       * 
70       * @param newpassword
71       * @return True if the password is valid (or any password is valid)
72       */
73      public boolean isPasswordValid(String newpassword) {
74          if (password == null) {
75              return true;
76          }
77          if (newpassword == null) {
78              return false;
79          }
80          return password.equals(newpassword);
81      }
82  
83      /**
84       * Is the given account a valid one
85       * 
86       * @param account
87       * @return True if the account is valid (or any account is valid)
88       */
89      public boolean isAccountValid(String account) {
90          if (accounts == null) {
91              logger.debug("No account needed");
92              return true;
93          }
94          if (account == null) {
95              logger.debug("No account given");
96              return false;
97          }
98          for (String acct : accounts) {
99              if (acct.equals(account)) {
100                 logger.debug("Account found");
101                 return true;
102             }
103         }
104         logger.debug("No account found");
105         return false;
106     }
107 
108     /**
109      * 
110      * @param isAdmin
111      *            True if the user should be an administrator
112      */
113     public void setAdmin(boolean isAdmin) {
114         this.isAdmin = isAdmin;
115     }
116 
117     /**
118      * @return the isAdmin
119      */
120     public boolean isAdmin() {
121         return isAdmin;
122     }
123 
124     /**
125      * @return the user
126      */
127     public String getUser() {
128         return user;
129     }
130 }