Changeset 6425
- Timestamp:
- Feb 26, 2014, 11:33:37 AM (9 years ago)
- Location:
- trunk/src/core/net/sf/basedb/core
- Files:
-
- 3 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/net/sf/basedb/core/AuthenticationContext.java
r6423 r6425 22 22 package net.sf.basedb.core; 23 23 24 import net.sf.basedb.core.authentication.Authenticat ionInformation;24 import net.sf.basedb.core.authentication.AuthenticatedUser; 25 25 import net.sf.basedb.core.authentication.LoginRequest; 26 26 import net.sf.basedb.core.data.UserData; … … 90 90 } 91 91 92 /** 93 Load a user item from the BASE database given an internal id. 94 @param id The internal id for the user 95 @return A UserData object or null if there is no user 96 */ 97 public UserData getUserById(int id) 98 { 99 return HibernateUtil.loadData(session, UserData.class, id); 100 } 92 101 93 102 /** … … 95 104 internal authentication. 96 105 */ 97 public Authenticat ionInformationverifyUserInternal(LoginRequest loginRequest)106 public AuthenticatedUser verifyUserInternal(LoginRequest loginRequest) 98 107 { 99 UserData userData = getSessionControl().verifyUserInternal(session, loginRequest); 100 AuthenticationInformation info = new AuthenticationInformation(userData.getLogin(), userData.getExternalId()); 101 return info; 108 return getSessionControl().verifyUserInternal(session, loginRequest); 102 109 } 103 110 -
trunk/src/core/net/sf/basedb/core/SessionControl.java
r6424 r6425 45 45 import net.sf.basedb.util.extensions.Registry; 46 46 import net.sf.basedb.util.extensions.manager.ExtensionsManager; 47 import net.sf.basedb.core.authentication.AuthenticatedUser; 47 48 import net.sf.basedb.core.authentication.AuthenticationManager; 48 import net.sf.basedb.core.authentication.AuthenticationInformation; 49 import net.sf.basedb.core.authentication.AuthenticationMethod; 50 import net.sf.basedb.core.authentication.ExtraUserInformation; 49 51 import net.sf.basedb.core.authentication.LoginRequest; 50 52 … … 59 61 import java.util.Collections; 60 62 import java.util.List; 63 61 64 62 65 /** … … 378 381 tx = HibernateUtil.newTransaction(session); 379 382 380 UserData userData = null;381 383 // Try external authentication at first, except... 382 384 // ...root user should always use internal verification … … 384 386 assert root != null : "root == null"; 385 387 388 AuthenticatedUser authUser = null; 386 389 if (!loginRequest.getLogin().equals(root.getLogin())) 387 390 { 388 userData= verifyUserExternal(session, loginRequest);391 authUser = verifyUserExternal(session, loginRequest); 389 392 } 390 393 391 if ( userData== null)394 if (authUser == null) 392 395 { 393 396 // If no user was found, use internal authentication 394 userData= verifyUserInternal(session, loginRequest);397 authUser = verifyUserInternal(session, loginRequest); 395 398 } 396 399 397 LoginInfo li = createLoginInfo(session, userData, loginRequest.getComment(), false); 400 UserData user = HibernateUtil.loadData(session, UserData.class, authUser.getInternalId()); 401 402 LoginInfo li = createLoginInfo(session, user, loginRequest.getComment(), false, authUser.getAuthenticationMethod()); 398 403 HibernateUtil.commit(tx); 399 404 currentContexts.clear(); … … 415 420 Verify the user with internal authentication. 416 421 */ 417 UserDataverifyUserInternal(org.hibernate.Session session, LoginRequest loginRequest)422 AuthenticatedUser verifyUserInternal(org.hibernate.Session session, LoginRequest loginRequest) 418 423 { 419 424 String login = loginRequest.getLogin(); … … 453 458 } 454 459 455 return userData;460 return new AuthenticatedUser(AuthenticationMethod.INTERNAL, userData); 456 461 } 457 462 … … 460 465 */ 461 466 @SuppressWarnings("unchecked") 462 private UserDataverifyUserExternal(org.hibernate.Session session, LoginRequest loginRequest)467 private AuthenticatedUser verifyUserExternal(org.hibernate.Session session, LoginRequest loginRequest) 463 468 { 464 469 AuthenticationContext context = new AuthenticationContext(this, session, loginRequest); … … 468 473 ExtensionsInvoker<AuthenticationManager> invoker = (ExtensionsInvoker<AuthenticationManager>)registry.useExtensions(context, xtManager.getSettings(), "net.sf.basedb.core.authentication-manager"); 469 474 470 AuthenticationInformation info = null; 471 boolean extraInfo = false; 475 AuthenticatedUser authUser = null; 472 476 try 473 477 { … … 476 480 for (AuthenticationManager auth : invoker) 477 481 { 478 info= auth.authenticate();479 if ( info!= null)482 authUser = auth.authenticate(); 483 if (authUser != null) 480 484 { 481 485 // Found a valid login 482 extraInfo = auth.supportsExtraInformation() && info.hasExtraInfo;483 486 break; 484 487 } … … 506 509 507 510 // Return null to force internal verification 508 if (info == null) return null; 509 510 // Load user information, first try 'id' and then 'login' 511 UserData userData = context.getUserByExternalId(info.id); 512 if (userData == null) 513 { 514 userData = context.getUserByLogin(info.login); 515 } 511 if (authUser == null) return null; 512 513 514 // Load user information, priority order is 'internal id', 'external id', 'login' 515 UserData userData = null; 516 if (authUser.getInternalId() > 0) 517 { 518 userData = HibernateUtil.loadData(session, UserData.class, authUser.getInternalId()); 519 } 520 if (userData == null && authUser.getExternalId() != null) 521 { 522 userData = context.getUserByExternalId(authUser.getExternalId()); 523 } 524 if (userData == null && authUser.getLogin() != null) 525 { 526 userData = context.getUserByLogin(authUser.getLogin()); 527 } 528 516 529 if (userData == null) 517 530 { 518 531 // Create new user 519 532 userData = new UserData(); 520 userData.setExternalId( info.id);521 userData.setLogin( info.login);522 userData.setName( info.name == null ? info.login : info.name);533 userData.setExternalId(authUser.getExternalId()); 534 userData.setLogin(authUser.getLogin()); 535 userData.setName(authUser.getLogin()); 523 536 userData.setQuota(HibernateUtil.loadData(session, QuotaData.class, SystemItems.getId(Quota.DEFAULT))); 524 537 User.addDefultRolesAndGroups(session, userData); … … 534 547 535 548 // Synchronize extra information if supported and allowed by config 536 if (extraInfo && (Config.getBoolean("auth.synchronize") || userData.getId() == 0)) 537 { 538 userData.setName(info.name == null ? info.login : info.name); 539 userData.setOrganisation(info.organisation); 540 userData.setAddress(info.address); 541 userData.setPhone(info.phone); 542 userData.setFax(info.fax); 543 userData.setEmail(info.email); 544 userData.setUrl(info.url); 545 userData.setDescription(info.description); 549 if (authUser.hasExtraInformation() && (Config.getBoolean("auth.synchronize") || userData.getId() == 0)) 550 { 551 ExtraUserInformation extraInfo = authUser.getExtraInformation(); 552 if (extraInfo.getName() != null) 553 { 554 userData.setName(extraInfo.getName()); 555 } 556 userData.setOrganisation(extraInfo.getOrganisation()); 557 userData.setAddress(extraInfo.getAddress()); 558 userData.setPhone(extraInfo.getPhone()); 559 userData.setFax(extraInfo.getFax()); 560 userData.setEmail(extraInfo.getEmail()); 561 userData.setUrl(extraInfo.getUrl()); 562 userData.setDescription(extraInfo.getDescription()); 563 564 List<ExtendedProperty> properties = ExtendedProperties.getProperties("UserData"); 565 if (properties != null) 566 { 567 for (ExtendedProperty ep : properties) 568 { 569 Object value = extraInfo.getExtended(ep.getName()); 570 if (value == null || ep.getType().isCorrectType(value)) 571 { 572 userData.setExtended(ep.getName(), value); 573 } 574 } 575 } 546 576 } 547 577 if (userData.getId() == 0) … … 550 580 } 551 581 HibernateUtil.flush(session); 552 return userData;582 return new AuthenticatedUser(authUser.getAuthenticationMethod(), userData); 553 583 } 554 584 … … 585 615 // Load user data 586 616 UserData userData = HibernateUtil.loadData(session, UserData.class, userId); 587 LoginInfo li = createLoginInfo(session, userData, comment, true );617 LoginInfo li = createLoginInfo(session, userData, comment, true, getAuthenticationMethod()); 588 618 HibernateUtil.commit(tx); 589 619 SessionControl impersonated = Application.newSessionControl(getExternalClientId(), getRemoteId(), null); … … 618 648 Create a LoginInfo object and load all information that it needs. 619 649 */ 620 private LoginInfo createLoginInfo(org.hibernate.Session session, UserData userData, String comment, boolean impersonated )650 private LoginInfo createLoginInfo(org.hibernate.Session session, UserData userData, String comment, boolean impersonated, AuthenticationMethod authenticationMethod) 621 651 throws BaseException 622 652 { … … 663 693 li.userId = userData.getId(); 664 694 li.userLogin = userData.getLogin(); 695 li.authenticationMethod = authenticationMethod; 665 696 li.sessionSettings = Collections.synchronizedMap(new HashMap<String,Object>()); 666 697 return li; … … 743 774 updateLastAccess(); 744 775 return loginInfo == null ? 0 : loginInfo.userId; 776 } 777 778 /** 779 Get the method that was used to authenticate the currently 780 logged in user. Internal authentication return 781 {@link AuthenticationMethod#INTERNAL}. 782 @return An AuthenticationMethod object or null if no user is logged in 783 @since 3.3 784 */ 785 public AuthenticationMethod getAuthenticationMethod() 786 { 787 return loginInfo == null ? null : loginInfo.authenticationMethod; 745 788 } 746 789 … … 2152 2195 2153 2196 /** 2197 The authentication method used to allow the user to 2198 login. 2199 */ 2200 private AuthenticationMethod authenticationMethod; 2201 2202 /** 2154 2203 The id of the {@link ProjectData} object of the active project. 2155 2204 */ -
trunk/src/core/net/sf/basedb/core/authentication/AuthenticationInformation.java
r6424 r6425 25 25 /** 26 26 Objects of this class are returned by the 27 {@link Authenticat ionManager#authenticate()}27 {@link Authenticator#authenticate(String, String)} 28 28 method and contains information about the authenticated 29 29 user. The only required fields are {@link #id} and {@link #login}. … … 32 32 <p> 33 33 If the implementation supports extra information, ie. the call to 34 {@link Authenticat ionManager#supportsExtraInformation()}34 {@link Authenticator#supportsExtraInformation()} 35 35 returns TRUE, the other fields may also contain information. 36 36 … … 38 38 @version 2.0 39 39 @base.modified $Date$ 40 @deprecated In 3.3, use {@link AuthenticatedUser} instead 40 41 */ 42 @Deprecated 41 43 public class AuthenticationInformation 42 44 { 43 45 /** 44 TRUE if the contructor with extra information was used, FALSE if the minial constructor was used.46 TRUE if the extra information has been provided, FALSE if not. 45 47 @since 3.3 46 48 */ … … 106 108 public AuthenticationInformation(String login, String id) 107 109 { 108 this(login, id, null, null, null, null, null, null, null, null , false);110 this(login, id, null, null, null, null, null, null, null, null); 109 111 } 110 112 … … 138 140 ) 139 141 { 140 this(login, id, name, organisation, address, phone, fax, email, url, description, true);141 }142 143 private AuthenticationInformation144 (145 String login,146 String id,147 String name,148 String organisation,149 String address,150 String phone,151 String fax,152 String email,153 String url,154 String description,155 boolean hasExtraInfo156 )157 {158 142 if (id == null) throw new NullPointerException("id"); 159 143 if (login == null) throw new NullPointerException("login"); … … 168 152 this.url = url; 169 153 this.description = description; 170 this.hasExtraInfo = hasExtraInfo;154 this.hasExtraInfo = !allNull(name, organisation, address, phone, fax, email, url, description); 171 155 } 172 156 157 158 private static boolean allNull(Object... objects) 159 { 160 for (Object o : objects) 161 { 162 if (o != null) return false; 163 } 164 return true; 165 } 173 166 } -
trunk/src/core/net/sf/basedb/core/authentication/AuthenticationManager.java
r6423 r6425 60 60 @throws A {@link AuthenticationException} if the user was not authenticated 61 61 */ 62 public Authenticat ionInformationauthenticate();62 public AuthenticatedUser authenticate(); 63 63 64 /**65 Should return TRUE or FALSE depending on if the authentication66 server returns additional information about the user.67 @return TRUE if additional information is returned, FALSE otherwise.68 @see AuthenticationInformation69 */70 public boolean supportsExtraInformation();71 64 72 65 } -
trunk/src/core/net/sf/basedb/core/authentication/OldAuthenticationWrapperFactory.java
r6424 r6425 46 46 47 47 private Authenticator auth; 48 private AuthenticationMethod authMethod; 48 49 49 50 public OldAuthenticationWrapperFactory() … … 53 54 { 54 55 auth = getAuthenticator(driver); 56 authMethod = AuthenticationMethod.getInstance(driver); 55 57 } 56 58 } … … 66 68 { 67 69 AuthenticationContext authContext = (AuthenticationContext)context.getClientContext(); 68 return new AuthenticationManager[] { new OldAuthenticationManager(auth, authContext.getLoginRequest()) }; 70 LoginRequest request = authContext.getLoginRequest(); 71 AuthenticationManager authManager = new OldAuthenticationManager(request); 72 73 return new AuthenticationManager[] { authManager }; 69 74 } 70 75 … … 89 94 } 90 95 91 staticclass OldAuthenticationManager96 class OldAuthenticationManager 92 97 implements AuthenticationManager 93 98 { 94 private final Authenticator auth;95 99 private final LoginRequest request; 96 100 97 OldAuthenticationManager( Authenticator auth,LoginRequest request)101 OldAuthenticationManager(LoginRequest request) 98 102 { 99 this.auth = auth;100 103 this.request = request; 101 104 } 102 105 103 106 @Override 104 public Authenticat ionInformationauthenticate()107 public AuthenticatedUser authenticate() 105 108 { 106 return auth.authenticate(request.getLogin(), request.getPassword()); 109 AuthenticationInformation info = auth.authenticate(request.getLogin(), request.getPassword()); 110 111 AuthenticatedUser authUser = new AuthenticatedUser(authMethod, info.login, info.id); 112 if (auth.supportsExtraInformation() && info.hasExtraInfo) 113 { 114 ExtraUserInformation extra = new ExtraUserInformation(); 115 extra.setAddress(info.address); 116 extra.setDescription(info.description); 117 extra.setEmail(info.email); 118 extra.setFax(info.fax); 119 extra.setName(info.name); 120 extra.setOrganisation(info.organisation); 121 extra.setPhone(info.phone); 122 extra.setUrl(info.url); 123 } 124 return authUser; 107 125 } 108 126 109 @Override110 public boolean supportsExtraInformation()111 {112 return auth.supportsExtraInformation();113 }114 115 127 } 116 128
Note: See TracChangeset
for help on using the changeset viewer.