Merge changes I7ab53fa7,Id4354942
authorMarcus Mängel <m.maengel@inopiae.de>
Mon, 6 Apr 2020 03:13:23 +0000 (03:13 +0000)
committerGerrit Code Review <gigi-system@dogcraft.de>
Mon, 6 Apr 2020 03:13:23 +0000 (03:13 +0000)
* changes:
  upd: add exception handling
  upd: rephrase variables names and update expireCATS

1  2 
util-testing/club/wpia/gigi/pages/Manager.java

@@@ -32,7 -32,6 +32,6 @@@ import club.wpia.gigi.Gigi
  import club.wpia.gigi.GigiApiException;
  import club.wpia.gigi.crypto.SPKAC;
  import club.wpia.gigi.database.GigiPreparedStatement;
- import club.wpia.gigi.database.GigiResultSet;
  import club.wpia.gigi.dbObjects.CATS;
  import club.wpia.gigi.dbObjects.CATS.CATSType;
  import club.wpia.gigi.dbObjects.Certificate;
@@@ -48,7 -47,6 +47,7 @@@ import club.wpia.gigi.dbObjects.DomainP
  import club.wpia.gigi.dbObjects.DomainPingType;
  import club.wpia.gigi.dbObjects.EmailAddress;
  import club.wpia.gigi.dbObjects.Group;
 +import club.wpia.gigi.dbObjects.Name;
  import club.wpia.gigi.dbObjects.NamePart;
  import club.wpia.gigi.dbObjects.NamePart.NamePartType;
  import club.wpia.gigi.dbObjects.User;
@@@ -167,9 -165,7 +166,9 @@@ public class Manager extends Page 
                  ps.setString(6, getRandomCountry().getCode());
                  ps.execute();
              }
 -            new Contract(u, ContractType.RA_AGENT_CONTRACT);
 +            if ( !Contract.hasSignedContract(u, ContractType.RA_AGENT_CONTRACT)) {
 +                new Contract(u, ContractType.RA_AGENT_CONTRACT);
 +            }
              return u;
          }
      }
      }
  
      private void expireCATS(User u, CATSType t) {
-         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `cats_passed` WHERE `user_id`=? AND `variant_id`=? AND `pass_date`>?")) {
-             ps.setInt(1, u.getId());
-             ps.setInt(2, t.getId());
-             ps.setTimestamp(3, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 366));
+         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `cats_passed` SET `pass_date`=? WHERE `user_id`=? AND `variant_id`=? AND `pass_date`>?")) {
+             ps.setTimestamp(1, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 367));
+             ps.setInt(2, u.getId());
+             ps.setInt(3, t.getId());
+             ps.setTimestamp(4, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 366));
              ps.execute();
-             GigiResultSet rs = ps.executeQuery();
-             while (rs.next()) {
-                 GigiPreparedStatement ps1 = new GigiPreparedStatement("UPDATE `cats_passed` SET `pass_date`=? WHERE `id`=?");
-                 ps1.setTimestamp(1, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 367));
-                 ps1.setInt(2, rs.getInt(1));
-                 ps1.execute();
-                 ps1.close();
-             }
+             ps.close();
          }
      }
  
      private static Manager instance;
                  throw new Error(e);
              }
          } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
