Changeset 6427
- Timestamp:
- Feb 28, 2014, 10:33:01 AM (9 years ago)
- Location:
- trunk/src/core/net/sf/basedb/core
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/AuthenticationContext.java
r6425 r6427 22 22 package net.sf.basedb.core; 23 23 24 import java.util.List; 25 24 26 import net.sf.basedb.core.authentication.AuthenticatedUser; 25 27 import net.sf.basedb.core.authentication.LoginRequest; 26 28 import net.sf.basedb.core.data.UserData; 29 import net.sf.basedb.core.query.QueryParameter; 27 30 import net.sf.basedb.util.extensions.ClientContext; 28 31 … … 91 94 92 95 /** 96 Find users in the database using a "free" restriction. The restriction 97 may contain named parameters but their values must be supplied 98 as {@link QueryParameter} objects. 99 100 IMPORTANT! Do not insert user-data into the restriction string since 101 it may open up for SQL injection attacks. 102 103 @param restriction A HQL restriction put in the WHERE clause 104 @param parameters Optional query parameters 105 @return A list of users (may be empty) 106 */ 107 public List<UserData> findUsers(String restriction, QueryParameter... parameters) 108 { 109 String sql = "SELECT usr FROM UserData usr"; 110 if (restriction != null) 111 { 112 sql += " WHERE " + restriction; 113 } 114 org.hibernate.Query query = HibernateUtil.createQuery(session, sql); 115 if (parameters != null) 116 { 117 for (QueryParameter qp : parameters) 118 { 119 query.setParameter(qp.getName(), qp.getValue(), qp.getType().getTypeWrapper().getHibernateType()); 120 } 121 } 122 return HibernateUtil.loadList(UserData.class, query, getSessionControl()); 123 } 124 125 /** 93 126 Load a user item from the BASE database given an internal id. 94 127 @param id The internal id for the user -
trunk/src/core/net/sf/basedb/core/SessionControl.java
r6425 r6427 418 418 419 419 /** 420 Verify the user with internal authentication. 420 Verify the user with internal authentication. If the LoginRequest 421 has a user id this is used first, otherwise the login is used. 421 422 */ 422 423 AuthenticatedUser verifyUserInternal(org.hibernate.Session session, LoginRequest loginRequest) … … 424 425 String login = loginRequest.getLogin(); 425 426 String password = loginRequest.getPassword(); 426 427 org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "GET_USER_FOR_LOGIN"); 428 /* 429 SELECT usr 430 FROM UserData usr 431 WHERE usr.login = :login 432 */ 433 query.setString("login", login); 434 UserData userData = HibernateUtil.loadData(UserData.class, query); 427 int userId = loginRequest.getUserId(); 428 429 UserData userData = null; 430 if (userId > 0) 431 { 432 userData = HibernateUtil.loadData(session, UserData.class, userId); 433 if (userData == null) 434 { 435 throw new ItemNotFoundException("The user with id '" + userId + "' is not known to BASE."); 436 } 437 login = userData.getLogin(); 438 } 439 else if (login != null) 440 { 441 org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "GET_USER_FOR_LOGIN"); 442 /* 443 SELECT usr 444 FROM UserData usr 445 WHERE usr.login = :login 446 */ 447 query.setString("login", login); 448 userData = HibernateUtil.loadData(UserData.class, query); 449 if (userData == null) 450 { 451 throw new ItemNotFoundException("The user with login '" + login + "' is not known to BASE."); 452 } 453 } 435 454 if (userData == null) 436 455 { 437 throw new ItemNotFoundException("The user with login '" + login + "' is not known to BASE."); 438 } 456 throw new ItemNotFoundException("User"); 457 } 458 439 459 if (userData.isRemoved()) 440 460 { -
trunk/src/core/net/sf/basedb/core/authentication/LoginRequest.java
r6423 r6427 35 35 { 36 36 37 private int userId; 37 38 private String login; 38 39 private String password; … … 44 45 {} 45 46 47 /** 48 Create a login request with login + password 49 */ 46 50 public LoginRequest(String login, String password) 47 51 { 48 52 this.login = login; 49 53 this.password = password; 54 } 55 56 /** 57 Create a login request with user id + password 58 */ 59 public LoginRequest(int userId, String password) 60 { 61 this.userId = userId; 62 this.password = password; 63 } 64 65 /** 66 Set the user id to use. 67 */ 68 public void setUserId(int userId) 69 { 70 this.userId = userId; 71 } 72 73 /** 74 Get the user id to use. 75 */ 76 public int getUserId() 77 { 78 return userId; 50 79 } 51 80
Note: See TracChangeset
for help on using the changeset viewer.