Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

work item customization: getting current user as logged in user

 Hello,

is there possible to get current logged user with JavaScript API?
I 've tried with custom attribute of type contributor with setting default value as authenticated user.
As authenticated user I can only get the user who created work item, not really logged user. So, this is the same if I used work item creator, right?
Currently, I am doing some important implementation with work items where I have next rules defined:

Let's say that I have a custom attribute verified with two choises: Yes and No. By default, No is chosen.  
Now, let's say that only one role (project lead) can set this attribute to Yes, and when another role (wi owner-developer) wants to set up this attribute to Yes, on wi saving, it resets automatically to No.
As I can't access with JavaScript to user role, I created Default Value Role Based user list, and got the user with specific role.
So, I created the rule when the work item owner (developer) and role based user (project lead) are different, on wi saving, custom attribute verified resets automatically to No.
Also, I created another rule when role based user (project lead) and authenticated user are the same, on wi saving, custom attribute verified resets automatically to Yes.
So, the problem is that authenticated user is always the same user who created work item, not currently logged in user.
My problem would be solved if any of these funcionality are supported:
accessing user role or getting currently logged in user.

Can anyone help me with this or tell me if any of mentioned functionalities are supported?

Thanks,

1 vote


Accepted answer

Permanent link
Very easy 


 ...

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


(function() {
...

var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;

dojo.declare("com.ibm.san.ariadna.process.SampleReadOnlyCondition", null,
{
matches : function(workItem, configuration) {

var loggedInUser = getAuthenticatedContributor().itemId;
var owner = workItem.getValue(WorkItemAttributes.OWNER);

var result = (loggedInUser != owner);

return result;
}

});
})();


Hope this help
Ralph Schoon selected this answer as the correct answer

4 votes

Comments

 Hi Amaury, 


I am trying to get current user because I have situation that I need to disable some attibutes on work items for all other users except me. 
I tried your script in eclipse client, bud It doesn't work for some reason. 
Do I need to setup anything else?