-             User u = User.getByEmail(req.getParameter("email"));
-             if (u == null) {
+             User userByEmail = User.getByEmail(req.getParameter("email"));
+             if (userByEmail == null) {
                  resp.getWriter().println("User not found.");
                  return;
              }
              try {
                  if (req.getParameter("addpriv") != null) {
-                     u.grantGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
+                     userByEmail.grantGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
                      resp.getWriter().println("Privilege granted");
                  } else {
-                     u.revokeGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
+                     userByEmail.revokeGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
                      resp.getWriter().println("Privilege revoked");
                  }
              } catch (GigiApiException e) {
              fetchMails(req, resp, mail);
          } else if (req.getParameter("cats") != null) {
              String mail = req.getParameter("catsEmail");
-             String testId = req.getParameter("catsType");
+             String catsTypeId = req.getParameter("catsType");
              User byEmail = User.getByEmail(mail);
              if (byEmail == null) {
                  resp.getWriter().println("User not found.");
                  return;
              }
-             if (testId == null) {
+             if (catsTypeId == null) {
                  resp.getWriter().println("No test given.");
                  return;
              }
-             CATSType test = CATSType.values()[Integer.parseInt(testId)];
+             CATSType test = null;
+             try {
+                 test = CATSType.values()[Integer.parseInt(catsTypeId)];
+             } catch (NumberFormatException e) {
+                 resp.getWriter().println("No valid integer given.");
+                 return;
+             }
              passCATS(byEmail, test);
              resp.getWriter().println("Test '" + test.getDisplayName() + "' was added to user account.");
          } else if (req.getParameter("catsexpire") != null) {
              String mail = req.getParameter("catsEmail");
-             String testId = req.getParameter("catsType");
-             User byEmail = User.getByEmail(mail);
-             if (byEmail == null) {
+             String catsTypeId = req.getParameter("catsType");
+             User userByEmail = User.getByEmail(mail);
+             if (userByEmail == null) {
                  resp.getWriter().println("User not found.");
                  return;
              }
-             if (testId == null) {
+             if (catsTypeId == null) {
                  resp.getWriter().println("No test given.");
                  return;
              }
-             CATSType test = CATSType.values()[Integer.parseInt(testId)];
-             expireCATS(byEmail, test);
+             CATSType test = null;
+             try {
+                 test = CATSType.values()[Integer.parseInt(catsTypeId)];
+             } catch (NumberFormatException e) {
+                 resp.getWriter().println("No valid integer given.");
+                 return;
+             }
+             expireCATS(userByEmail, test);
              resp.getWriter().println("Test '" + test.getDisplayName() + "' is set expired for user account.");
          } else if (req.getParameter("verify") != null) {
              String mail = req.getParameter("verifyEmail");
              String verificationPoints = req.getParameter("verificationPoints");
-             User byEmail = User.getByEmail(mail);
+             User userByEmail = User.getByEmail(mail);
  
-             if (byEmail == null) {
+             if (userByEmail == null) {
                  resp.getWriter().println("User not found.");
                  return;
              }
                      vp = 0;
                  }
  
-                 int agentNumber = addVerificationPoints(vp, byEmail);
+                 int agentNumber = addVerificationPoints(vp, userByEmail);
  
                  while (vp > 0) {
                      int currentVP = 10;
                      if (vp < 10) {
                          currentVP = vp;
                      }
-                     if (Notary.checkVerificationIsPossible(getAgent(agentNumber), byEmail.getPreferredName())) {
+                     if (Notary.checkVerificationIsPossible(getAgent(agentNumber), userByEmail.getPreferredName())) {
  
-                         Notary.verify(getAgent(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
+                         Notary.verify(getAgent(agentNumber), userByEmail, userByEmail.getPreferredName(), userByEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
                          vp -= currentVP;
                          verifications += 1;
  
  
              resp.getWriter().println("User has been verified " + verifications + " times." + info);
  
 +        } else if (req.getParameter("verifyexpire") != null) {
 +            String mail = req.getParameter("verifyEmail");
 +            User byEmail = User.getByEmail(mail);
 +            if (byEmail == null) {
 +                resp.getWriter().println("User not found.");
 +                return;
 +            } else {
 +                setVerificationDateToPast(byEmail.getPreferredName());
 +            }
 +
 +            resp.getWriter().println("Verification set to time past the limit.");
          } else if (req.getParameter("letverify") != null) {
              String mail = req.getParameter("letverifyEmail");
-             User byEmail = User.getByEmail(mail);
-             if (byEmail == null || !byEmail.canVerify()) {
+             User userByEmail = User.getByEmail(mail);
+             if (userByEmail == null || !userByEmail.canVerify()) {
                  resp.getWriter().println("User not found, or found user is not allowed to verify.");
              } else {
                  try {
                      for (int i = 0; i < 25; i++) {
                          User a = getAgent(i);
-                         Notary.verify(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
+                         Notary.verify(userByEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
                      }
                      resp.getWriter().println("Successfully added experience points.");
                  } catch (GigiApiException e) {
                  }
              }
          } else if (req.getParameter("addEmail") != null) {
-             User u = User.getByEmail(req.getParameter("addEmailEmail"));
+             User userByEmail = User.getByEmail(req.getParameter("addEmailEmail"));
              try {
-                 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"), Locale.ENGLISH);
+                 EmailAddress ea = new EmailAddress(userByEmail, req.getParameter("addEmailNew"), Locale.ENGLISH);
                  verify(ea.getAddress(), ea);
              } catch (IllegalArgumentException e) {
                  e.printStackTrace();
                  e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
              }
          } else if (req.getParameter("addCert") != null) {
-             User u = User.getByEmail(req.getParameter("addCertEmail"));
+             User userByEmail = User.getByEmail(req.getParameter("addCertEmail"));
              try {
                  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                  kpg.initialize(4096);
  
                  byte[] res = s.getEncoded(sign);
  
-                 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u, sessionAc.isStronglyAuthenticated()), Base64.getEncoder().encodeToString(res), "challenge");
-                 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + u.getEmail());
+                 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(userByEmail, userByEmail, sessionAc.isStronglyAuthenticated()), Base64.getEncoder().encodeToString(res), "challenge");
+                 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + userByEmail.getEmail());
                  Certificate draft = cr.draft();
-                 draft.issue(null, "2y", u).waitFor(10000);
+                 draft.issue(null, "2y", userByEmail).waitFor(10000);
                  if (draft.getStatus() == CertificateStatus.ISSUED) {
                      resp.getWriter().println("added certificate");
                  } else {
              resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
              resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
          } else if (req.getParameter("makeAgent") != null) {
-             User u = User.getByEmail(req.getParameter("agentEmail"));
-             if (u == null) {
+             User userByEmail = User.getByEmail(req.getParameter("agentEmail"));
+             if (userByEmail == null) {
                  resp.getWriter().println("User not found, or found user is not allowed to verify.");
              } else {
-                 if (u.getVerificationPoints() < 100) {
-                     addVerificationPoints(100, u);
+                 if (userByEmail.getVerificationPoints() < 100) {
+                     addVerificationPoints(100, userByEmail);
                  }
-                 if ( !u.hasPassedCATS()) {
-                     passCATS(u, CATSType.AGENT_CHALLENGE);
+                 if ( !userByEmail.hasPassedCATS()) {
+                     passCATS(userByEmail, CATSType.AGENT_CHALLENGE);
                  }
-                 if ( !Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT)) {
+                 if ( !Contract.hasSignedContract(userByEmail, Contract.ContractType.RA_AGENT_CONTRACT)) {
                      try {
-                         new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
+                         new Contract(userByEmail, Contract.ContractType.RA_AGENT_CONTRACT);
                      } catch (GigiApiException e) {
                          throw new Error(e);
                      }
  
          form.output(resp.getWriter(), getLanguage(req), vars);
      }
 +
 +    private static void setVerificationDateToPast(Name name) {
 +        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 +        Calendar c = Calendar.getInstance();
 +        c.setTimeInMillis(System.currentTimeMillis());
 +        c.add(Calendar.MONTH, -TimeConditions.getInstance().getVerificationMonths());
 +        String date = sdf.format(new Date(c.getTimeInMillis()));
 +        GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `notary` SET `date`=? WHERE `to`=? AND `date`>?");
 +        ps.setString(1, date);
 +        ps.setInt(2, name.getId());
 +        ps.setString(3, date);
 +        ps.execute();
 +        ps.close();
 +    }
  }