dba/users  library module
UR

Summary

Edit user.
Tags

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

__source : users/user-edit.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 1 modules.

(None)
imports
this
imports

Variables

3.1 $dba:SUB

Summary
Sub category
Type
xs:string

Functions

4.1 dba:user-edit

Arities: dba:user-edit#5UR

Summary
Edits a user.
Signature
dba:user-edit ( $name as xs:string, $newname as xs:string, $pw as xs:string, $perm as xs:string, $info as xs:string )  as empty-sequence()
Parameters
  • name as xs:string user name
  • newname as xs:string new name
  • pw as xs:string password
  • perm as xs:string permission
  • info as xs:string
Return
  • empty-sequence()redirection
Invokes 14 functions from 4 modules
  • util:redirect#2
  • {http://basex.org/modules/user}alter#2
  • {http://basex.org/modules/user}exists#1
  • {http://basex.org/modules/user}grant#2
  • {http://basex.org/modules/user}info#1
  • {http://basex.org/modules/user}list-details#1
  • {http://basex.org/modules/user}password#2
  • {http://basex.org/modules/user}update-info#2
  • {http://www.w3.org/2001/XMLSchema}QName#1
  • {http://www.w3.org/2005/xpath-functions}deep-equal#2
  • {http://www.w3.org/2005/xpath-functions}error#1
  • {http://www.w3.org/2005/xpath-functions}error#2
  • {http://www.w3.org/2005/xpath-functions}not#1
  • {http://www.w3.org/2005/xpath-functions}parse-xml#1
Invoked by 0 functions from 0 modules
    Annotations
    %updating()
    %rest:POST()
    %rest:path('/dba/user-edit')
    %rest:form-param('name','{$name}')
    %rest:form-param('newname','{$newname}')
    %rest:form-param('pw','{$pw}')
    %rest:form-param('perm','{$perm}')
    %rest:form-param('info','{$info}')
    Source ( 40 lines)
    function dba:user-edit(
      $name     as xs:string,
      $newname  as xs:string,
      $pw       as xs:string,
      $perm     as xs:string,
      $info     as xs:string
    ) as empty-sequence() {
      try {
        let $old := user:list-details($name)
        return (
          (: change name of user :)
          if($name = $newname) then () else (
            if(user:exists($newname)) then (
               error((), 'User already exists.')
             ) else (
               user:alter($name, $newname)
            )
          ),
          (: change password :)
          if($pw = '') then () else user:password($name, $pw),
          (: change permissions :)
          if($perm = $old/@permission) then () else user:grant($name, $perm),
          (: change user info :)
          let $xml := if($info) then (
            parse-xml($info)/*[self::info or error(xs:QName(err:FORC0006))]
          ) else (
            <info/>
          )
          where not(deep-equal(user:info($name), $xml))
          return user:update-info($xml, $name)
        ),
        util:redirect($dba:SUB, map { 'name': $newname, 'info': 'User was saved.' })
      } catch * {
        let $error := if ($err:code != xs:QName('err:FODC0006')) then $err:description else
          'Information must be XML with an info root element.'
        return util:redirect($dba:SUB, map {
          'name': $name, 'newname': $newname, 'pw': $pw, 'perm': $perm, 'error': $error
        })
      }
    }

    Namespaces

    The following namespaces are defined:

    PrefixUri
    annhttp://www.w3.org/2012/xquery
    dbadba/users 2 3 4 5 6 7
    resthttp://exquery.org/ns/restxq
    utildba/util

    6 RestXQ

    Paths defined 1.

    PathMethodFunction
    /dba/user-editPOSTdba:user-edit#5

    Source Code

    (:~
     : Edit user.
     :
     : @author Christian Grün, BaseX Team 2005-21, BSD License
     :)
    module namespace dba = 'dba/users';
    
    import module namespace util = 'dba/util' at '../lib/util.xqm';
    
    (:~ Sub category :)
    declare variable $dba:SUB := 'user';
    
    (:~
     : Edits a user.
     : @param  $name     user name
     : @param  $newname  new name
     : @param  $pw       password
     : @param  $perm     permission
     : @return redirection
     :)
    declare
      %updating
      %rest:POST
      %rest:path('/dba/user-edit')
      %rest:form-param('name',    '{$name}')
      %rest:form-param('newname', '{$newname}')
      %rest:form-param('pw',      '{$pw}')
      %rest:form-param('perm',    '{$perm}')
      %rest:form-param('info',    '{$info}')
    function dba:user-edit(
      $name     as xs:string,
      $newname  as xs:string,
      $pw       as xs:string,
      $perm     as xs:string,
      $info     as xs:string
    ) as empty-sequence() {
      try {
        let $old := user:list-details($name)
        return (
          (: change name of user :)
          if($name = $newname) then () else (
            if(user:exists($newname)) then (
               error((), 'User already exists.')
             ) else (
               user:alter($name, $newname)
            )
          ),
          (: change password :)
          if($pw = '') then () else user:password($name, $pw),
          (: change permissions :)
          if($perm = $old/@permission) then () else user:grant($name, $perm),
          (: change user info :)
          let $xml := if($info) then (
            parse-xml($info)/*[self::info or error(xs:QName(err:FORC0006))]
          ) else (
            <info/>
          )
          where not(deep-equal(user:info($name), $xml))
          return user:update-info($xml, $name)
        ),
        util:redirect($dba:SUB, map { 'name': $newname, 'info': 'User was saved.' })
      } catch * {
        let $error := if ($err:code != xs:QName('err:FODC0006')) then $err:description else
          'Information must be XML with an info root element.'
        return util:redirect($dba:SUB, map {
          'name': $name, 'newname': $newname, 'pw': $pw, 'perm': $perm, 'error': $error
        })
      }
    };