X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FUser.java;h=c07db48cb298fd5b7cf5da2470da3dcf6e494386;hb=6de1708def257130eca7f9e29ee41be8a28562ef;hp=bc927fa9849bd647033fe8a50dfdf2baa7a4f645;hpb=61f103133741ad8b9b45d03d89bbdc09a61bbfef;p=gigi.git diff --git a/src/org/cacert/gigi/dbObjects/User.java b/src/org/cacert/gigi/dbObjects/User.java index bc927fa9..c07db48c 100644 --- a/src/org/cacert/gigi/dbObjects/User.java +++ b/src/org/cacert/gigi/dbObjects/User.java @@ -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 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 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 myCache = new ObjectCache<>(); public static synchronized User getById(int id) {