2

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
AAhad
  • 2,805
  • 1
  • 25
  • 42
  • It would be great if you could add an answer with the workaround you describe in a comment to Anton's answer: it would allow upovoting it and make linking easier. – xverges Feb 26 '15 at 05:10

1 Answers1

2
  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", {...})

AAhad
  • 2,805
  • 1
  • 25
  • 42
Anton
  • 3,166
  • 1
  • 13
  • 12
  • 2
    Yes i first call WL.Server.getActiveUser("myRealm") and then i WL.Server.setActiveUser. But i have not tried first disposing/removing existing user identity and then setting it again. But i have found another work around , that looks good to me, it is directly saving in the HTTPSession using this syntax. ( **var obj= WL.Server.getClientRequest ().getSession().getAttribute("mykey")** and **WL.Server.getClientRequest().getSession().setAttribute("mykey", myobj)** ) have declared it inside Adapters . Thanks – AAhad Jun 10 '13 at 08:59
  • @AAhad, please either accept this answer, or write your own answer and accept that. – Idan Adar May 06 '14 at 07:31