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;
+ 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();
}
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;
}