Skip to content

Commit 40fe860

Browse files
committed
Set permissions when storing documents & cols
Ensure newly created documents and collections belong to the default “repo” group. This is needed when publishing packages and updating the logs, because a user who belongs to the “repo” group may have a different primary group. Primary groups are used when storing resources, so for example user “repojoe” is part of the “repo” group but has a primary group of “dba”, so resources created by this user will belong to group “dba”, and later when a user who is part of the “repo” group tries to update the document, they’ll get a permissions error.
1 parent 18bb136 commit 40fe860

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

modules/log.xqm

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ xquery version "3.1";
2727
module namespace log="http://exist-db.org/xquery/app/log";
2828

2929
import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";
30+
import module namespace scanrepo="http://exist-db.org/xquery/admin/scanrepo" at "scan.xqm";
3031

3132
(:~
3233
: Append entries to the structured application event log
@@ -36,21 +37,21 @@ import module namespace config="http://exist-db.org/xquery/apps/config" at "conf
3637
:)
3738
declare function log:event($event as element(event)) as empty-sequence() {
3839
let $today := current-date()
39-
let $log-collection := log:collection($today)
40+
let $log-collection-name := log:collection($today)
41+
let $log-collection := $config:logs-col || "/" || $log-collection-name
4042
let $log-document-name := log:document-name($today)
41-
let $log-document-path :=
42-
($config:logs-col, $log-collection, $log-document-name)
43-
=> string-join("/")
44-
let $_ :=
45-
if (doc-available($log-document-path)) then
46-
update insert $event into doc($log-document-path)/public-repo-log
47-
else (
48-
log:mkcol($config:logs-col, $log-collection),
49-
xmldb:store(
50-
$config:logs-col || "/" || $log-collection,
51-
$log-document-name,
52-
element public-repo-log { $event })
53-
)
43+
let $log-document := $log-collection || "/" || $log-document-name
44+
let $store-log :=
45+
if (doc-available($log-document)) then
46+
update insert $event into doc($log-document)/public-repo-log
47+
else
48+
(
49+
if (xmldb:collection-available($log-collection)) then
50+
()
51+
else
52+
log:mkcol($config:logs-col, $log-collection-name),
53+
scanrepo:store($log-collection, $log-document-name, element public-repo-log { $event })
54+
)
5455
return
5556
()
5657
};
@@ -76,7 +77,11 @@ function log:mkcol-recursive($collection as xs:string, $components as xs:string*
7677
if (exists($components)) then
7778
let $newColl := concat($collection, "/", $components[1])
7879
return (
79-
xmldb:create-collection($collection, $components[1]),
80+
xmldb:create-collection($collection, $components[1]) !
81+
(
82+
sm:chgrp(xs:anyURI(.), config:repo-permissions()?mode),
83+
.
84+
),
8085
log:mkcol-recursive($newColl, subsequence($components, 2))
8186
)
8287
else

modules/publish-package.xq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ declare function local:log-put-package-event($filename as xs:string) as empty-se
3232
};
3333

3434
declare function local:upload-and-publish($xar-filename as xs:string, $xar-binary as xs:base64Binary) {
35-
let $path := xmldb:store($config:packages-col, $xar-filename, $xar-binary)
35+
let $path := scanrepo:store($config:packages-col, $xar-filename, $xar-binary)
3636
let $publish := scanrepo:publish-package($xar-filename)
3737
return
3838
map {

modules/scan.xqm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ declare namespace xmldb="http://exist-db.org/xquery/xmldb";
1717

1818
declare namespace expath="http://expath.org/ns/pkg";
1919

20+
(:~
21+
: Helper function to store resources and set permissions for access by repo group
22+
:)
23+
declare function scanrepo:store($collection-uri as xs:string, $resource-name as xs:string, $contents as item()?) as xs:string {
24+
xmldb:store($collection-uri, $resource-name, $contents) !
25+
(
26+
sm:chgrp(., config:repo-permissions()?group),
27+
sm:chmod(., config:repo-permissions()?mode),
28+
.
29+
)
30+
};
31+
2032
(:~
2133
: Helper function to store a package's icon and transform its metadata into the format needed for raw-metadata
2234
:)
@@ -26,7 +38,7 @@ function scanrepo:handle-icon($path as xs:string, $data as item()?, $param as it
2638
let $pkgName := substring-before($param, ".xar")
2739
let $suffix := replace($path, "^.*\.([^\.]+)", "$1")
2840
let $name := concat($pkgName, ".", $suffix)
29-
let $stored := xmldb:store($config:icons-col, $name, $data)
41+
let $stored := scanrepo:store($config:icons-col, $name, $data)
3042
return
3143
element icon { $name }
3244
};
@@ -214,7 +226,7 @@ declare function scanrepo:rebuild-package-groups() as xs:string {
214226
$group
215227
}
216228
return
217-
xmldb:store($config:metadata-col, $config:package-groups-doc-name, $package-groups)
229+
scanrepo:store($config:metadata-col, $config:package-groups-doc-name, $package-groups)
218230
};
219231

220232
(:~
@@ -229,7 +241,7 @@ declare function scanrepo:rebuild-raw-packages() as xs:string {
229241
scanrepo:extract-raw-package($package-xar)
230242
}
231243
return
232-
xmldb:store($config:metadata-col, $config:raw-packages-doc-name, $raw-packages)
244+
scanrepo:store($config:metadata-col, $config:raw-packages-doc-name, $raw-packages)
233245
};
234246

235247
(:~

0 commit comments

Comments
 (0)