Skip to content

Commit 3f2d61f

Browse files
committed
[feature] add DB utility module
Helps with setting permisssions and creating collections.
1 parent aba5466 commit 3f2d61f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

modules/db-utility.xqm

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
xquery version "3.1";
2+
3+
module namespace dbu="http://exist-db.org/xquery/utility/db";
4+
5+
import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";
6+
7+
declare variable $dbu:default-permissions := config:repo-permissions();
8+
9+
(:~
10+
: create collection(s) if they do not exist
11+
: any newly created collection will have the package-permissions set
12+
: will throw an error if the current user does not have the appropriate rights
13+
:
14+
: @param $path xs:string
15+
: @returns the path that was entered
16+
:)
17+
declare
18+
function dbu:ensure-collection($path as xs:string) as xs:string {
19+
if (xmldb:collection-available($path))
20+
then $path
21+
else
22+
tokenize($path, "/")
23+
=> tail()
24+
=> fold-left("", dbu:create-collection-with-repo-permissions#2)
25+
};
26+
27+
(:~
28+
: create collection(s) if they do not exist
29+
: any newly created collection will have the permissions given in the second parameter
30+
: will throw an error if the current user does not have the appropriate rights
31+
:
32+
: @param $path xs:string
33+
: @param $permissions map(xs:string, xs:string) user, group, mode
34+
: @returns the path that was entered
35+
:)
36+
declare
37+
function dbu:ensure-collection($path as xs:string, $permissions as map(*)) as xs:string {
38+
if (xmldb:collection-available($path))
39+
then $path
40+
else
41+
tokenize($path, "/")
42+
=> tail()
43+
=> fold-left("", dbu:create-collection(?, ?, $permissions))
44+
};
45+
46+
(:~
47+
: set owner, group and mode for a collection or resource to package defaults
48+
: will throw an error, if the current user does not have the appropriate rights
49+
:
50+
: @param $resource-or-collection xs:string
51+
: @returns the path that was entered
52+
:)
53+
declare
54+
function dbu:set-repo-permissions ($resource-or-collection as xs:string) as xs:string {
55+
dbu:set-permissions($resource-or-collection, $dbu:default-permissions)
56+
};
57+
58+
(:~
59+
: set owner, group and mode for a collection or resource
60+
: will throw an error, if the current user does not have the appropriate rights
61+
:
62+
: @param $resource-or-collection xs:string
63+
: @param $permissions map(xs:string, xs:string) with "user", "group", "mode"
64+
: @returns the path that was entered
65+
:)
66+
declare
67+
function dbu:set-permissions ($resource-or-collection as xs:string, $permissions as map(*)) as xs:string {
68+
let $set := (
69+
sm:chown($resource-or-collection, $permissions?owner),
70+
sm:chgrp($resource-or-collection, $permissions?group),
71+
sm:chmod(xs:anyURI($resource-or-collection), $permissions?mode)
72+
)
73+
74+
return $resource-or-collection
75+
};
76+
77+
declare
78+
%private
79+
function dbu:create-collection-with-repo-permissions ($collection as xs:string, $next as xs:string) as xs:string {
80+
if (xmldb:collection-available(concat($collection, '/', $next)))
81+
then concat($collection, '/', $next)
82+
else
83+
xmldb:create-collection($collection, $next)
84+
=> dbu:set-repo-permissions()
85+
};
86+
87+
88+
declare
89+
%private
90+
function dbu:create-collection ($collection as xs:string, $next as xs:string, $permissions as map(*)) as xs:string {
91+
if (xmldb:collection-available(concat($collection, '/', $next)))
92+
then concat($collection, '/', $next)
93+
else
94+
xmldb:create-collection($collection, $next)
95+
=> dbu:set-permissions($permissions)
96+
};

0 commit comments

Comments
 (0)