Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/gui/login/LoginModel.java


1   /*
2    * LoginModel.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:48 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.gui.login;
12  
13  import java.awt.Frame;
14  
15  import com.flexstor.common.awt.dialogs.MessageBox;
16  import com.flexstor.common.awt.statusdisplay.StatusDispMgr;
17  import com.flexstor.common.gateway.UserGateway;
18  import com.flexstor.common.gateway.exceptions.TransactionFailedException;
19  import com.flexstor.common.resources.Resources;
20  import com.flexstor.common.util.Diagnostic;
21  
22  public class LoginModel
23  {
24     private LoginUserData     loginData;
25     private boolean             bAutoLogin;
26     private boolean             bAlowChangePW;
27     
28     public static final int USERID_LENGTH            = 25;
29     public static final int PASSWORD_LENGTH          = 12;
30  
31     public static final int STATE_SHOW_LOGIN_GUI     = 0;
32     public static final int STATE_READY_TO_LOGIN     = 1;
33     public static final int STATE_LOGGED_IN          = 2;
34     public static final int STATE_LOG_IN_FAILED      = 3;
35     public static final int STATE_LOGIN_CANCELED     = 4;
36     public static final int STATE_SHOW_CHANGE_PW_GUI = 5;
37     public static final int STATE_READY_TO_CHANGE_PW = 6;
38     public static final int STATE_PASSWORD_CHANGED   = 7;
39     public static final int STATE_CHANGE_PW_FAILED   = 8;
40     public static final int STATE_CHANGE_PW_CANCELED = 9;
41     
42     public LoginModel ( boolean bAutoLogin, boolean bAlowChangePW, String sUserID, String sPassword )
43     {
44        // Only allow auto login for a valid user id and password
45        if ( sUserID == null || sUserID.equals("") || sPassword == null || sPassword.equals("") )
46           bAutoLogin = false;
47  
48        // Prevent garbage user id or password if not doing auto login
49        if ( !bAutoLogin )
50        {
51           sUserID    = "";
52           sPassword  = "";
53        }
54        
55        this.bAutoLogin    = bAutoLogin;
56        this.bAlowChangePW = bAlowChangePW;
57        loginData          = new LoginUserData ( sUserID, sPassword );
58     }
59     
60     public String getUserID ( )
61     {
62        return loginData.getUserID();
63     }
64     
65     public String getPassword()
66     {
67        return loginData.getPassword();
68     }
69     
70     // returns true if login worked.
71     public boolean attemptLogin ( )
72     {
73        Frame internalFrame = new Frame();
74        internalFrame.setBounds ( -1000, -1000, 0, 0 );
75        internalFrame.setVisible ( true );
76        
77        int nState = STATE_SHOW_LOGIN_GUI;
78  
79        if ( bAutoLogin )
80        {
81           Diagnostic.trace ( 1, "Auto login mode." );
82           internalFrame.setTitle ( Resources.get(5333) );
83  
84           if ( performLogin() == STATE_LOGGED_IN )
85           {
86              internalFrame.setVisible ( false );
87              return true;
88           }
89  
90           MessageBox.showMessageDialog ( internalFrame, 5014 );
91        }
92        
93        while ( true )
94        {
95           switch ( nState )
96           {
97              case STATE_SHOW_LOGIN_GUI:
98                 LoginDialog loginDlg = new LoginDialog ( internalFrame, Resources.get( 5012 ), bAlowChangePW );
99                 nState = loginDlg.doLogin(loginData);
100                break;
101          
102             case STATE_READY_TO_LOGIN:
103                nState = performLogin();
104                break;
105          
106             case STATE_LOGGED_IN:
107                internalFrame.setVisible ( false );
108                return true;
109          
110             case STATE_LOG_IN_FAILED:
111                nState = STATE_SHOW_LOGIN_GUI;
112                MessageBox.showMessageDialog ( internalFrame, 5015 );
113                break;
114          
115             case STATE_LOGIN_CANCELED:
116                internalFrame.setVisible ( false );
117                return false;
118 
119             case STATE_SHOW_CHANGE_PW_GUI:
120                ChangePwDialog cpDlg = new ChangePwDialog ( internalFrame, Resources.get( 5013 ) );
121                nState = cpDlg.doChangePassword ( loginData );
122                break;
123                
124             case STATE_READY_TO_CHANGE_PW:
125                nState = performChangePassword();
126                break;
127                
128             case STATE_PASSWORD_CHANGED:
129                MessageBox.showMessageDialog ( internalFrame, 5017 );
130                internalFrame.setVisible ( false );
131                return true;
132                
133             case STATE_CHANGE_PW_FAILED:
134                MessageBox.showMessageDialog ( internalFrame, 5016 );
135                nState = STATE_SHOW_CHANGE_PW_GUI;
136                break;
137                
138             case STATE_CHANGE_PW_CANCELED:
139                nState = STATE_SHOW_LOGIN_GUI;
140                break;
141          }
142       }
143    }
144    /**
145     * For client/Admin log-in.
146     * Rules :
147     * 1. It should not be null.
148     * 2. It should not be an emptry string.
149     * 3. It should not have leading, traling  spaces.
150     * 4. It should not have any spaces in between.
151     * 5. Is should have digits and characters only. No special characters.
152     */
153    public static boolean isValidPassword ( String strPassword )
154    {
155      // In the first place it should be non-null and non-empty-string.
156      // Rule 1 and 2
157      if ( ( strPassword == null ) ||
158           ( strPassword.trim().length() == 0 ) )
159      return false;
160 
161      // Other rules (3,4 and 5)
162      for ( int i=0; i<strPassword.length(); i++ )
163      {
164         if ( ! Character.isLetterOrDigit( strPassword.charAt( i ) ) )
165           return false;
166      }
167      return true;
168    }
169 
170 
171    private int performLogin ( )
172    {
173       UserGateway gateway = null;
174 
175       try
176       {
177          StatusDispMgr.setMessage ( Resources.get( 5009 ) );
178          gateway = new UserGateway();
179          gateway.connect ( loginData.getUserID(), loginData.getPassword() );
180          return STATE_LOGGED_IN;
181       }
182       catch ( TransactionFailedException e )
183       {
184          return STATE_LOG_IN_FAILED;
185       }
186       finally
187       {
188          if ( gateway != null )
189             gateway.dispose();
190 
191          StatusDispMgr.setMessage ( "" );
192       }
193    }
194 
195    private int performChangePassword ( )
196    {
197       UserGateway  gateway  = null;
198       
199       try
200       {
201          StatusDispMgr.setMessage ( Resources.get( 5010 ) );
202          gateway = new UserGateway();
203          gateway.connect ( loginData.getUserID());
204          gateway.changePassword (loginData.getUserID(),loginData.getPassword(),loginData.getNewPassword() );
205          return STATE_PASSWORD_CHANGED;
206       }
207       catch ( TransactionFailedException e )
208       {
209          return STATE_CHANGE_PW_FAILED;
210       }
211       finally
212       {
213          if ( gateway != null )
214             gateway.dispose();
215             
216          StatusDispMgr.setMessage ( "" );
217       }
218    }
219 }