0

Node/JS 的新手,我正在为现有的 IT 门户创建密码恢复页面,它搜索 AD(ldap) 和用户注册的数据库。根据两者的结果,向用户提供身份验证选项,然后更改密码

当我从客户端发出请求 ($get()) 以查找用户时,后端会间歇性地停止。请参阅下面的控制台日志记录

这是控制台日志记录

Attempting to find : <account name>
Found AD user: <account name>
in acc prep
in sql request
in getUserRegistration //within auth controller

.
.here it prints the results of the sql query // At this point I would expect the promise to be fulfilled.
.

这是花时间做谁知道什么的地方。它最终会完成,但通常需要几分钟。然后打印...

in sql results

停顿似乎是在 then() 中调用return auth.getUserRegistration(globalUser.sql)之后。来自 auth 控制器的日志记录非常快地记录了 sql 查询的结果,该结果正好在它应该被解析之前,从而将结果返回到下面的 then()。所以我不相信身份验证控制器(它独立完美地工作)存在问题,而是在resolve()中存在问题。也许是因为它在另一个承诺中?

我发现有趣和困惑的是,如果我从另一个选项卡/浏览器浏览到门户,后端工作完成并且页面立即呈现。

请参阅下面的代码

/* Client request */

$.get(urlqry, function(result) {
  if (result.match("<div id=\"pop\">")) {

    $.modal(result, {
        onClose: function () {
          $("[id^=simplemodal").remove();
          $("#findload").hide();
          $("#forgetbtn").show();
          $.modal.close();
      }
    });

  } else {
    console.log("in else");
    $("#divid").html(result)
  }

});

/* 
this is the route that renders based the work from 2 different controllers
1. check if user exists in Active Directory (ldap)
2. check if user has registered (sql)
*/

router.get('/forgetpwd', function(req, res) {

  var usrname = req.query.usr
  var domain = req.query.realm

  if (usrname && domain) {

    console.log("Attempting to find : " + usrname + "@" + domain);

    var globalUser = {
      username: "",
      upn: "",
      accounts: [],
      sql: {},
      registered: false
    }

    ad.getUser(usrname, domain)

    .then(function(result) {
      if (result.userPrincipalName) {

        console.log("Found AD user: " + result.userPrincipalName);

        globalUser.username = result.sAMAccountName
        globalUser.upn = result.userPrincipalName
        globalUser.accounts = result.extensionAttribute15

      } else {

        var fail = {
          status: "fail",
          message: "Unknown Error"
        }

        var msg = result ? result : fail

        throw msg;

      }

    })

    .then(function(result) {
        console.log("in acc prep");

        globalUser.sql.username = globalUser.username
        var upns = globalUser.accounts

        if (upns) {
          upns = upns.split(",")

          for (u in upns) {

            var upn = upns[u].split("@")
            var domain = upn.slice(-1)[0].toLowerCase()

            switch (domain) {
              case "domain1":
                globalUser.sql.domain1UPN = upns[u]
                break;
              case "domain2":
                globalUser.sql.domain2UPN = upns[u]
                break;
              case "domain3":
                globalUser.sql.ihUPN = upns[u]
                break;
              default:
                globalUser.accounts = ""
                break;
            }
          }
        } else {
          globalUser.accounts = ""
        }
        return;

      })

    .then(function(result) {
      console.log("in sql request");

      return auth.getUserRegistration(globalUser.sql)

    })

    .then(function(result) {

      console.log("in sql results");

      if (result.length > 0) {

        globalUser.registered = true
        return

      } else {

        globalUser.registered = false
        return

      }

    })

    .then(function(result) {

      var accounts = {
        username: globalUser.username,
        accounts: globalUser.accounts.split(","),
        registered: globalUser.registered

      }

      console.log(accounts);
      res.render('./forget-options', accounts);

    })

    .catch(function(error) {
      console.log("in catch");
      var failure = {
        status: "fail",
        message: error

      }
      console.log(failure);
      res.render("./ack", failure);
      return

    })

  } else {

    console.log("User field empty and/or Domaim no selected");

  }
});

/*
Checks if a a record exist in a DB based on results from ad.getUser
requires edge/edge-sql
*/

auth.getUserRegistration = function(accounts) {
  return new Promise(function(resolve, reject) {

    var query = "select * from [users] where (username = "
    query += "\'" + accounts.username + "\'"
    if (accounts.domain2UPN) {
      query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
    }
    if (accounts.domain1UPN) {
      query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
    }

    query += ") and q1col is not null and q2col is not null and pin is not null;"

    var sqlparams = {
      connectionString: "Data Source=name.domain.com;Initial Catalog=dbname;Integrated Security=True",
      source: query
    };

    var getReg = edge.func('sql', sqlparams);

    getReg(null, function(error, result) {

      if (error) {

        console.log("Retrieving user ?? registration");
        reject(error);

      } else {

        console.log("in getUserRegistration");
        console.log(result);
        resolve(result);

      }
    });
  });
}
4

1 回答 1

0

我最终将我的 db 模块从 edge-sql 更改为 mssql 并相应地更新了我的代码。它立即起作用。

auth.getUserRegistration = function(accounts) {
  return new Promise(function(resolve, reject) {

    var query = "select * from [users] where (username = "
    query += "\'" + accounts.username + "\'"
    if (accounts.domain2UPN) {
      query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
    }
    if (accounts.domain1UPN) {
      query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
    }

    query += ") and q1col is not null and q2col is not null and pin is not null;"

    sql.connect("mssql://<usr>:<pwd>@<server>/<db>").then(function() {         
        new sql.Request().query(query).then(function(recordset) {
            console.log(recordset);
            resolve(recordset);
        }).catch(function(err) {
            console.log(err);
        });

    }).catch(function(err) {
            console.log(err);
        });
    });
  });
}
于 2016-05-28T23:20:37.237 回答