View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.security;
20  
21  import org.caleigo.core.*;
22  
23  /*** A login manager that uses an IDataSource to check the validity of user logins.
24   *
25   * @author  Mattias Hagstrand
26   * @version 1.00
27   *
28   *//*
29    *
30    * WHEN        WHO               WHY & WHAT
31    * -----------------------------------------------------------------------------
32    * 2002-10-16  Mattias Hagstrand    Creation
33    */
34  public class DataSourceLoginHandler implements ILoginHandler
35  {
36      // Data members ------------------------------------------------------------
37      protected IEntityDescriptor mEntityDescriptor;
38      protected IFieldDescriptor mUserIDFieldDescriptor;
39      protected IFieldDescriptor mPasswordFieldDescriptor;
40      protected IFieldDescriptor mGroupFieldDescriptor;
41      
42      // Constructors ------------------------------------------------------------
43      
44      /*** Creates a DataSourceLoginManager and initializes it with a IUserLoginHandler.
45       *
46       * @param entityDescriptor           the IEntityDescriptor that is used to get user information.
47       * @param userIDFieldDescriptor      the IFieldDescriptor that is used to get the user id.
48       * @param passwordFieldDescriptor    the IFieldDescriptor that is used to get the password.
49       * @param passwordFieldDescriptor    the IFieldDescriptor that is used to get the group, may be <code>null</code>.
50       */
51      public DataSourceLoginHandler(IEntityDescriptor entityDescriptor,
52                                    IFieldDescriptor userIDFieldDescriptor,
53                                    IFieldDescriptor passwordFieldDescriptor,
54                                    IFieldDescriptor groupFieldDescriptor)
55      {
56          if (userIDFieldDescriptor.getDataType() != DataType.STRING)
57              throw new IllegalArgumentException("UserID field must be of the type STRING");
58          if (passwordFieldDescriptor.getDataType() != DataType.STRING)
59              throw new IllegalArgumentException("Password field must be of the type STRING");
60          if (userIDFieldDescriptor.getEntityDescriptor() != entityDescriptor)
61              throw new IllegalArgumentException("Invalid userID field descriptor: entityDescriptor=" + entityDescriptor.getCodeName() + ", fieldDescriptor=" + userIDFieldDescriptor.getCodeName());
62          if (passwordFieldDescriptor.getEntityDescriptor() != entityDescriptor)
63              throw new IllegalArgumentException("Invalid password field descriptor: entityDescriptor=" + entityDescriptor.getCodeName() + ", fieldDescriptor=" + passwordFieldDescriptor.getCodeName());
64          
65          mEntityDescriptor = entityDescriptor;
66          mUserIDFieldDescriptor = userIDFieldDescriptor;
67          mPasswordFieldDescriptor = passwordFieldDescriptor;
68          mGroupFieldDescriptor = groupFieldDescriptor;
69      }
70          
71      // Superclass overrides ----------------------------------------------------
72      public UserInfo login(Object loginInfo) throws SecurityException
73      {
74          if (loginInfo == null)
75              return null; // can't authenticate a null user
76          
77          if (!(loginInfo instanceof LoginService.UserLoginInfo))
78              throw new SecurityException("provided login info is of wrong type: " + loginInfo.getClass()
79                      + ", should be of type: org.caleigo.security.LoginService.UserLoginInfo");
80          
81          LoginService.UserLoginInfo userLoginInfo = (LoginService.UserLoginInfo) loginInfo;
82          
83          String userID = userLoginInfo.getUsername();
84          String password = userLoginInfo.getPassword();
85          ISelection userSelection = mEntityDescriptor.loadSelection(Qualifier.create(mUserIDFieldDescriptor, userID));
86  
87          if (userSelection.size() > 1)
88              throw new SecurityException(userSelection.size() + " users found with the user id " + userID);
89          if (userSelection.size() == 0)
90              throw new SecurityException("Invalid user id: " + userID);
91          
92          if (userSelection.getEntity(0).getData(mPasswordFieldDescriptor) == null || 
93              ((String) userSelection.getEntity(0).getData(mPasswordFieldDescriptor)).compareTo(password) != 0)
94              throw new SecurityException("Invalid password for user: " + userID);
95          
96          UserInfo userInfo = new UserInfo.DefaultUserInfo();
97          
98          userInfo.setCurrentUserID(userID);
99          userInfo.setProperty("entity", userSelection.getEntity(0));
100         
101         if(mGroupFieldDescriptor!=null)
102         {
103             if(userSelection.getEntity(0).isDataNull(mGroupFieldDescriptor))
104             	userInfo.setProperty("group", null);
105             else
106             	userInfo.setProperty("group", mGroupFieldDescriptor.getDataType().convertToString(userSelection.getEntity(0).getData(mGroupFieldDescriptor)));
107         }        
108         return userInfo;
109     }
110 }