It's all about the answers!

Ask a question

how can i get logged in user in javascript?


chen fengmiao (7032731) | asked Dec 03 '11, 9:51 p.m.
i use RTC in 3.0.1. I using scripts for attribute customization in work item to validate something. but i don't get current user in script. How can i to do it? What function can get curent user in script? Can you provide some API and some example ? Thank you!

9 answers



permanent link
Ralph Schoon (59.4k23642) | answered Dec 04 '11, 7:54 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
As stated in the other thread, I don't think this is possible.

permanent link
Jorge Diaz (8664234) | answered Dec 05 '11, 2:51 a.m.
JAZZ DEVELOPER
You can get the logged in user in javascript with the following lines of code:


dojo.require("com.ibm.team.repository.web.client.session.Session");

// get the Session object
var Session = com.ibm.team.repository.web.client.session.Session;

// get the AuthenticatedContributor info object from the session
var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;

// get the user ID
var userId = getAuthenticatedContributor().userId;


You can inspect the used objects from some other information you may need in your scripts.

Hope this helps!

Regards,

Jorge.

permanent link
Ralph Schoon (59.4k23642) | answered Dec 05 '11, 3:19 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi Jorge,

thanks for the snippet, I will add it to my example collection. Can you use this to set the user in some contributor attribute script too? I was unable to do so in an experiment I did recently.

permanent link
Jorge Diaz (8664234) | answered Dec 05 '11, 3:30 a.m.
JAZZ DEVELOPER
You mean for example in a calculated value or default one? Yes, I don't see why you wouldn't.

I personally have this code in a js helper library for reuse by my various javascripts that need to know about the user.

Please give a try and I can try to help if you find any problems.

Regards,

Jorge.

permanent link
Ralph Schoon (59.4k23642) | answered Dec 05 '11, 7:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi Jorge,

I tried that in the context of a Calculated Value Provider and the dojo.require() statement for the Session object prevents the script from loading. The internal scripting engine does not have the session object exposed.
https://jazz.net/wiki/bin/view/Main/AttributeCustomizationExamples#Get_an_item_s_UUID_users_iterati indicates that there is no access to the logged in user.


Solution: There is no direct way of getting the user id from the UI,...


So I must currently assume that this not accessible at least in the Javascript API that is provided for Attribute customization.

permanent link
Jorge Diaz (8664234) | answered Dec 07 '11, 3:58 a.m.
JAZZ DEVELOPER
For logging purposes, after checking with Ralph:

for Attribute Customization scripts there seem to be no way of gettting the user ID. For customizations that contribute to the UI (like creating a Web Action or an Attribute Presentation), the Session javascript class can be used to retrieve user ID information.

Regards,

Jorge.

permanent link
Gabriel Galv√£o (3179) | answered Mar 15 '12, 4:06 p.m.
Sorry for resurrecting the topic but I would like to know if this is possible:

I've tried the code linked by Ralph for detecting the logged in user by checking with the work item creator:

workItem.getValue(WorkItemAttributes.OWNER);

but that line won't work unless the WI is saved, correct?

I needed to discover the logged in user for it needs to be passed as an argument to a query executed through a Calculated Value Custom Attribute. Is there any way of accomplishing this?

regards,

ggalvao

For logging purposes, after checking with Ralph:

for Attribute Customization scripts there seem to be no way of gettting the user ID. For customizations that contribute to the UI (like creating a Web Action or an Attribute Presentation), the Session javascript class can be used to retrieve user ID information.

Regards,

Jorge.

permanent link
Ralph Schoon (59.4k23642) | answered Mar 16 '12, 3:24 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi,

I have not found a way to discover the logged in user with scripts. As far as I remember I was also pretty unsuccessful with the owner attribute in scripts. The link mentioned in the thread above indicates that there is currently only limited support for IItem objects. For the owner etc. I was able to retrieve an item ID, but there are no operations I am aware of that would allow to get the name etc.

Having said that, there is a way to get the logged in user (authenticated user) using the default value customization. You might be able to use a hidden attribute that you set using this. I am not sure however.

permanent link
Ralph Schoon (59.4k23642) | answered May 21 '15, 4:01 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Please see answer https://jazz.net/forum/questions/133477/work-item-customization-getting-current-user-as-logged-in-user/184711 for how to get the item ID for the user that runs the operation. Thanks to Amaury Quintero for providing this info.

Your answer


Register or to post your answer.