dba/users
library moduleURA
Summary
Create new user.
- Tags
Author: Christian Grün, BaseX Team 2005-21, BSD License
__source : users/user-create.xqm
Imports
This module is imported by 0 modules. It imports 3 modules.
Variables
Functions
4.1 dba:user-create
Arities: dba:user-create#3URdba:user-create#4RA
dba:user-create
(
$name
as
xs:string
, $pw
as
xs:string
, $perm
as
xs:string
)
as
empty-sequence()
dba:user-create
(
$name
as
xs:string?
, $pw
as
xs:string?
, $perm
as
xs:string
, $error
as
xs:string?
)
as
element(html)
- name
as
xs:string
user name - pw
as
xs:string
password - perm
as
xs:string
permission
empty-sequence()
redirection
Invokes 8 functions from 4 modules
- html:button#2
- html:focus#1
- html:link#2
- html:wrap#2
- util:redirect#2
- {http://basex.org/modules/user}create#3
- {http://basex.org/modules/user}exists#1
- {http://www.w3.org/2005/xpath-functions}error#2
Invoked by 0 functions from 0 modules
Annotations
%rest:GET | () |
%rest:path | ('/dba/user-create') |
%rest:query-param | ('name','{$name}') |
%rest:query-param | ('pw','{$pw}') |
%rest:query-param | ('perm','{$perm}','none') |
%rest:query-param | ('error','{$error}') |
%output:method | ('html') |
Annotations
%updating | () |
%rest:POST | () |
%rest:path | ('/dba/user-create') |
%rest:query-param | ('name','{$name}') |
%rest:query-param | ('pw','{$pw}') |
%rest:query-param | ('perm','{$perm}') |
Source ( 70 lines)
function dba:user-create(
$name as xs:string,
$pw as xs:string,
$perm as xs:string
) as empty-sequence() {
try {
if(user:exists($name)) then (
error((), 'User already exists.')
) else (
user:create($name, $pw, $perm)
),
util:redirect($dba:CAT, map { 'info': 'User was created.' })
} catch * {
util:redirect('user-create', map {
'name': $name, 'pw': $pw, 'perm': $perm, 'error': $err:description
})
}
}
function dba:user-create(
$name as xs:string?,
$pw as xs:string?,
$perm as xs:string,
$error as xs:string?
) as element(html) {
html:wrap(map { 'header': $dba:CAT, 'error': $error },
<tr>
<td>
<form action='user-create' method='post' autocomplete='off'>
<!-- force chrome not to autocomplete form -->
<input style='display:none' type='text' name='fake1'/>
<input style='display:none' type='password' name='fake2'/>
<h2>{
html:link('Users', $dba:CAT), ' » ',
html:button('create', 'Create')
}</h2>
<!-- dummy value; prevents reset of options when nothing is selected -->
<input type='hidden' name='opts' value='x'/>
<table>
<tr>
<td>Name:</td>
<td>
<input type='text' name='name' value='{ $name }' id='name'/>
{ html:focus('name') }
<div class='small'/>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type='password' name='pw' value='{ $pw }' id='pw'
autocomplete='new-password'/>
<div class='small'/>
</td>
</tr>
<tr>
<td>Permission:</td>
<td>
<select name='perm' size='5'>{
for $p in $options:PERMISSIONS
return element option { attribute selected { }[$p = $perm], $p }
}</select>
<div class='small'/>
</td>
</tr>
</table>
</form>
</td>
</tr>
)
}
Namespaces
The following namespaces are defined:
Prefix | Uri |
---|---|
ann | http://www.w3.org/2012/xquery |
dba | dba/users 2 3 4 5 6 7 |
html | dba/html |
options | dba/options |
output | http://www.w3.org/2010/xslt-xquery-serialization |
rest | http://exquery.org/ns/restxq |
util | dba/util |
6 RestXQ
Paths defined 2.
Path | Method | Function |
---|---|---|
/dba/user-create | GET | dba:user-create#4 |
/dba/user-create | POST | dba:user-create#3 |
Source Code
(:~
: Create new user.
:
: @author Christian Grün, BaseX Team 2005-21, BSD License
:)
module namespace dba = 'dba/users';
import module namespace html = 'dba/html' at '../lib/html.xqm';
import module namespace options = 'dba/options' at '../lib/options.xqm';
import module namespace util = 'dba/util' at '../lib/util.xqm';
(:~ Top category :)
declare variable $dba:CAT := 'users';
(:~
: Form for creating a new user.
: @param $name entered user name
: @param $pw entered password
: @param $perm chosen permission
: @param $error error string
: @return page
:)
declare
%rest:GET
%rest:path('/dba/user-create')
%rest:query-param('name', '{$name}')
%rest:query-param('pw', '{$pw}')
%rest:query-param('perm', '{$perm}', 'none')
%rest:query-param('error', '{$error}')
%output:method('html')
function dba:user-create(
$name as xs:string?,
$pw as xs:string?,
$perm as xs:string,
$error as xs:string?
) as element(html) {
html:wrap(map { 'header': $dba:CAT, 'error': $error },
<tr>
<td>
<form action='user-create' method='post' autocomplete='off'>
<!-- force chrome not to autocomplete form -->
<input style='display:none' type='text' name='fake1'/>
<input style='display:none' type='password' name='fake2'/>
<h2>{
html:link('Users', $dba:CAT), ' » ',
html:button('create', 'Create')
}</h2>
<!-- dummy value; prevents reset of options when nothing is selected -->
<input type='hidden' name='opts' value='x'/>
<table>
<tr>
<td>Name:</td>
<td>
<input type='text' name='name' value='{ $name }' id='name'/>
{ html:focus('name') }
<div class='small'/>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type='password' name='pw' value='{ $pw }' id='pw'
autocomplete='new-password'/>
<div class='small'/>
</td>
</tr>
<tr>
<td>Permission:</td>
<td>
<select name='perm' size='5'>{
for $p in $options:PERMISSIONS
return element option { attribute selected { }[$p = $perm], $p }
}</select>
<div class='small'/>
</td>
</tr>
</table>
</form>
</td>
</tr>
)
};
(:~
: Creates a user.
: @param $name user name
: @param $pw password
: @param $perm permission
: @return redirection
:)
declare
%updating
%rest:POST
%rest:path('/dba/user-create')
%rest:query-param('name', '{$name}')
%rest:query-param('pw', '{$pw}')
%rest:query-param('perm', '{$perm}')
function dba:user-create(
$name as xs:string,
$pw as xs:string,
$perm as xs:string
) as empty-sequence() {
try {
if(user:exists($name)) then (
error((), 'User already exists.')
) else (
user:create($name, $pw, $perm)
),
util:redirect($dba:CAT, map { 'info': 'User was created.' })
} catch * {
util:redirect('user-create', map {
'name': $name, 'pw': $pw, 'perm': $perm, 'error': $err:description
})
}
};