Search code examples
xquerymarklogic

remove users from marklogic


I have a set of user names, who are to be removed from the marklogic database. Some of the users from the list may or may not exist in the database. I tried using below script but it fails when there is no user found. How to make the script run without stopping when a user is not found i.e. to go ahead with the removal of the next user rather than erroring out and stopping the script execution. Please help.

xquery version "1.0-ml";
import module namespace sec = "http://marklogic.com/xdmp/security" at "/MarkLogic/security.xqy";
declare variable $USERS := ("user1","user2","user3");
for $user in $USERS
     return $user||'-'||fn:empty((sec:remove-user($user)));

Below is the error

[1.0-ml] SEC-USERDNE: (err:FOER0000) User does not exist: sec:user-name = user1
Stack Trace
In /MarkLogic/security.xqy on line 3612
In get-element("http://marklogic.com/xdmp/security", "sec:user", "sec:user-name", "user1", "SEC-USERDNE")
$col := "http://marklogic.com/xdmp/security"
$elem := "sec:user"
$filter := "sec:user-name"
$value := "user1"
$function-error := "SEC-USERDNE"
$filterValue := <sec:user-name xmlns:sec="http://marklogic.com/xdmp/security">user1</sec:user-name>
$outElem := ()

Solution

  • You could add a where clause that checks if the user exists. So, like this

    xquery version "1.0-ml";
    import module namespace sec = "http://marklogic.com/xdmp/security" at "/MarkLogic/security.xqy";
    
    declare variable $USERS := ("user1","user2","user3");
    
    for $user in $USERS
    where sec:user-exists($user)
    return $user||'-'||fn:empty((sec:remove-user($user)));
    

    If you want it to print true or false based on whether or not the user was actually removed, you could just put that check in an if as part of the return instead.