Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Add User to Group in Java API

J M November 5, 2013

I am working on an SSO class for JIRA 6.1. I am trying to add a user to a group (both already exist) using Java. I believe that the following code should work:

User user = cs.getUser(username);
Group group = cs.getGroup(groupname);
cs.addUserToGroup(user,group);

This this code example, 'cs' is a CrowdService object returned from ComponentAccessor and username and groupname are Strings of existing users. I've verified that the CrowdService is returning a User object and a Group object that both work (i.e. check getClass() and other checks that the objects are valid). However the call to CrowdService.addUserToGroup fails with the following error:

cause = java.lang.NoSuchMethodError: com.atlassian.crowd.embedded.api.CrowdService.addUserToGroup(Lcom/atlassian/crowd/embedded/api/User;Lcom/atlassian/crowd/embedded/api/Group;)V,
                stacktrace = java.lang.NoSuchMethodError: com.atlassian.crowd.embedded.api.CrowdService.addUserToGroup(Lcom/atlassian/crowd/embedded/api/User;Lcom/atlassian/crowd/embedded/api/Group;)V

Is appears that the module cannot find addUserToGroup even though other methods in the same class work fine. Is this the right way to add a user to a group in the Java API or am I missing something?

6 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Erkki_Aalto
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 19, 2013

I have found the cause of the problem in Confluence. addUserToGroup has changed from void to boolean. Changing the Confluence version from 5.0 to 5.3 in pom.xml and rebuilding helped. The corresponding fix should work for Jira, too.

J M November 20, 2013

Which pom.xml? I'm not building a plugin, this is a class for SSO which doesn't have a pom.xml itself -- at least as far as I've seen it's not used or needed. Since I'm not attempting to assign the output to any variable, I don't see why the return type matters. In fact, I've tried to use it as a boolean directly in the if() test and I get the exact same thing.

J M November 20, 2013

I marked this as the answer for the assist. The code is right, the problem was that I didn't think I could use the Atlassian SDK to build a non-plugin module. That was false and I finally figured it out and building through the SDK causes it to work.

0 votes
Pankaj Pimple March 31, 2015

Hi All,

We are also facing the same issue and we really need this to resolved as this issue started after upgrade of JIRA to 6.3.13.

We could see that below related files have been part of new release.

embedded-crowd-spi-2.6.2-m2.jar
embedded-crowd-core-2.6.2-m2.jar
embedded-crowd-api-2.6.2-m2.jar

to 

embedded-crowd-api-2.8.0-OD-6-JIRA-05.jar

embedded-crowd-spi-2.8.0-OD-6-JIRA-05.jar

embedded-crowd-core-2.8.0-OD-6-JIRA-05.jar

Now our script is giving exactly above mentioned error 

{code}

Caused by: java.lang.NoSuchMethodError: com.atlassian.crowd.embedded.api.CrowdService.addUserToGroup(Lcom/atlassian/crowd/embedded/api/User;Lcom/atlassian/crowd/embedded/api/Group;)V
at com.barcap.devtools.jira.sso.BarcapJiraAuthenticator.createJiraUser(BarcapJiraAuthenticator.java:251)
at com.barcap.devtools.jira.sso.BarcapJiraAuthenticator.getOrCreateUserForJira(BarcapJiraAuthenticator.java:207)
at com.barcap.devtools.jira.sso.BarcapJiraAuthenticator.getUser(BarcapJiraAuthenticator.java:163)
at com.atlassian.seraph.filter.SecurityFilter.doFilter(SecurityFilter.java:136)

{code}

We are desperate to get this resolved. Could anyone please help?

Thanks and regards,

Pankaj

 

J M March 31, 2015

How big of an upgrade did you make going to 6.3.13 - i.e. was it from 6.2 or earlier? This is definitely working against 6.3.10 and I can't believe that the minor-minor release would have made a change to the API that would break the addUserToGroup() function.

Pankaj Pimple March 31, 2015

We have upgraded from 6.0.8 to 6.3.13

J M April 1, 2015

Are you building against LATEST in your pom.xml? You might be building against the old SDK.

