Sfoglia il codice sorgente

Support non-username as primary key.

Ben Alex 20 anni fa
parent
commit
c699f7d40e

+ 32 - 3
core/src/main/java/org/acegisecurity/userdetails/jdbc/JdbcDaoImpl.java

@@ -1,4 +1,4 @@
-/* Copyright 2004, 2005 Acegi Technology Pty Limited
+/* Copyright 2004 Acegi Technology Pty Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -84,6 +84,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements AuthenticationDao {
     private String authoritiesByUsernameQuery;
     private String rolePrefix = "";
     private String usersByUsernameQuery;
+    private boolean usernameBasedPrimaryKey = true;
 
     //~ Constructors ===========================================================
 
@@ -139,6 +140,28 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements AuthenticationDao {
         return rolePrefix;
     }
 
+    /**
+     * If <code>true</code> (the default), indicates the {@link
+     * #getUsersByUsernameMapping()} returns a username in response to a
+     * query. If <code>false</code>, indicates that a primary key is used
+     * instead. If set to <code>true</code>, the class will use the
+     * database-derived username in the returned <code>UserDetails</code>. If
+     * <code>false</code>, the class will use the {@link
+     * #loadUserByUsername(String)} derived username in the returned
+     * <code>UserDetails</code>.
+     *
+     * @param usernameBasedPrimaryKey <code>true</code> if the mapping queries
+     *        return the username <code>String</code>, or <code>false</code>
+     *        if the mapping returns a database primary key.
+     */
+    public void setUsernameBasedPrimaryKey(boolean usernameBasedPrimaryKey) {
+        this.usernameBasedPrimaryKey = usernameBasedPrimaryKey;
+    }
+
+    public boolean isUsernameBasedPrimaryKey() {
+        return usernameBasedPrimaryKey;
+    }
+
     public void setUsersByUsernameMapping(MappingSqlQuery usersByUsernameQuery) {
         this.usersByUsernameMapping = usersByUsernameQuery;
     }
@@ -191,8 +214,14 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements AuthenticationDao {
 
         arrayAuths = (GrantedAuthority[]) dbAuths.toArray(arrayAuths);
 
-        return new User(user.getUsername(), user.getPassword(),
-            user.isEnabled(), true, true, true, arrayAuths);
+        String returnUsername = user.getUsername();
+
+        if (!usernameBasedPrimaryKey) {
+            returnUsername = username;
+        }
+
+        return new User(returnUsername, user.getPassword(), user.isEnabled(),
+            true, true, true, arrayAuths);
     }
 
     /**

+ 2 - 0
doc/xdocs/changes.xml

@@ -26,6 +26,7 @@
   </properties>
   <body>
     <release version="0.9.0" date="In CVS">
+      <action dev="benalex" type="update">JdbcDaoImpl modified to support synthetic primary keys</action>        
       <action dev="benalex" type="update">Greatly improve BasicAclEntryAfterInvocationCollectionFilteringProvider performance with large collections (if the principal has access to relatively few collection elements)</action>        
       <action dev="benalex" type="update">Reorder DaoAuthenticationProvider exception logic as per developer list discussion</action>        
       <action dev="benalex" type="update">ContextHolder refactored and replaced by SecurityContextHolder</action>        
@@ -34,6 +35,7 @@
       <action dev="benalex" type="update">AnonymousProcessingFilter offers protected method to control when it should execute</action>
       <action dev="benalex" type="fix">AbstractAuthenticationToken.getName() now returns username alone if UserDetails present</action>
       <action dev="raykrueger" type="update">AuthorityGranter.grant now returns a java.util.Set of role names, instead of a single role name</action>  
+      <action dev="benalex" type="update">JavaDoc improvements</action>  
     </release>
     <release version="0.8.2" date="2005-04-20">
       <action dev="benalex" type="fix">Correct location of AuthenticationSimpleHttpInvokerRequestExecutor in clientContext.xml</action>