Created
December 9, 2016 20:20
-
-
Save willsthompson/febf65aced2f88e92134bc8e8e1e7ca8 to your computer and use it in GitHub Desktop.
Checks documents and deletes any permissions to missing roles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
let $uris := cts:uris((), 'limit=30000') | |
let $permissions-map := map:new(( | |
$uris ! map:entry(., xdmp:document-get-permissions(.)) | |
)) | |
let $orphaned-map := | |
xdmp:eval(' | |
xquery version "1.0-ml"; | |
import module namespace sec="http://marklogic.com/xdmp/security" at | |
"/MarkLogic/security.xqy"; | |
declare variable $PERMISSIONS external; | |
map:new( | |
for $uri in map:keys($PERMISSIONS) | |
let $orphaned := | |
for $p in map:get($PERMISSIONS, $uri) | |
return try { | |
let $name := sec:get-role-names($p/sec:role-id) | |
return () | |
} | |
catch ($e) { | |
if ($e/error:code = "SEC-ROLEDNE") | |
then $p | |
else xdmp:rethrow() | |
} | |
where (exists($orphaned)) | |
return map:entry($uri, $orphaned) | |
) | |
', | |
(xs:QName('PERMISSIONS'), $permissions-map), | |
<options xmlns="xdmp:eval"> | |
<database>{xdmp:security-database()}</database> | |
</options>) | |
for $o in map:keys($orphaned-map) | |
let $permissions := map:get($orphaned-map, $o) | |
return xdmp:document-remove-permissions($o, $permissions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment