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;
private Locale locale;
- public User(int id) {
+ private Set<Group> groups = new HashSet<>();
+
+ private User(int id) {
this.id = id;
updateName(id);
}
}
}
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();
}
}
public boolean canAssure() throws SQLException {
+ if ( !isOfAge(14)) { // PoJAM
+ return false;
+ }
if (getAssurancePoints() < 100) {
return false;
}
* @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;
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");
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 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;
}