changeset 413:f5ecca16f601

Implement method to find/get a user by its account name artifacts/trunk@5260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 14:04:38 +0000
parents f4b14240ffa3
children c22f3a7640d7
files artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties
diffstat 3 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Aug 23 14:03:16 2012 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Fri Aug 24 14:04:38 2012 +0000
@@ -96,6 +96,7 @@
     public String SQL_USERS_INSERT;
     public String SQL_USERS_SELECT_ID_BY_GID;
     public String SQL_USERS_SELECT_GID;
+    public String SQL_USERS_SELECT_ACCOUNT;
     public String SQL_USERS_DELETE_ID;
     public String SQL_USERS_DELETE_COLLECTIONS;
     public String SQL_USERS_SELECT_ALL;
@@ -311,6 +312,7 @@
         SQL_USERS_INSERT = sql.get("users.insert");
         SQL_USERS_SELECT_ID_BY_GID = sql.get("users.select.id.by.gid");
         SQL_USERS_SELECT_GID = sql.get("users.select.gid");
+        SQL_USERS_SELECT_ACCOUNT = sql.get("users.select.account");
         SQL_USERS_DELETE_ID = sql.get("users.delete.id");
         SQL_USERS_DELETE_COLLECTIONS = sql.get("users.delete.collections");
         SQL_USERS_SELECT_ALL = sql.get("users.select.all");
@@ -981,6 +983,43 @@
         return exec.runRead() ? user[0] : null;
     }
 
+    /**
+     * Find/Get user by account
+     */
+    public User findUser(
+        final String      account,
+        final UserFactory factory,
+        final Object      context
+    ) {
+
+        final User [] user = new User[1];
+        logger.debug("Tying to find user by account " + account);
+
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+            public boolean doIt() throws SQLException {
+                prepareStatement(SQL_USERS_SELECT_ACCOUNT);
+                stmnt.setString(1, account);
+                result = stmnt.executeQuery();
+                if (!result.next()) { // no such user
+                    logger.debug("No user found.");
+                    return false;
+                }
+                String  identifier = result.getString(1);
+                String  name     = result.getString(2);
+                String account   = result.getString(3);
+                byte [] roleData = result.getBytes(4);
+
+                Document role = XMLUtils.fromByteArray(roleData, true);
+
+                user[0] = factory.createUser(
+                    identifier, name, account, role, context);
+                return true;
+            }
+        };
+
+        return exec.runRead() ? user[0] : null;
+    }
+
     public User [] getUsers(
         final UserFactory factory,
         final Object      context
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Aug 23 14:03:16 2012 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Fri Aug 24 14:04:38 2012 +0000
@@ -37,6 +37,7 @@
 
 users.select.id.by.gid=SELECT id FROM users WHERE gid = ?
 users.select.gid=SELECT id, name, account, role FROM users WHERE gid = ?
+users.select.account=SELECT id, name, account, role FROM users WHERE account = ?
 
 users.delete.id=DELETE FROM users WHERE id = ?
 
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Aug 23 14:03:16 2012 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Fri Aug 24 14:04:38 2012 +0000
@@ -37,6 +37,7 @@
 
 users.select.id.by.gid=SELECT id FROM users WHERE gid = ?::uuid
 users.select.gid=SELECT id, name, account, role FROM users WHERE gid = ?::uuid
+users.select.account=SELECT id, name, account, role FROM users WHERE account = ?
 
 users.delete.id=DELETE FROM users WHERE id = ?
 

http://dive4elements.wald.intevation.org