-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xqm
220 lines (201 loc) · 6.11 KB
/
index.xqm
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
xquery version '3.0' ;
module namespace xpr.session = "xpr.session" ;
(:~
: This xquery module is an application for xpr
:
: @author emchateau & sardinecan (ANR Experts)
: @since 2019-01
: @licence GNU http://www.gnu.org/licenses
: @version 0.2
:
: xpr is free software: you can redistribute it and/or modify
: it under the terms of the GNU General Public License as published by
: the Free Software Foundation, either version 3 of the License, or
: (at your option) any later version.
:
:)
import module namespace Session = 'http://basex.org/modules/session';
import module namespace xpr.xpr = 'xpr.xpr' at '../xpr/xpr.xqm' ;
import module namespace G = 'xpr.globals' at '../xpr/globals.xqm' ;
import module namespace xpr.mappings.html = 'xpr.mappings.html' at '../xpr/mappings.html.xqm' ;
import module namespace xpr.models.xpr = 'xpr.models.xpr' at '../xpr/models.xpr.xqm' ;
import module namespace xpr.models.networks = 'xpr.models.networks' at '../xpr/models.networks.xqm' ;
declare namespace rest = "http://exquery.org/ns/restxq" ;
declare namespace file = "http://expath.org/ns/file" ;
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization" ;
declare namespace db = "http://basex.org/modules/db" ;
declare namespace web = "http://basex.org/modules/web" ;
declare namespace update = "http://basex.org/modules/update" ;
declare namespace perm = "http://basex.org/modules/perm" ;
declare namespace user = "http://basex.org/modules/user" ;
declare namespace session = 'http://basex.org/modules/session' ;
declare namespace http = "http://expath.org/ns/http-client" ;
declare namespace ev = "http://www.w3.org/2001/xml-events" ;
declare namespace eac = "eac" ;
declare namespace map = "http://www.w3.org/2005/xpath-functions/map" ;
declare namespace xf = "http://www.w3.org/2002/xforms" ;
declare namespace xlink = "http://www.w3.org/1999/xlink" ;
declare namespace xpr = "xpr" ;
declare default element namespace "xpr" ;
declare default function namespace "xpr.xpr" ;
declare default collation "http://basex.org/collation?lang=fr" ;
(:~ Login page (visible to everyone). :)
declare
%rest:path("xpr/login")
%output:method("html")
function xpr.session:login() {
<html>
Please log in:
<form action="/xpr/login/check" method="post">
<input name="name"/>
<input type="password" name="pass"/>
<input type="submit"/>
</form>
</html>
};
(:~ Main page (restricted to logged in users). :)
declare
%rest:path("/main")
%output:method("html")
function xpr.session:main() {
<html>
Welcome to the main page:
<a href='/main/admin'>admin area</a>,
<a href='/logout'>log out</a>.
</html>
};
(:~ Admin page. :)
declare
%rest:path("/main/admin")
%output:method("html")
%perm:allow("admin")
function xpr.session:admin() {
<html>
Welcome to the admin page. You are {fn:string(user:list-details(Session:get('id'))/@name)}
</html>
};
(:~ Admin page. :)
declare
%rest:path("/who")
%output:method("html")
function xpr.session:who() {
<html>
Welcome to the who page. You are
<code>{user:list-details(Session:get('id'))}</code>
</html>
};
(:~
: Global permission checks.
: Rejects any usage of the HTTP DELETE method.
:)
declare
%perm:check
%rest:DELETE
function xpr.session:check() {
fn:error((), 'Access denied to DELETE method.')
};
(:~
: Permission check: Area for logged-in users.
: Checks if a session id exists for the current user; if not, redirects to the login page.
:)
declare
%perm:check('/main')
function xpr.session:check-app() {
let $user := Session:get('id')
where fn:empty($user)
return web:redirect('/')
};
(:~
: Permissions: Admin area.
: Checks if the current user is admin; if not, redirects to the main page.
: @param $perm map with permission data
:)
declare
%perm:check('/main/admin', '{$perm}')
function xpr.session:check-admin($perm) {
let $user := Session:get('id')
where fn:not(user:list-details($user)/@permission = $perm?allow)
return web:redirect('/main')
};
(:~
: Permissions: Admin area.
: Checks if the current user is admin; if not, redirects to the main page.
: @param $perm map with permission data
:)
declare
%perm:check('xpr/expertises/new', '{$perm}')
function xpr.session:checkExpertiseRight($perm) {
let $user := Session:get('id')
where fn:empty($user) or fn:not(user:list-details($user)/*:info/*:grant/@type = $perm?allow)
return web:redirect('/xpr/login')
};
declare
%rest:path("xpr/login/check")
%rest:query-param("name", "{$name}")
%rest:query-param("pass", "{$pass}")
function xpr.session:login($name, $pass) {
try {
user:check($name, $pass),
Session:set('id', $name),
web:redirect("/main")
} catch user:* {
web:redirect("/")
}
};
declare
%rest:path("xpr/logout")
function xpr.session:logout() {
Session:delete('id'),
web:redirect("/")
};
(:~
: This function creates a new user
: @return an xforms to create a new user
:)
declare
%rest:path("xpr/users/new")
%output:method("xml")
function xpr.session:newUser() {
let $content := map {
'instance' : '',
'model' : 'xprUserModel.xml',
'trigger' : '',
'form' : 'xprUserForm.xml'
}
let $outputParam := map {
'layout' : "template.xml"
}
return
(processing-instruction xml-stylesheet { fn:concat("href='", $G:xsltFormsPath, "'"), "type='text/xsl'"},
<?css-conversion no?>,
xpr.models.xpr:wrapper($content, $outputParam)
)
};
(:~
: This function creates new user
:)
declare
%rest:path("xpr/users/put")
%output:method("xml")
%rest:header-param("Referer", "{$referer}", "none")
%rest:PUT("{$param}")
%updating
function xpr.session:putUser($param, $referer) {
let $db := db:open("xpr")
let $user := $param
let $userName := fn:normalize-space($user/*:user/*:name)
let $userPwd := fn:normalize-space($user/*:user/*:password)
let $userPermission := fn:normalize-space($user/*:user/*:permission)
let $userInfo :=
<info xmlns="">{
for $right in $user/*:user/*:info/*:grant
return <grant type="{$right/@type}">{fn:normalize-space($right)}</grant>
}</info>
return
user:create(
$userName,
$userPwd,
$userPermission,
'xpr',
$userInfo)
};