]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/User.java
ADD: user group management (with testcase)
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
index bc927fa9849bd647033fe8a50dfdf2baa7a4f645..c07db48cb298fd5b7cf5da2470da3dcf6e494386 100644 (file)
@@ -5,7 +5,10 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Calendar;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Locale;
+import java.util.Set;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
@@ -28,6 +31,8 @@ public class User implements IdCachable {
 
     private Locale locale;
 
+    private Set<Group> groups = new HashSet<>();
+
     private User(int id) {
         this.id = id;
         updateName(id);
@@ -50,6 +55,13 @@ public class User implements IdCachable {
                 }
             }
             rs.close();
+            PreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
+            psg.setInt(1, id);
+            ResultSet rs2 = psg.executeQuery();
+            while (rs2.next()) {
+                groups.add(Group.getByString(rs2.getString(1)));
+            }
+            rs2.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }
@@ -504,6 +516,40 @@ public class User implements IdCachable {
         update.executeUpdate();
     }
 
+    public boolean isInGroup(Group g) {
+        return groups.contains(g);
+    }
+
+    public Set<Group> getGroups() {
+        return Collections.unmodifiableSet(groups);
+    }
+
+    public void grantGroup(User granter, Group toGrant) throws GigiApiException {
+        groups.add(toGrant);
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
+            ps.setInt(1, getId());
+            ps.setString(2, toGrant.getDatabaseName());
+            ps.setInt(3, granter.getId());
+            ps.execute();
+        } catch (SQLException e) {
+            throw new GigiApiException(e);
+        }
+    }
+
+    public void revokeGroup(User revoker, Group toRevoke) throws GigiApiException {
+        groups.remove(toRevoke);
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
+            ps.setInt(1, revoker.getId());
+            ps.setString(2, toRevoke.getDatabaseName());
+            ps.setInt(3, getId());
+            ps.execute();
+        } catch (SQLException e) {
+            throw new GigiApiException(e);
+        }
+    }
+
     private static ObjectCache<User> myCache = new ObjectCache<>();
 
     public static synchronized User getById(int id) {