Mercurial > dive4elements > framework
diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 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 | f456d9d55541 |
children | da33275fb765 |
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