1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37 protected IEntityDescriptor mEntityDescriptor;
38 protected IFieldDescriptor mUserIDFieldDescriptor;
39 protected IFieldDescriptor mPasswordFieldDescriptor;
40 protected IFieldDescriptor mGroupFieldDescriptor;
41
42
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
72 public UserInfo login(Object loginInfo) throws SecurityException
73 {
74 if (loginInfo == null)
75 return null;
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 }