[DB]: Add orgAdmin management code
authorFelix Dörre <felix@dogcraft.de>
Wed, 1 Oct 2014 18:34:58 +0000 (20:34 +0200)
committerJanis Streib <janis@dogcraft.de>
Wed, 31 Dec 2014 01:35:44 +0000 (02:35 +0100)
doc/tableStructure.sql
src/org/cacert/gigi/dbObjects/Organisation.java
tests/org/cacert/gigi/TestOrga.java [new file with mode: 0644]

index 7bc8e36..55fdfdf 100644 (file)
@@ -308,6 +308,7 @@ CREATE TABLE IF NOT EXISTS `org_admin` (
   `memid` int(11) NOT NULL,
   `creator` int(11) NOT NULL,
   `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `deleter` int(11) NULL DEFAULT NULL,
   `deleted` timestamp NULL DEFAULT NULL,
   PRIMARY KEY (`orgid`, `memid`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
index 7151203..d85ffb8 100644 (file)
@@ -1,5 +1,8 @@
 package org.cacert.gigi.dbObjects;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
@@ -63,4 +66,33 @@ public class Organisation extends CertificateOwner {
         }
         return null;
     }
+
+    public void addAdmin(User admin, User actor) {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?");
+        ps.setInt(1, getId());
+        ps.setInt(2, admin.getId());
+        ps.setInt(3, actor.getId());
+        ps.execute();
+    }
+
+    public void removeAdmin(User admin, User actor) {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?");
+        ps.setInt(1, actor.getId());
+        ps.setInt(2, getId());
+        ps.setInt(3, admin.getId());
+        ps.execute();
+    }
+
+    public List<User> getAllAdmins() {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid FROM org_admin WHERE orgid=? AND deleted is null");
+        ps.setInt(1, getId());
+        GigiResultSet rs = ps.executeQuery();
+        rs.last();
+        ArrayList<User> al = new ArrayList<>(rs.getRow());
+        rs.beforeFirst();
+        while (rs.next()) {
+            al.add(User.getById(rs.getInt(1)));
+        }
+        return al;
+    }
 }
diff --git a/tests/org/cacert/gigi/TestOrga.java b/tests/org/cacert/gigi/TestOrga.java
new file mode 100644 (file)
index 0000000..da4ce25
--- /dev/null
@@ -0,0 +1,32 @@
+package org.cacert.gigi;
+
+import static org.junit.Assert.*;
+
+import org.cacert.gigi.dbObjects.Organisation;
+import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.testUtils.ManagedTest;
+import org.junit.Test;
+
+public class TestOrga extends ManagedTest {
+
+    @Test
+    public void testAddRm() {
+        User u1 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
+        User u2 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
+        User u3 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
+        User u4 = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
+        Organisation o1 = new Organisation("name", "ST", "prov", "city", u1);
+        assertEquals(0, o1.getAllAdmins().size());
+        o1.addAdmin(u2, u1);
+        assertEquals(1, o1.getAllAdmins().size());
+        o1.addAdmin(u3, u1);
+        assertEquals(2, o1.getAllAdmins().size());
+        o1.addAdmin(u4, u1);
+        assertEquals(3, o1.getAllAdmins().size());
+        o1.removeAdmin(u3, u1);
+        assertEquals(2, o1.getAllAdmins().size());
+        o1.removeAdmin(u4, u1);
+        o1.removeAdmin(u2, u1);
+        assertEquals(0, o1.getAllAdmins().size());
+    }
+}