Recuperate organisation groups and roles

I want to recuperate groups and roles of my organisation in two combobox, of corse with a Groovy code.

Any ideas ?

Hi.

You need to create 2 groovy code :

import org.bonitasoft.engine.identity.GroupCriterion;
import org.bonitasoft.engine.identity.Group;
List list = new ArrayList();
List groupList = apiAccessor.getIdentityAPI().getGroups(startIndex, maxResults, GroupCriterion.NAME_ASC);
for (Group group : groupList) {
list.add(group.getName());
}
return list;

and

import org.bonitasoft.engine.identity.RoleCriterion;
import org.bonitasoft.engine.identity.Role;
List list = new ArrayList();
List roleList = apiAccessor.getIdentityAPI().getRoles(startIndex, maxResults, RoleCriterion.NAME_ASC);
for (Role role : roleList) {
list.add(role.getName());
}
return list;

Thank you. Just i have a litle problem. For maxResults i did this :

long nb = apiAccessor.getIdentityAPI().getNumberOfGroups();

But i get an empty list !

This works:

import org.bonitasoft.engine.identity.GroupCriterion;
import org.bonitasoft.engine.identity.Group;

int nb = apiAccessor.getIdentityAPI().getNumberOfGroups();
List list = new ArrayList();
List groupList = apiAccessor.getIdentityAPI().getGroups(0, nb, GroupCriterion.NAME_ASC);

for (Group group : groupList) {
list.add(group.getName());
}
return list;

You can download a test process here (zip file): http://dl.free.fr/getfile.pl?file=/RsZTpw4o

I get an empty list ! May you give me more details about how to implement the code ?

Hi.

In the pool, I add 2 data : groupList and roleList (of type List).
I create a Script Task. In the Operation tab of this task, I add 2 scripts.
The first script : “groupList” Takes value of “groupScript”.
Same thing for roleList with “roleScript”.
In the script I add the code.

I try to use : long nb = apiAccessor.getIdentityAPI().getNumberOfGroups();

You can do for example (groupScript) :

import org.bonitasoft.engine.identity.GroupCriterion;
import org.bonitasoft.engine.identity.Group;
List list = new ArrayList();
List groupList = apiAccessor.getIdentityAPI().getGroups(0, (int) apiAccessor.getIdentityAPI().getNumberOfGroups(), GroupCriterion.NAME_ASC);
for (Group group : groupList) {
list.add(group.getName());
}
return list;

It works :wink: .

Do you have any idea about how to assign selected groupe and role to the user ? in the same code of corse

To assign a Group and Role to a user, you can use the function addUserMembership() :

import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.identity.User;
User user = apiAccessor.getIdentityAPI().getUserByUserName(userName);
Role role = apiAccessor.getIdentityAPI().getRoleByName(roleName);
Group group = apiAccessor.getIdentityAPI().getGroupByPath(groupName);
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

You need to create a script that return nothing and assign it to a empty variable.
For exemple I do this in a Scritp Task:
“emptyVariable” take value of “assignScript”
with this script :

import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.identity.User;

apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);
apiAccessor.getIdentityAPI().createRole(“NewRole”);

User user = apiAccessor.getIdentityAPI().getUserByUserName(“YLombardi”);
Role role = apiAccessor.getIdentityAPI().getRoleByName(“NewRole”);
Group group = apiAccessor.getIdentityAPI().getGroupByPath(“NewGroup”);
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

return “nothing”;

By UserName you mean the login ? Because i want to do that just after this code :

import org.bonitasoft.engine.identity.ContactDataCreator;
import org.bonitasoft.engine.identity.UserCreator;

ContactDataCreator cd = new ContactDataCreator();
cd.setEmail(field_rec_mail1);
UserCreator user = new UserCreator (field_u_login3, field_u_pw3);
user.setPersonalContactData(cd);
user.setFirstName(field_f_name3).setLastName(field_p_name3);
apiAccessor.getIdentityAPI().createUser(user);

Yes, the userName is the login of the user.

If you want to add this just after your code, I think you can do this :

import org.bonitasoft.engine.identity.ContactDataCreator;
import org.bonitasoft.engine.identity.UserCreator;
import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.identity.User;

