From 14f1ffd18576abdca6516d812be9a043127fdef1 Mon Sep 17 00:00:00 2001 From: Daniel Gerhardt <daniel.gerhardt@mni.thm.de> Date: Sat, 5 Apr 2014 19:02:45 +0200 Subject: [PATCH] Implement CouchDB based user management --- .../java/de/thm/arsnova/dao/CouchDBDao.java | 47 +++++++++++++++++++ .../java/de/thm/arsnova/dao/IDatabaseDao.java | 5 ++ .../de/thm/arsnova/dao/StubDatabaseDao.java | 13 +++++ 3 files changed, 65 insertions(+) diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index fa3b627f..508bf5d9 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -50,6 +50,7 @@ import com.fourspaces.couchdb.ViewResults; import de.thm.arsnova.connector.model.Course; import de.thm.arsnova.entities.Answer; +import de.thm.arsnova.entities.DbUser; import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.InterposedQuestion; import de.thm.arsnova.entities.InterposedReadingCount; @@ -1338,4 +1339,50 @@ public class CouchDBDao implements IDatabaseDao { this.deleteAnswers(q); } } + + @Override + public DbUser createOrUpdateUser(DbUser user) { + try { + String id = user.getId(); + String rev = user.getRev(); + Document d = new Document(); + + if (null != id) { + d = database.getDocument(id, rev); + } + + d.put("type", "userdetails"); + d.put("username", user.getUsername()); + d.put("password", user.getPassword()); + d.put("activationKey", user.getActivationKey()); + d.put("creation", user.getCreation()); + d.put("lastLogin", user.getLastLogin()); + + database.saveDocument(d, id); + user.setId(d.getId()); + user.setRev(d.getRev()); + + return user; + } catch (IOException e) { + LOGGER.error("Could not save user {}", user); + } + + return null; + } + + @Override + public DbUser getUser(String username) { + NovaView view = new NovaView("user/all"); + view.setKey(username); + ViewResults results = this.getDatabase().view(view); + + if (results.getJSONArray("rows").optJSONObject(0) == null) { + return null; + } + + return (DbUser) JSONObject.toBean( + results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), + DbUser.class + ); + } } diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index b7f153c1..b30507c2 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -23,6 +23,7 @@ import java.util.List; import de.thm.arsnova.connector.model.Course; import de.thm.arsnova.entities.Answer; +import de.thm.arsnova.entities.DbUser; import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.InterposedQuestion; import de.thm.arsnova.entities.InterposedReadingCount; @@ -161,4 +162,8 @@ public interface IDatabaseDao { void publishAllQuestions(Session session, boolean publish); void deleteAllQuestionsAnswers(Session session); + + DbUser createOrUpdateUser(DbUser user); + + DbUser getUser(String username); } diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index 8e354540..a93f1bcc 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap; import de.thm.arsnova.connector.model.Course; import de.thm.arsnova.entities.Answer; +import de.thm.arsnova.entities.DbUser; import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.InterposedQuestion; @@ -497,4 +498,16 @@ public class StubDatabaseDao implements IDatabaseDao { // TODO Auto-generated method stub } + + @Override + public DbUser createOrUpdateUser(DbUser user) { + // TODO Auto-generated method stub + return null; + } + + @Override + public DbUser getUser(String username) { + // TODO Auto-generated method stub + return null; + } } -- GitLab