dba/users  library module
URA

Summary

Create new user.
Tags

Author: Christian Grün, BaseX Team 2005-21, BSD License

__source : users/user-create.xqm

Related documents
ViewDescriptionFormat
xqdocxqDoc xml file from the source modulexml
xqparsexqparse xml file from the source modulexml

Imports

This module is imported by 0 modules. It imports 3 modules.

(None)
imports
this
imports

Variables

3.1 $dba:CAT

Summary
Top category
Type
xs:string

Functions

4.1 dba:user-create

Arities: dba:user-create#3URdba:user-create#4RA

Summary
Form for creating a new user.
Signature
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)
Parameters
  • name as xs:string user name
  • pw as xs:string password
  • perm as xs:string permission
Return
  • empty-sequence()redirection
Invokes 8 functions from 4 modules
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:

    PrefixUri
    annhttp://www.w3.org/2012/xquery
    dbadba/users 2 3 4 5 6 7
    htmldba/html
    optionsdba/options
    outputhttp://www.w3.org/2010/xslt-xquery-serialization
    resthttp://exquery.org/ns/restxq
    utildba/util

    6 RestXQ

    Paths defined 2.

    PathMethodFunction
    /dba/user-createGETdba:user-create#4
    /dba/user-createPOSTdba: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
        })
      }
    };