#1 2024-05-21 02:32:07

dkwfung
Member
Registered: 2024-04-25

Add member into group via groovy

Dear all,

I would like to ask you how to add member into group via groovy.

Grateful if you could share the document or groovy to me, please.

Thank you

Regards,

David

Offline

#2 2024-05-21 06:27:44

Felix
Moderator
Registered: 2022-08-01

Re: Add member into group via groovy

There is a simple Script you can use... we need to put this into our example repository wink
... the formatting of the groovy GSTRING (this tripple-quoted string """ ) is a bit strange... it's \n (new line) separated... and doesn't like empty lines

import net.datenwerke.security.service.usermanager.UserManagerService;
import net.datenwerke.security.service.usermanager.entities.User;
import net.datenwerke.security.service.usermanager.UserPropertiesService;
import net.datenwerke.security.service.usermanager.entities.OrganisationalUnit;
import net.datenwerke.security.service.usermanager.entities.AbstractUserManagerNode;
import net.datenwerke.security.service.usermanager.entities.Group;
//import net.datenwerke.rs.terminal.service.terminal.TerminalService;

UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);
UserPropertiesService userPropertiesService = GLOBALS.getRsService(UserPropertiesService.class);
//                     FIRST.  LAST.      USER.     EMAIL      OU
def USER_CSV_STRING = """FIRSTNAME;LASTNAME;USERNAME;EMAIL;OU
FIRSTNAME2;LASTNAME2;USERNAME2;EMAIL2;OU"""

/* load service */
//def userService = GLOBALS.getInstance(UserManagerService.class);
//def terminal = GLOBALS.getInstance(TerminalService.class);
USER_CSV_STRING.split("\n").each{ u ->

    def us = u.split(";")
  /* create user */
  def user = new User();
  user.setUsername(us[2]);
  user.setFirstname(us[0]);
  user.setLastname(us[1]);
  user.setEmail(us[3]);

  /* Assigning apikey */

  //userPropertiesService.setPropertyValue(user, "apikey", "xxxxxx");

  /* Adding the Users to OU */

  User users = userManagerService.getUserByName(user.getUsername());
  

  List<OrganisationalUnit> ouList = userManagerService.getOUsByName(us[4]);
  user.setParent(ouList[0])

  /* persist user */
  //userManagerService.persist(targetNode);
  try{
     userManagerService.persist(user);
  }catch ( Exception ex ) {
    tout.println("persisting $user failed: $ex")
  }	
 
}
return "added new user";

Softwareentwickler bei Infofabrik

Offline

Board footer

Powered by FluxBB