ContactDataCreator cd = new ContactDataCreator();
cd.setEmail(field_rec_mail1);
UserCreator userCreator = new UserCreator (field_u_login3, field_u_pw3);
userCreator.setPersonalContactData(cd);
userCreator.setFirstName(field_f_name3).setLastName(field_p_name3);
User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Role role = apiAccessor.getIdentityAPI().getRoleByName(roleName);
Group group = apiAccessor.getIdentityAPI().getGroupByPath(groupName);
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

I tried this befor but i get an “Error when submitting the form” ! it should works if we follow the logic but it doesn’t :confused:

I try it and it works.
Here is my code :

import org.bonitasoft.engine.identity.ContactDataCreator;
import org.bonitasoft.engine.identity.UserCreator;
import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.identity.User;

apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);
apiAccessor.getIdentityAPI().createRole(“NewRole”);

ContactDataCreator cd = new ContactDataCreator();
cd.setEmail(“adresse@mail.fr”);
UserCreator userCreator = new UserCreator (“login”, “password”);
userCreator.setPersonalContactData(cd);
userCreator.setFirstName(“FirstName”).setLastName(“LastName”);
User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Role role = apiAccessor.getIdentityAPI().getRoleByName(“NewRole”);
Group group = apiAccessor.getIdentityAPI().getGroupByPath(“NewGroup”);
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

In the first lines of code :

apiAccessor.getIdentityAPI().createGroup(“NewGroup”, “”);
apiAccessor.getIdentityAPI().createRole(“NewRole”);

You mean that in “NewGroup” and “NewRole” i should put my my selected items in the form coresponding to my groupe and role ?

And what about the second ones (“NewGroup” and “NewRole”) should i put the same thing ? because i dont understand why you created them if they already exist ? (I mean in the first tow lines of code) ? and why (“NewGroup”,“”) when u create groupe why not just (“NewGroup”);

Thanks

No, I create a group and a role for the test. You don’t need to create them if they already exist.

You should delete lines 7-8 and just get your existing group/role with line 17-18.

Why (“NewGroup”,“”) : because bonitasoft create the function like this. The function to create a group is createGroup(groupName, parentGroupName);
I have no parent group so I keep it empty.

Here is what i put :

import org.bonitasoft.engine.identity.ContactDataCreator; import org.bonitasoft.engine.identity.UserCreator; import org.bonitasoft.engine.profile.ProfileMemberCreator; import org.bonitasoft.engine.identity.Group; import org.bonitasoft.engine.identity.Role; import org.bonitasoft.engine.identity.User;

ContactDataCreator cd = new ContactDataCreator();
cd.setEmail(field_rec_mail1);
UserCreator userCreator = new UserCreator (field_u_login3, field_u_pw3);
userCreator.setPersonalContactData(cd);

userCreator.setFirstName(field_f_name3).setLastName(field_p_name3);

User user = apiAccessor.getIdentityAPI().createUser(userCreator);

Group group = apiAccessor.getIdentityAPI().getGroupByPath(field_Sélectionner1);
Role role = apiAccessor.getIdentityAPI().getRoleByName(field_Sélectionner2);
apiAccessor.getIdentityAPI().addUserMembership(user.getId(), group.getId(), role.getId());

You said that u did the same thing and it works for you ?

Yes, I do this and it works.

You can see the new User on Bonita Portal. You need to use a Administrator Profil and look in the Organisation tab.

Yes of corse . But the problem is that i get an “error while submitting the form” . I will look for the problem !

By the way, is there any solution to attribute the profile (Administrator or user) by some groovy code or i must do it in the portal ?

For information i am using the community version

I don’t know why you have this error.
What says the log file ?
Your data (field_rec_mail1, field_u_login3, …) are not null ?

For the profil, maybe you can do that with the ProfilAPI but I havn’t try this yet. I always use the portal.

The solution is to use

apiAccessor.getProfileAPI().createProfileMember(1, u.getId(), g.getId(), r.getId())

1 is for User and 2 for Admin

The solution is to use

apiAccessor.getProfileAPI().createProfileMember(1, u.getId(), g.getId(), r.getId())

1 is for User and 2 for Admin