Worklight: WL.Server.setActiveUser - Can't mod

2019-01-27 01:52发布

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:

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条回答
趁早两清
2楼-- · 2019-01-27 02:17
  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", {...})

查看更多
登录 后发表回答