【问题标题】:OpenIDM update users attributes over custom endpointOpenIDM 通过自定义端点更新用户属性
【发布时间】:2017-06-07 17:15:53
【问题描述】:

我正在尝试在 OpenIDM 中开发一个自定义 js 端点,在其中我使用我在脚本中生成的两个属性(otpexpiryotpvalue)更新搜索的用户。

我在openidm/conf/endpoint-otp.json 中添加了一个json conf 来链接:

{
 "context" : "endpoint/otp/*",
 "type" : "text/javascript",
 "file" : "script/otp.js"
}

这是我的脚本openidm/script/otp.js:

(function() {
 if(request.method === "update") {
  var five_minutes = 5 * 60 * 1000;
  var timestamp = new Date().getTime();
  var otpexpiry = timestamp + five_minutes;
  var otpvalue = Math.floor(Math.random() * 9999);


  /* Not sure of this code below I have to update the user searched with " otpdate : otpexpiry " and "otp : otpvalue "*/
  var u = request.value;
  var id = "managed/user/" + u._id;
  if (id != null) {
    openidm['update']( ... );
  }

  return {
       method: "update",
       resourceName: request.resourcePath,
       revision: request.revision,
       parameters: request.additionalParameters,
       patch: request.patchpperations,
       context: context.current
       };
  } else {
       throw { code: 500, message: "Unknown request type " + request.method};
  }
})();

如何更新搜索用户的两个变量?

【问题讨论】:

    标签: javascript json openidm


    【解决方案1】:

    对于任何对我如何实现这一目标感兴趣的人,以下是我的代码。我只是从 POST 传递数据,查询用户,然后使用 openidm.patch 方法更新属性。

    (function(){
        var content = request.content;
            java.lang.System.out.println(content);
    
            var userID = content.id;
            var userEmail = content.email;
            java.lang.System.out.println("User ID: " + userID);
            java.lang.System.out.println("User Email: " + userEmail);
    
            var qry = {
                    '_queryFilter' : '/mail eq "' + userEmail + '"'
            };
            var userFound = openidm.query("managed/user", qry);
    
            if (userFound != null) {
                    java.lang.System.out.println(userFound);
    
                    /*Generate OTP and Expiry */
                    var five_minutes = 5 * 60 * 1000;
                    var timestamp = new Date().getTime();
                    var otpexpiry = timestamp + five_minutes;
                    var otpvalue = Math.floor(Math.random() * 9999);
    
                    java.lang.System.out.println("OTP Valore: " + otpvalue);
                    java.lang.System.out.println("Scadenza: " + otpexpiry);
    
                    var patch = [{ "operation" : "replace", "field" : "/otp", "value" : otpvalue },{ "operation" : "replace", "field" : "/otpdate" , "value" : otpexpiry }];
    
                    openidm.patch("managed/user/" + userID, null, patch);
    
                    return true;
    
            }
    
    })();
    

    您可以使用 POST 方法触发此端点:

    curl -k \
    --header "X-OpenIDM-Username: xxx" \
    --header "X-OpenIDM-Password: xxx" \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" \
    --data '{ \
    "id" : "2d141539-50b6-4637-bfe8-24a1d61fa556", \
    "email" : "jdoe@example.com" }' \
    --request POST "https://localhost:8443/openidm/endpoint/otp"
    

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2015-07-11
      • 1970-01-01
      • 2018-03-30
      相关资源
      最近更新 更多