Regards,
Milan

 Hi Milan, in effect this only works in Web because the package we are using is web (dojo.require("com.ibm.team.repository.web.client.session.Session");

For doing the same for Eclipse you have to use Java and put in the common plugin in order to be loaded for the server and client or put in the client plugin for just only be loaded in Eclipse client. But it is just Java code.

I'm trying to use this code to set a defaultvalue, but declaring getAuthenticatedContributor causes nothing to return.  I've tried debugging, and both variables do get defined. 

Any idea why this wouldn't work?

dojo.provide("ba.workitem.defectEscalation.gscOwner.defaultvalue");

dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("com.ibm.team.repository.web.client.session.Session");
           
(function(){
        var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;
       
        dojo.declare("ba.workitem.defectEscalation.gscOwner.defaultvalue", null, {
            getDefaultValue: function(attribute, workItem, configuration){

                var loggedinUser = getAuthenticatedContributor().itemId;
                return loggedinUser;

           
        }
    });
})();

















This would only work in the Web UI as far as I know.

 Should it work in 5.0.2 as well? 


I get error in client log while using Session package:
!ENTRY com.ibm.team.rtc.common.scriptengine 2 0 2015-10-05 17:42:46.964
!MESSAGE unable to find file: com/ibm/team/repository/web/client/session/Session.js

!ENTRY com.ibm.team.rtc.common.scriptengine 4 0 2015-10-05 17:42:46.979
!MESSAGE Error executing script epoch.js
!STACK 0
org.mozilla.javascript.EcmaError: TypeError: Cannot read property "web" from undefined ({"Bundle-SymbolicName":"org.dojotoolkit.dojo", "path":"resources", "name":"dojo.js"}#313(Function)#1(eval)#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
...

This ONLY works in the web UI, as far as I can tell.

@rschoon - are there any plans to make it working in Javascript in Eclipse client as well?

I don't know. Request an enhancement, or use the Java capabilities for the moment.

showing 5 of 8 show 3 more comments

2 other answers

Permanent link
 Milan, look at rsjazz.wordpress.com for API examples. Dependent on what API u use, you can access the logged in user using methods on the team repository object or through the abstract service. You can search the blog. Search for authenticated user for example.

0 votes

Comments

I think you can only get the authenticated user in Java based attribute customization. Search for provider in my blog for the example post. 

He wanted Javascript access.. I think that s still not available, right?

I realized that he wanted JavaScript too late. As far as I can tell, there is no way to know the looged-in user from JavaScript today.

Are there existing enhancements to provide the same access to attributes of a work item through JavaScript that can be done in Java?  In addition to current user, it would be helpful to have access to Links and Approvals.

1 vote

So, is there possible to call REST service from JavaScript using DOJO in RTC?

Maybe I could get the current user info retrieved from service?


Ok Ralph, thanks for the answer.

One more question regarding to RTC setup for API development. 
As I want to establish this environment and use default RTC eclipse client installed via IM, do I need only import RTC SDK and follow this document:
because there are instructions about whole setup.
Thanks,

I would suggest to follow the links in the post I sent you. Follow the links in the section at the top saying

"If you are just starting with extending Rational Team Concert and using the API’s for automation, start........"

Hi Ralph,


I succeeded to establish development environment for attribute validation in Java, and I also succeeded to create two value providers based on your examples.
Now, I saw one example "Condition that checks if the user editing the work item has a specific role", and I think this is something what I need. 
My question is, how and where in process xml define which process roles I should check...?
I saw one piece of code that it must be set as valueSetProvider.
Do I need first create ValueSet based on "User Role Based List", and check it out through Java, or I can set it manually through process xml?
Thanks,

Milan, as shown in the blog, and described here here: https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Configuring_additional_script_pa you need to configure the provider. https://jazz.net/library/article/1093 Lab 5 explains it in more detail for some ofthe examples, I believe. You search the provider configuration e.g. using the name. You modify the entry, remove the trailing / and add a new closing statement. In between you add the parameters.


Hi Ralph, 

I got it and succeeded to set condition based on current user role.
Do you know maybe which method I can use to get current user role name? I am investigating, but maybe you can save me some time.
Thanks, 

Please look into https://rsjazz.wordpress.com/2012/12/09/analyzing-a-aroject-areas-members-and-roles-using-the-plain-java-client-libraries/ and search for IRole2 - that is the interface that provides you with the role name.

Please try search on my blog, searching for "role name" would have found you the right article.

 Milan, you don't need to call a WebService. I think the best solution is to create a Data Connector for the Server Side JavaScript to work, then you need to differentiate when the code is running on client or when is running on server. That way you have all control about the code and you can get the current user on server side


Amaury

showing 5 of 14 show 9 more comments

Permanent link
 Hi Ralph, 
thank you very much for the help.

Now, I have a situation where I created calculate value provider in Java, and deployed it successfully. I was able to see it in Project Area Attribute Customization, but when I want to put it to be shown on some wi attribute, I can't choose it, it looks like it doesn't exist, but in Project Area Attribute Customization it really exists.
What could be a problem?

Here is my java code (regarding the current user role, I want to retrieve some string):
--------------------------------------------------------------------------------
package hr.apisit.hello;

import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.common.IContributorHandle;
import com.ibm.team.workitem.common.IWorkItemCommon;
import com.ibm.team.workitem.common.internal.attributeValueProviders.IConfiguration;
import com.ibm.team.workitem.common.internal.attributeValueProviders.IValueProvider;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IWorkItem;

public class IzracunRole implements IValueProvider<String> {

@Override
public String getValue(IAttribute attribute, IWorkItem workItem,
IWorkItemCommon workItemCommon, IConfiguration configuration,
IProgressMonitor monitor) throws TeamRepositoryException {
List lookupRoles = ContributorUtil.getConfiguration(configuration);
IContributorHandle user= workItemCommon.getAuditableCommon().getUser();
Boolean usrRole = ContributorUtil.hasRole(workItem, user, lookupRoles, workItemCommon, monitor);
if(usrRole == true){
return "right role";
}
else
{
return "wrong role";
}
}

}


-------------------------------------------------------------------------------

0 votes

Comments

I can only guess. this is probably due to the fact that you have to specify the type this is returning, as descried in my blog, and you did not specify the correct type. Or it is some other kind of deployment issue. Are you trying to deploy on Jetty?

Hi Ralph, I solved the issue, the problem was that I didn't defined public part bellow public class and above public String getValue(IAttribute attribute, IWorkItem workItem...

It works now.

But now, I am facing with another problem: I have not the same behavior on client and web.
So, what I wanted to do works perfect in Eclipse Client, but not in the web (tested with Gchroome and IE).
I 've also done your example with retriving current date with calculated provider, and it works in client, but not on web :(
What could be a problem?

You probably failed to deploy the extension on the server. You need to deploy it on the server as well as in Eclipse.

1 vote

 Hi Ralph,


I was little playing with java codes from your blog site, and like I mentioned before, I succeeded to set condition based on current user role editing work item. The condition is related to read only attributes for condition.
I also noticed that if I return some calculated value in some wi attibute regarding the current role editing work item, this will work only during the creation of work item. 
So, if I create work item, my current role "project leader" return "right role" string, and some other role "developer" return "wrong role" string, in wi attribute "right role" will stay for always, and it won't be changed event the developer edits that work item and his role return "wrong role". 
Hope you understood me.

Milan, as all attribute customizations, the Java versions require to define dependent attributes that trigger changes. This may be an issue, I am not sure for which attribute to create a dependency here.  I always had conditions that were dependent on some attribute. So maybe this can't be done. I am not sure Milan, sorry. I would have to spend time on this.

 Ralph, I thing this is worth of investigating, and I 'll also try to spend some time on this. Our management has specific requirements which can only be done by implementing these Java API-s to work items.

Another things which I want to ask you: Is there possible to manage with timesheet tracking part of work items through JavaScript or Java APIs? For example, to disable past week for entering time spent hours...
Also, is there possible to write some Advisor in Java for sending emails to users? For example, if some developer has spent more hours than it is being estimated he could be warned up that he exceeded spent hours.

Thanks,

Milan, I am pretty sure there are enhancement requests to make things read only by role. there are also enhancement requests for making things invisible by role. I can't look it up right now. It might make sense to search for these work items and add your support.

There are API's for time tracking data. Essentially there is a special link on the work item, as far as I know. I know customers have created their own tools to maintain them. This can only be achieved by Java or REST API's. For the reasons I stated in the workshop JavaScript does not provide an API. You can search this forum, about details, there might be some.

Yes, you can create follow up actions and other means to send mail This has also been discussed here several times. You can start here for an example. I would suggest to open new questions if you need more answers on the forum.

showing 5 of 7 show 2 more comments

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 12,014
× 57
× 35

Question asked: Nov 09 '13, 10:03 p.m.

Question was seen: 10,768 times

Last updated: Jun 07 '16, 3:44 a.m.

Confirmation Cancel Confirm