Friday, May 22, 2015

how to Delete/Disable the users through Java API in Adobe Experience Manager(AEM)

how to Delete/Disable the users through Java API in Adobe Experience Manager(AEM)

This post will explain how to Delete/Disable the users through Java API in Adobe Experience Manager(AEM).

 @Reference
 ResourceResolverFactory resolverFactory;

 ResourceResolver adminResolver = null;
 Session adminSession=null;

 try {  
adminResolver = resolverFactory.getAdministrativeResourceResolver(null);
        adminSession = adminResolver.adaptTo(Session.class);
        final UserManager userManager= adminResolver.adaptTo(UserManager.class);
             
      Authorizable authorizable = userManager.getAuthorizable("userName");
      if (authorizable instanceof User) {
         User user = (User)authorizable;
         user.remove(); //Remove the user

        user.disable() // Disable the user.
    }
                       
 } catch (Exception e) {
e.printStackTrace();
 } finally {
        if (adminResolver != null) adminResolver.close();              
 }

2 comments:

  1. hi,

    i've tried this code, remove()method not removing the user. could you please help..

    thanks,

    ReplyDelete
  2. hi,.

    u missed adminSession.save();
    rest all working fine.

    Thanks,

    ReplyDelete