1 2 3 package org.enhydra.shark.authentication; 4 5 import java.sql.SQLException ; 6 7 import org.enhydra.dods.DODS; 8 import org.enhydra.shark.api.RootException; 9 import org.enhydra.shark.api.UserTransaction; 10 import org.enhydra.shark.api.internal.authentication.AuthenticationManager; 11 import org.enhydra.shark.api.internal.working.CallbackUtilities; 12 import org.enhydra.shark.usergroup.data.UserDO; 13 import org.enhydra.shark.usergroup.data.UserQuery; 14 import org.enhydra.shark.usertransaction.SharkDODSUserTransaction; 15 import org.enhydra.shark.utilities.MiscUtilities; 16 import org.enhydra.shark.utilities.dods.DODSUtilities; 17 18 import com.lutris.appserver.server.sql.*; 19 20 25 public class DODSAuthenticationManager implements AuthenticationManager { 26 27 private static final String LDB_PARAM_NAME = "DODSAuthenticationManager.DatabaseName"; 28 private CallbackUtilities callback; 29 private LogicalDatabase db; 30 31 39 public void configure (CallbackUtilities cus) throws RootException { 40 if (null == cus) 41 throw new RootException("Cannot configure without call back impl."); 42 callback = cus; 43 DODSUtilities.init(callback.getProperties()); 44 String dbName = callback 45 .getProperty(LDB_PARAM_NAME, DODS.getDatabaseManager().getDefaultDB()); 46 try { 47 db = DODS.getDatabaseManager().findLogicalDatabase(dbName); 48 } catch (DatabaseManagerException e) { 49 throw new RootException("Couldn't find logical database.", e); 50 } 51 callback.debug("DODSUserGroupManager configured"); 52 } 53 54 65 public boolean validateUser(UserTransaction t, 66 String username, 67 String password) throws RootException { 68 boolean ret = false; 69 DBTransaction dbt = beginTransaction(t); 70 try { 71 UserQuery uQry = new UserQuery(dbt); 72 uQry.setQueryUserid(username); 73 uQry.requireUniqueInstance(); 74 UserDO user = uQry.getNextDO(); 75 76 ret = (null != user) 77 ? MiscUtilities.passwordDigest(password).equals(user.getPasswd()) 78 : false; 79 80 endTransaction(t, dbt, false); 81 } catch (Exception e) { 82 if (e instanceof RootException) { 83 throw (RootException)e; 84 } 85 throw new RootException(e); 86 } 87 return ret; 88 } 89 90 private void endTransaction(UserTransaction t, 91 DBTransaction dbt, 92 boolean commitToo) throws RootException { 93 if (!dbt.getDatabaseName().equals(db.getName())) { 94 try { 95 if (commitToo) { 96 dbt.commit(); 97 } 98 } catch (SQLException e) { 99 throw new RootException(e); 100 } finally { 101 dbt.release(); 102 } 103 } 104 } 105 106 private DBTransaction beginTransaction(UserTransaction t) throws RootException { 107 DBTransaction dbt = ((SharkDODSUserTransaction)t).getDODSTransaction(); 108 if (!dbt.getDatabaseName().equals(db.getName())) { 109 try { 110 dbt = db.createTransaction(); 111 } catch (SQLException e) { 112 throw new RootException(e); 113 } 114 } 115 return dbt; 116 } 117 } 118 119 120 | Popular Tags |