Worklight: WL.Server.setActiveUser - Can't modify - Illegal State: Cannot change identity

回眸只為那壹抹淺笑 提交于 2019-12-18 07:07:13

问题


I am trying to modify the logged in user identity,

var mydata="this is custom data array";

var user = JSON.parse(JSON.stringify(WL.Server.getActiveUser()));
           WL.Logger.debug("Before Update" + user.attributes);
           WL.Logger.debug(" displayName =" + user.displayName );
           WL.Logger.debug("isUserAuthenticated ="+ user.isUserAuthenticated );
           WL.Logger.debug("userId =" + user.userId );

          WL.Server.setActiveUser ("myAppRealm" ,{    userId: user.userId ,
               displayName: user.displayName,
               isUserAuthenticated: user.isUserAuthenticated,
               attributes: {  userdata: mydata   }
           } );
           WL.Logger.debug(" ---- Updateed user ---- "  );
           var user = JSON.parse(JSON.stringify(WL.Server.getActiveUser())); 

but its giving below exception.

response [/apps/services/api/myApp/common/query] success: /*-secure-
{"isSuccessful":false,"warnings":[],"errors":["Illegal State: Cannot change identity of an already logged in user in realm 'myAppRealm'. The application must logout first."],"responseID":"67","info":[]}*/ worklight.js:1097
Procedure invocation error. Illegal State: Cannot change identity of an already logged in user in realm 'myAppRealm'. The application must logout first. 

How can I modify this?

Edit:

@Xv. Well, that time I had requirement to save some values in a user's session object. For that I first tried modifying user's object as mentioned above but then I found below mentioned APIs that helps in retrieving , modifying or adding values in a session object.

WL 6.3 docs:

  • MobileFirst server-side API>JavaScript server-side API>Classes>WL.Server

Accessing an HttpServletRequest object.

  • WL.Server.getClientRequest

This gives you direct access to HttpServletRequest object and then you can use all of its methods as you would do in JEE applications.

For example:

WL.Server.getClientRequest().getSession().getAttribute("mykey") WL.Server.getClientRequest().getSession().setAttribute("mykey", myobj)


回答1:


  1. Always supply a realm name in getActiveUser API, e.g. WL.Server.getActiveUser("myRealm")

  2. Just like error message says - you cannot alter active user identity, it is not mutable. What you need to do is to dispose of existing user identity first by invoking WL.Server.setActiveUser("myRealm", null) and then call WL.Server.setActiveUser("myRealm", {...})



来源:https://stackoverflow.com/questions/16997804/worklight-wl-server-setactiveuser-cant-modify-illegal-state-cannot-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!