0 votes
Erkki_Aalto
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 12, 2013

The same problem appears with Confluence Shibboleth authenticator in Confluence 5.3. What has been done to embedded Crowd API between 5.2 and 5.3?

0 votes
J M November 7, 2013

So I screwed myself by giving away a karma bounty point for this answer so I can only reply once per 24 hours.

Bhusan, is that code working within an SSO/Seraph module? the crowdService.addUserToGroup() method is exactly what's failing for me with NoSuchMethod. I was curious if that's somehow unavailable during an SSO login?

Rambanam, I don't think that works in an SSO module because I can't retrieve a JiraServiceContext for a "logged in user" when I'm actually trying to authenticate the user outright.

Here's the real code I'm trying. This is within getUser(request,response) override of JiraSeraphAuthenticator:

// username is a String with the user already authenticated
// getCrowdService() returns a CrowdService object

log.debug(String.format("Attempting SSO Login for User %s", username));
CrowdService cs = getCrowdService();
User user = cs.getUser(username);
if ( user == null ){
  log.info("No JIRA user " + username + " in JIRA");
  return null;
} else {
  request.getSession().setAttribute(DefaultAuthenticator.LOGGED_IN_KEY, user);         
request.getSession().setAttribute(DefaultAuthenticator.LOGGED_OUT_KEY, null); }

// everyone has to be in a/the default group to login if( ! cs.isUserMemberOfGroup(username,DEFAULT_GROUP) ){ try{ Group defgroup = cs.getGroup(DEFAULT_GROUP); cs.addUserToGroup(user,defgroup); if( cs.isUserMemberOfGroup(username,DEFAULT_GROUP) ){ log.info("user " + username + " added to group " + DEFAULT_GROUP); } else { log.info("user " + username + " could not be added to group " + DEFAULT_GROUP); } } catch(Exception e){ log.info("critical failure adding use to " + DEFAULT_GROUP + ":" + e.getMessage()); } } else { log.debug("user " + username + "in group " + DEFAULT_GROUP); } return user;

This all works except for the call to addUserToGroup(). If the user is manually added to jira-users, then the SSO login is perfect. The "auto add" to jira-users seems to be broken for users who login via an SSO module. The error when addUserToGroup fails is:

generalInfo = {
  interpretedMsg = ,
  cause = java.lang.NoSuchMethodError:   com.atlassian.crowd.embedded.api.CrowdService.addUserToGroup(Lcom/atlassian/crowd/embedded/api/User;Lcom/atlassian/crowd/embedded/api/Group;)V,
  stacktrace = java.lang.NoSuchMethodError: com.atlassian.crowd.embedded.api.CrowdService.addUserToGroup(Lcom/atlassian/crowd/embedded/api/User;Lcom/atlassian/crowd/embedded/api/Group;)V
[^]                             at com.example.jira.HttpHeaderSSOAuthenticator.getUser(HttpHeaderSSOAuthenticator.java:185)

If the auto-add user to group feature worked, I wouldn't be trying to do this. Any help would be GREATLY appreciated, or even an alternative way of triggering the automatic add to jira-users outside of the SSO module that works would be a perfect alternative.

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 5, 2013

the follwoing way we added users to group(but not tested on jira 6.1)

String group ="Group Name";
List groups = Lists.newArrayList(group);
Collection<String> usersToAdd = new HashSet<String>();
usersToAdd.add("prasad");
ErrorCollection errorCollection = new SimpleErrorCollection();
JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(loggedInUser, errorCollection);
if (groupService.validateAddUsersToGroup(jiraServiceContext, groups, usersToAdd) != null) {
	groupService.addUsersToGroups(jiraServiceContext, groups, usersToAdd);
}

0 votes
Bhushan Nagaraj
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 5, 2013

Just tried this code and it works. Are you sure your username or groupname is not empty or null?

CrowdService crowdService = ComponentAccessor.getCrowdService();

try

{

crowdService.addUserToGroup(crowdService.getUser("admin"), crowdService.getGroup("jira-developers");

}

catch(OperationNotPermittedException exception){

//log error

}

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events