]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/User.java
ToS: replacement of CCA/ToS in certs/CertificateIssueForm
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
index 55e567f5dd6a93a793a9c358cde85c7242360748..99d44a7c3674a1b6fcc329e76be731bc5de90025 100644 (file)
@@ -1,8 +1,6 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.Date;
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -15,15 +13,21 @@ import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.DateSelector;
+import org.cacert.gigi.util.CalendarUtil;
+import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.Notary;
 import org.cacert.gigi.util.PasswordHash;
 import org.cacert.gigi.util.PasswordStrengthChecker;
 
+/**
+ * Represents an acting, assurable, user. Synchronizing on user means: no
+ * name-change and no assurance.
+ */
 public class User extends CertificateOwner {
 
     private Name name = new Name(null, null, null, null);
 
-    private Date dob;
+    private DayDate dob;
 
     private String email;
 
@@ -42,7 +46,7 @@ public class User extends CertificateOwner {
 
     private void updateName(GigiResultSet rs) {
         name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
-        dob = rs.getDate("dob");
+        dob = new DayDate(rs.getDate("dob"));
         email = rs.getString("email");
 
         String localeStr = rs.getString("language");
@@ -63,7 +67,7 @@ public class User extends CertificateOwner {
         }
     }
 
-    public User(String email, String password, Name name, Date dob, Locale locale) throws GigiApiException {
+    public User(String email, String password, Name name, DayDate dob, Locale locale) throws GigiApiException {
         this.email = email;
         this.dob = dob;
         this.name = name;
@@ -75,7 +79,7 @@ public class User extends CertificateOwner {
             query.setString(4, name.getMname());
             query.setString(5, name.getLname());
             query.setString(6, name.getSuffix());
-            query.setDate(7, dob);
+            query.setDate(7, dob.toSQLDate());
             query.setString(8, locale.toString());
             query.setInt(9, getId());
             query.execute();
@@ -87,11 +91,11 @@ public class User extends CertificateOwner {
         return name;
     }
 
-    public Date getDoB() {
+    public DayDate getDoB() {
         return dob;
     }
 
-    public void setDoB(Date dob) {
+    public void setDoB(DayDate dob) {
         this.dob = dob;
     }
 
@@ -128,7 +132,7 @@ public class User extends CertificateOwner {
     }
 
     public boolean canAssure() {
-        if ( !isOfAge(14)) { // PoJAM
+        if ( !CalendarUtil.isOfAge(dob, 14)) { // PoJAM
             return false;
         }
         if (getAssurancePoints() < 100) {
@@ -154,7 +158,7 @@ public class User extends CertificateOwner {
     }
 
     public int getAssurancePoints() {
-        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL")) {
+        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP)")) {
             query.setInt(1, getId());
 
             GigiResultSet rs = query.executeQuery();
@@ -190,7 +194,7 @@ public class User extends CertificateOwner {
      * @return the maximal points @
      */
     public int getMaxAssurePoints() {
-        if ( !isOfAge(18)) {
+        if ( !CalendarUtil.isOfAge(dob, 18)) {
             return 10; // PoJAM
         }
 
@@ -216,17 +220,6 @@ public class User extends CertificateOwner {
         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 boolean isValidName(String name) {
         return getName().matches(name);
     }
@@ -278,7 +271,7 @@ public class User extends CertificateOwner {
                 List<Assurance> assurances = new LinkedList<Assurance>();
 
                 while (res.next()) {
-                    assurances.add(new Assurance(res));
+                    assurances.add(assuranceByRes(res));
                 }
 
                 this.receivedAssurances = assurances.toArray(new Assurance[0]);
@@ -297,7 +290,7 @@ public class User extends CertificateOwner {
                     List<Assurance> assurances = new LinkedList<Assurance>();
 
                     while (res.next()) {
-                        assurances.add(new Assurance(res));
+                        assurances.add(assuranceByRes(res));
                     }
 
                     this.madeAssurances = assurances.toArray(new Assurance[0]);
@@ -331,7 +324,7 @@ public class User extends CertificateOwner {
             update.setString(2, name.getLname());
             update.setString(3, name.getMname());
             update.setString(4, name.getSuffix());
-            update.setDate(5, getDoB());
+            update.setDate(5, getDoB().toSQLDate());
             update.setInt(6, getId());
             update.executeUpdate();
         }
@@ -409,8 +402,12 @@ public class User extends CertificateOwner {
     }
 
     public List<Organisation> getOrganisations() {
+        return getOrganisations(false);
+    }
+
+    public List<Organisation> getOrganisations(boolean isAdmin) {
         List<Organisation> orgas = new ArrayList<>();
-        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL")) {
+        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL" + (isAdmin ? " AND master='y'" : ""))) {
             query.setInt(1, getId());
             try (GigiResultSet res = query.executeQuery()) {
                 while (res.next()) {
@@ -538,4 +535,8 @@ public class User extends CertificateOwner {
             ps.executeUpdate();
         }
     }
+
+    private Assurance assuranceByRes(GigiResultSet res) {
+        return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), User.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date"));
+    }
 }