示例#1
0
  @Override
  public void execute(DatabaseInterface conn, DatabaseTask task, OperationResult log) {
    if (task.isReplicating()) {
      // TODO what should happen during a replicate?
      task.complete();
      return;
    }

    RecordStruct params = task.getParamsAsRecord();
    TablesAdapter db = new TablesAdapter(conn, task);
    BigDateTime when = BigDateTime.nowDateTime();

    String password = params.getFieldAsString("Password");
    String uname = params.getFieldAsString("Username");

    // TODO part of Trust monitoring -- boolean suspect =
    // if (AddUserRequest.meetsPasswordPolicy(password, true).hasLogLevel(DebugLevel.Warn))
    //	params.withField("Suspect", true);

    String uid = null;

    Object userid = db.firstInIndex("dcUser", "dcUsername", uname, when, false);

    if (userid != null) uid = userid.toString();

    // fail right away if not a valid user
    if (StringUtil.isEmpty(uid)) {
      log.errorTr(123);
      task.complete();
      return;
    }

    String ckey = params.getFieldAsString("ClientKeyPrint");

    // find out if this is a master key
    if (StringUtil.isNotEmpty(ckey)) {
      System.out.println("sign in client key: " + ckey);

      task.pushDomain(Constants.DB_GLOBAL_ROOT_DOMAIN);

      Object mk =
          db.getStaticList("dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcMasterKeys", ckey);

      Object mpp =
          (mk == null)
              ? null
              : db.getStaticScalar(
                  "dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcMasterPasswordPattern");

      task.popDomain();

      // if master key is present for the client key then check the password pattern
      if (mk != null) {
        boolean passcheck = false;

        if (StringUtil.isEmpty((String) mpp)) {
          passcheck = true;
        } else {
          Pattern pp = Pattern.compile((String) mpp);
          Matcher pm = pp.matcher(password);
          passcheck = pm.matches();
        }

        if (passcheck) {
          this.signIn(conn, task, db, log, when, uid);
          return;
        }
      }
    }

    if (StringUtil.isNotEmpty(password)) {
      password = password.trim();

      Object fndpass = db.getDynamicScalar("dcUser", uid, "dcPassword", when);

      System.out.println("local password: "******"try local password: "******"root")) {
        task.pushDomain(Constants.DB_GLOBAL_ROOT_DOMAIN);

        Object gp =
            db.getStaticScalar("dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcGlobalPassword");

        task.popDomain();

        System.out.println("global password: "******"try global password: "******"dcUser", uid, "dcConfirmCode");

      if (password.equals(fndpass)) {
        Object ra = db.getStaticScalar("dcUser", uid, "dcRecoverAt");

        if (ra == null) {
          // if code matches then good login
          this.signIn(conn, task, db, log, when, uid);
          return;
        }

        if (ra != null) {
          DateTime radt = Struct.objectToDateTime(ra);
          DateTime pastra = new DateTime().minusHours(2);

          if (!pastra.isAfter(radt)) {
            // if code matches and has not expired then good login
            this.signIn(conn, task, db, log, when, uid);
            return;
          }
        }
      }
    }

    log.errorTr(123);
    task.complete();
  }