#1 2020-12-09 16:06:33

Stéphane
Member
Registered: 2020-09-30

How to delete user by script ?

Hello,

In the ldapimport.groovy, when the users in the "external" OrganisationUnit are not to be, the script removes them with no parent but they still exists

In the script, we find:

if(null != current.getOrigin() && current.getOrigin().startsWith(providerUrl) && !nodesInDirectoryByGuid.containsKey(current.getGuid())){
			current.getParent().removeChild(current);
			removedNodes.add(current);
		}

The problem is that those users still exist for ldap identification.

We can see them by clicking on + in users management, they are out of User Root.

How can we delete them and what must be the change in the script to delete them instead of remove them.

Thanks

Offline

#2 2020-12-10 08:04:23

eduardo
Administrator
Registered: 2016-11-01
Website

Re: How to delete user by script ?

Hi Stéphane,

this seems to be a bug in the script, we are currently working on it. Basically, the bugfix is the following:

//current.getParent().removeChild(current);
userManagerService.forceRemove(current);

Does this work for you ?

Regards,
Eduardo

Offline

#3 2020-12-10 10:50:58

Stéphane
Member
Registered: 2020-09-30

Re: How to delete user by script ?

Hello,

it doesn't work for the users removed with no parent

I try this to delete them:

userManagerService.getAllUsers().each{
  if (it.getParent()==null) {
     it.setWriteProtection(false);
     tout.println(it.getUsername());
     userManagerService.forceRemove(it);
  }
}

no error but not deleted

certainly a  userManagerService.persist but on which node ?

Offline

#4 2020-12-10 12:09:11

eduardo
Administrator
Registered: 2016-11-01
Website

Re: How to delete user by script ?

Hi Stéphane,

the bugfix I sent was for modifying the ldapimport script, for next users deleted to be deleted correctly.

The users already (incorrectly) deleted have to be removed manually.
You can use these scripts for this:

Show users directly under root:

import java.util.Map.Entry
import java.util.logging.Level
import java.util.logging.Logger


import net.datenwerke.security.service.usermanager.UserManagerService
import net.datenwerke.security.service.usermanager.entities.AbstractUserManagerNode
import net.datenwerke.security.service.usermanager.entities.Group
import net.datenwerke.security.service.usermanager.entities.OrganisationalUnit
import net.datenwerke.security.service.usermanager.entities.User

UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);

Logger logger = Logger.getLogger(getClass().getName());
List<AbstractUserManagerNode> umRoots = userManagerService.getRoots();

for (AbstractUserManagerNode rootNode: umRoots) {
    
  logger.log(Level.SEVERE, rootNode.getName() + ": " + rootNode);
  
}

For actually deleting these users:

import java.util.Map.Entry
import java.util.logging.Level
import java.util.logging.Logger


import net.datenwerke.security.service.usermanager.UserManagerService
import net.datenwerke.security.service.usermanager.entities.AbstractUserManagerNode
import net.datenwerke.security.service.usermanager.entities.Group
import net.datenwerke.security.service.usermanager.entities.OrganisationalUnit
import net.datenwerke.security.service.usermanager.entities.User

UserManagerService userManagerService = GLOBALS.getRsService(UserManagerService.class);

Logger logger = Logger.getLogger(getClass().getName());
List<AbstractUserManagerNode> umRoots = userManagerService.getRoots();

for (AbstractUserManagerNode rootNode: umRoots) {
    
  System.out.println(rootNode.getName() + ": " + rootNode);
  if (rootNode.getName().equals("User Root")) {
    logger.log(Level.SEVERE, "CLEANING: skipping: " + rootNode.getName());
    continue;
  }
  logger.log(Level.SEVERE, "CLEANING: deleting: " + rootNode.getName());
  rootNode.setWriteProtection(false);
  userManagerService.remove(rootNode);
}

If you include the bugfix userManagerService.forceRemove(current); into your ldapimport script, this should work as expected for next users being deleted.
Can you please confirm this?

Regards,
Eduardo

Offline

#5 2020-12-10 15:46:22

Stéphane
Member
Registered: 2020-09-30

Re: How to delete user by script ?

Thanks Eduardo,

it was OK to delete users with no parent. (very well)

for the ldapimport.groovy, the userManagerService was not known in the class and the users were write protected so i did that and it's ok:

       //current.getParent().removeChild(current);
       current.setWriteProtection(false);
       getUserManagerService().forceRemove(current); //userManagerService was set as private in the class and setter and getter were added

Regards
Stéphane

Last edited by Stéphane (2020-12-10 15:49:47)

Offline

Board footer

Powered by FluxBB