forked from eXist-db/eXide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.xq
273 lines (246 loc) · 10.6 KB
/
controller.xq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
xquery version "3.1";
import module namespace login="http://exist-db.org/xquery/login"
at "resource:org/exist/xquery/modules/persistentlogin/login.xql";
import module namespace config="http://exist-db.org/xquery/apps/config" at "modules/config.xqm";
declare namespace json="http://www.json.org";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:prefix external;
declare variable $exist:controller external;
declare variable $local:login-domain := 'org.exist.login';
declare variable $local:config := config:get-configuration();
declare variable $local:method := request:get-method() => lower-case();
declare variable $local:uri := request:get-uri();
declare variable $local:forwarded-for := request:get-header("X-Forwarded-URI");
declare variable $local:wants-json := tokenize(request:get-header('Accept'), ', ?') = 'application/json';
declare function local:get-user () as xs:string? {
let $login := login:set-user($local:login-domain, "P7D", false())
let $user-id := request:get-attribute($local:login-domain || ".user")
return $user-id
};
declare function local:user-allowed($user as xs:string?) as xs:boolean {
$local:config/restrictions/@guest = "yes" or (
not(empty($user)) and
not($user = ('guest', 'nobody'))
)
};
declare function local:query-execution-allowed($user as xs:string?, $is-dba as xs:boolean) as xs:boolean {
$is-dba or (
$local:config/restrictions/@execute-query = "yes" and
local:user-allowed($user)
)
};
let $user := local:get-user()
let $user-to-check := ($user, request:get-attribute("xquery.user"), 'nobody')[1]
let $user-is-dba := sm:is-dba($user-to-check)
let $user-allowed := local:user-allowed($user)
let $xquery-execution-allowed := local:query-execution-allowed($user, $user-is-dba)
(: let $_ := util:log('debug', map{
'request': map {
'method': $local:method,
'forward': $local:forwarded-for,
'uri': $local:uri
},
'exist': map {
'resource': $exist:resource,
'path': $exist:path,
'prefix': $exist:prefix,
'controller': $exist:controller
},
'user': map {
'name': $user,
'allowed': $user-allowed,
'dba': $user-is-dba
}
}) :)
return
(: public :)
if ($exist:path eq '') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{$local:uri}/"/>
</dispatch>
else if ($exist:path eq '/') then
let $path :=
if (
lower-case($local:uri) = "/exist/apps/exide/" and
lower-case($local:forwarded-for) = "/apps/exide/"
)
then "/apps/eXide/"
else ""
let $resource :=
if ($user-allowed)
then "index.html"
else "login.html"
return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{$path}{$resource}"/>
</dispatch>
else if ($local:method = 'get' and $exist:resource = "login.html" and not($user-allowed)) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="login.html">
<set-header name="Cache-Control" value="max-age=3600; must-revalidate;"/>
</forward>
</dispatch>
else if ($local:method = 'get' and $exist:resource = "backdrop.svg") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="resources/images/backdrop.svg">
<set-header name="Cache-Control" value="max-age=73600; must-revalidate;"/>
</forward>
</dispatch>
(: handle unauthorized request :)
else if (not($user-allowed))
then (
if ($local:wants-json)
then (
util:declare-option("output:method", "json"),
response:set-status-code(401),
<status>
<error>unauthorized</error>
</status>
)
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect method="get" url="login.html"/> <!-- maybe add additional parameters -->
</dispatch>
)
(: restricted resources :)
(:
: Login a user via AJAX. Just returns a 401 if login fails.
:)
else if (
$local:wants-json and
$local:method = 'post' and
$exist:resource = 'login'
)
then (
util:declare-option("output:method", "json"),
<status>
<user>{$user}</user>
<isAdmin json:literal="true">{$user-is-dba}</isAdmin>
</status>
)
else if ($local:method = ('get', 'post') and $exist:resource = ('login.html', 'login'))
then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="index.html"/> <!-- maybe add additional parameters -->
</dispatch>
)
else if (starts-with($exist:path, "/store/")) then
let $resource := substring-after($exist:path, "/store")
return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/store.xq">
<add-parameter name="path" value="{$resource}"/>
</forward>
</dispatch>
else if (starts-with($exist:path, "/check/")) then
let $resource := substring-after($exist:path, "/validate")
return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/validate-xml.xq">
<add-parameter name="validate" value="no"/>
</forward>
</dispatch>
else if ($local:method = 'get' and $exist:resource = "index.html") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<view>
<forward url="modules/view.xq">
<set-header name="Cache-Control" value="max-age=3600"/>
</forward>
</view>
</dispatch>
else if ($exist:resource eq 'execute') then
let $query := request:get-parameter("qu", ())
let $base := request:get-parameter("base", ())
let $output := request:get-parameter("output", "xml")
let $startTime := util:system-time()
return
if (not($xquery-execution-allowed)) then
response:set-status-code(403)
else
switch ($output)
case "adaptive"
case "html5"
case "xhtml"
case "xhtml5"
case "text"
case "microxml"
case "json"
case "xml" return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- Query is executed by XQueryServlet -->
<forward servlet="XQueryServlet">
<set-header name="Cache-Control" value="no-cache"/>
<!-- Query is passed via the attribute 'xquery.source' -->
<set-attribute name="xquery.source" value="{$query}"/>
<!-- Results should be written into attribute 'results' -->
<set-attribute name="xquery.attribute" value="results"/>
<set-attribute name="xquery.module-load-path" value="{$base}"/>
<clear-attribute name="results"/>
<!-- Errors should be passed through instead of terminating the request -->
<set-attribute name="xquery.report-errors" value="yes"/>
<set-attribute name="start-time" value="{util:system-time()}"/>
</forward>
<view>
<!-- Post process the result: store it into the HTTP session
and return the number of hits only. -->
<forward url="modules/session.xq">
<clear-attribute name="xquery.source"/>
<clear-attribute name="xquery.attribute"/>
<set-attribute name="elapsed"
value="{string(seconds-from-duration(util:system-time() - $startTime))}"/>
</forward>
</view>
</dispatch>
default return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- Query is executed by XQueryServlet -->
<forward servlet="XQueryServlet">
<set-header name="Cache-Control" value="no-cache"/>
<!-- Query is passed via the attribute 'xquery.source' -->
<set-attribute name="xquery.source" value="{$query}"/>
<set-attribute name="xquery.module-load-path" value="{$base}"/>
<!-- Errors should be passed through instead of terminating the request -->
<set-attribute name="xquery.report-errors" value="yes"/>
<set-attribute name="start-time" value="{util:system-time()}"/>
</forward>
</dispatch>
(: Retrieve an item from the query results stored in the HTTP session. The
: format of the URL will be /sandbox/results/X, where X is the number of the
: item in the result set :)
else if ($local:method = 'get' and starts-with($exist:path, '/results/')) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="../modules/session.xq">
<set-header name="Cache-Control" value="no-cache"/>
<add-parameter name="num" value="{$exist:resource}"/>
</forward>
</dispatch>
else if ($local:method = 'get' and $exist:resource eq "outline") then
let $query := request:get-parameter("qu", ())
let $base := request:get-parameter("base", ())
return
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- Query is executed by XQueryServlet -->
<forward url="modules/outline.xq">
<set-header name="Cache-Control" value="no-cache"/>
<set-attribute name="xquery.module-load-path" value="{$base}"/>
</forward>
</dispatch>
else if ($exist:resource eq "debug") then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- Query is executed by XQueryServlet -->
<forward url="modules/debuger.xq">
<set-header name="Cache-Control" value="no-cache"/>
</forward>
</dispatch>
else if (ends-with($exist:path, ".xq")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<set-header name="Cache-Control" value="no-cache"/>
<set-attribute name="app-root" value="{$exist:prefix}{$exist:controller}"/>
</dispatch>
else
(: everything else is passed through :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<cache-control cache="yes"/>
</dispatch>