Skip to content

Instantly share code, notes, and snippets.

@holmesw
Created November 9, 2012 16:26
Show Gist options
  • Save holmesw/4046659 to your computer and use it in GitHub Desktop.
Save holmesw/4046659 to your computer and use it in GitHub Desktop.
Create URI Privileges For Directories/Collections XQuery 1.0-ml
(:~
: XQuery main module to create URI Privileges in ML Security Database
:
: @version 3.1
:)
xquery version "1.0-ml";
import module namespace sec="http://marklogic.com/xdmp/security" at "/MarkLogic/security.xqy";
declare variable $uri-stubs as xs:string* :=
(
"/dir/",
"/whatever/",
"/data/"
);
declare variable $roles as xs:string* :=
(
"whatever-1",
"whatever-2"
);
declare variable $prefix as xs:string := "some-value";
(:~
: create privilege for URI Stub
:
: @see: http://api.xqueryhacker.com/#sec:create-privilege
:
: $param $uri-stub the URI Stub to generate the privilege for
: @return the privilege id
:)
declare function local:uri-stub(
$uri-stub as xs:string
) as xs:unsignedLong? {
try {
sec:create-privilege(
local:prep-privilege($uri-stub),
$uri-stub,
"uri",
$roles
)
}
catch ($e) {
sec:get-privilege(
$uri-stub,
"uri"
)/sec:privilege-id
}
};
(:~
: prepare URI Stub for privilege
:
:
: $param $uri-stub the URI Stub to generate the privilege for
: @return the prepared URI stub
:)
declare function local:prep-privilege(
$uri-stub as xs:string
) as xs:string {
fn:concat(
$prefix,
fn:replace(
fn:substring(
$uri-stub,
1,
fn:string-length(
$uri-stub
) - 1
),
"/",
"-"
)
)
};
local:uri-stub($uri-stubs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment