X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FUser.java;h=c07db48cb298fd5b7cf5da2470da3dcf6e494386;hb=8b8d9aeca13b47b9d865b2aa230f40c1d910351a;hp=2d8cd96fc0e572f8e4e178b0ccc798cee146a094;hpb=1080b258a15e784ebad30be6f9e20df0de7c5938;p=gigi.git diff --git a/src/org/cacert/gigi/dbObjects/User.java b/src/org/cacert/gigi/dbObjects/User.java index 2d8cd96f..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(); } @@ -162,6 +174,9 @@ public class User implements IdCachable { } public boolean canAssure() throws SQLException { + if ( !isOfAge(14)) { // PoJAM + return false; + } if (getAssurancePoints() < 100) { return false; } @@ -227,17 +242,12 @@ public class User implements IdCachable { * @throws SQLException */ public int getMaxAssurePoints() throws SQLException { + if ( !isOfAge(18)) { + return 10; // PoJAM + } + int exp = getExperiencePoints(); int points = 10; - Calendar c = Calendar.getInstance(); - c.setTime(dob); - int year = c.get(Calendar.YEAR); - int month = c.get(Calendar.MONTH); - int day = c.get(Calendar.DAY_OF_MONTH); - c.set(year + 18, month, day); - if (System.currentTimeMillis() < c.getTime().getTime()) { - return points; // not 18 Years old. - } if (exp >= 10) { points += 5; @@ -257,6 +267,17 @@ public class User implements IdCachable { return points; } + public boolean isOfAge(int desiredAge) { + Calendar c = Calendar.getInstance(); + c.setTime(dob); + int year = c.get(Calendar.YEAR); + int month = c.get(Calendar.MONTH); + int day = c.get(Calendar.DAY_OF_MONTH); + c.set(year, month, day); + c.add(Calendar.YEAR, desiredAge); + return System.currentTimeMillis() >= c.getTime().getTime(); + } + public EmailAddress[] getEmails() { try { PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0"); @@ -495,14 +516,46 @@ 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 User getById(int id) { + public static synchronized User getById(int id) { User u = myCache.get(id); if (u == null) { - synchronized (User.class) { - myCache.put(u = new User(id)); - } + myCache.put(u = new User(id)); } return u; }