dba/users  library module
URP

Summary

Updates user information.
Authors
  • Christian Grün, BaseX Team 2005-23, BSD License
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
dba/users
imports

Variables

3.1 $dba:CAT

Summary
Top category
Type
Source ( 1 lines)
variable $dba:CAT := 'users'

3.2 $dba:SUB

Summary
Sub category
Type
Source ( 1 lines)
variable $dba:SUB := 'user'

Functions

4.1 dba:user-info

Arities: #1P

Summary
Converts a user info string to XML.
Signatures
dba:user-info ( $info as xs:string ) as element(info)
Parameters
  • info as xs:string user info
Return
  • element(info) info element
Invoked by 0 functions from 0 modules
    Annotations (1)
    %private()
    Source ( 11 lines)
    function dba:user-info(
      $info  as xs:string
    ) as element(info) {
      if($info) then (
        parse-xml($info)/*[self::info or error(xs:QName(err:FODC0006))] update {
          delete node .//text()[not(normalize-space())]
        }
      ) else (
        element info {}
      )
    }

    4.2 dba:user-update

    Arities: #5UR

    Summary
    Updates a user.
    Signatures
    dba:user-update ( $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 username
    • newname as xs:string new name
    • pw as xs:string password
    • perm as xs:string permission
    • info as xs:string
    Return
    • empty-sequence redirection
    Invoked by 0 functions from 0 modules
      Annotations (8)
      %updating()
      %rest:POST()
      %rest:path('/dba/user-update')
      %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 ( 36 lines)
      function dba:user-update(
        $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 := dba:user-info($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 updated.' })
        } catch * {
          let $error := if ($err:code != xs:QName('err:FODC0006')) then $err:description else
            'XML with "info" root element expected.'
          return util:redirect($dba:SUB, map {
            'name': $name, 'newname': $newname, 'pw': $pw, 'perm': $perm, 'error': $error
          })
        }
      }

      4.3 dba:users-info

      Arities: #1UR

      Summary
      Updates users information.
      Signatures
      dba:users-info ( $info as xs:string ) as empty-sequence
      Parameters
      • info as xs:string users information
      Return
      • empty-sequence redirection
      Invoked by 0 functions from 0 modules
        Annotations (4)
        %updating()
        %rest:POST()
        %rest:path('/dba/users-info')
        %rest:form-param('info','{$info}')
        Source ( 16 lines)
        function dba:users-info(
          $info  as xs:string
        ) as empty-sequence() {
          try {
            (: change user info :)
            let $xml := dba:user-info($info)
            where not(deep-equal(user:info(), $xml))
            return user:update-info($xml),
        
            util:redirect($dba:CAT, map { 'info': 'User information was updated.' })
          } catch err:FODC0006 {
            util:redirect($dba:CAT, map { 'error': 'XML with "info" root element expected.' })
          } catch * {
            util:redirect($dba:CAT, map { 'error': $err:description })
          }
        }

        Namespaces

        The following namespaces are defined:

        PrefixUri
        dbadba/users 2 3 4 5 6 7
        errhttp://www.w3.org/2005/xqt-errors
        resthttp://exquery.org/ns/restxq
        userhttp://basex.org/modules/user
        utildba/util
        xshttp://www.w3.org/2001/XMLSchema

        6 RestXQ

        Paths defined 2.

        PathMethodFunction
        /dba/user-updatePOSTdba:user-update#5
        /dba/users-infoPOSTdba:users-info#1

        Source Code

        (:~
         : Updates user information.
         :
         : @author Christian Grün, BaseX Team 2005-23, BSD License
         :)
        module namespace dba = 'dba/users';
        
        import module namespace util = 'dba/util' at '../lib/util.xqm';
        
        (:~ Top category :)
        declare variable $dba:CAT := 'users';
        (:~ Sub category :)
        declare variable $dba:SUB := 'user';
        
        (:~
         : Updates users information.
         : @param  $info  users information
         : @return redirection
         :)
        declare
          %updating
          %rest:POST
          %rest:path('/dba/users-info')
          %rest:form-param('info', '{$info}')
        function dba:users-info(
          $info  as xs:string
        ) as empty-sequence() {
          try {
            (: change user info :)
            let $xml := dba:user-info($info)
            where not(deep-equal(user:info(), $xml))
            return user:update-info($xml),
        
            util:redirect($dba:CAT, map { 'info': 'User information was updated.' })
          } catch err:FODC0006 {
            util:redirect($dba:CAT, map { 'error': 'XML with "info" root element expected.' })
          } catch * {
            util:redirect($dba:CAT, map { 'error': $err:description })
          }
        };
        
        (:~
         : Updates a user.
         : @param  $name     username
         : @param  $newname  new name
         : @param  $pw       password
         : @param  $perm     permission
         : @return redirection
         :)
        declare
          %updating
          %rest:POST
          %rest:path('/dba/user-update')
          %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-update(
          $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 := dba:user-info($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 updated.' })
          } catch * {
            let $error := if ($err:code != xs:QName('err:FODC0006')) then $err:description else
              'XML with "info" root element expected.'
            return util:redirect($dba:SUB, map {
              'name': $name, 'newname': $newname, 'pw': $pw, 'perm': $perm, 'error': $error
            })
          }
        };
        
        (:~
         : Converts a user info string to XML.
         : @param  $info  user info
         : @return info element
         :)
        declare %private function dba:user-info(
          $info  as xs:string
        ) as element(info) {
          if($info) then (
            parse-xml($info)/*[self::info or error(xs:QName(err:FODC0006))] update {
              delete node .//text()[not(normalize-space())]
            }
          ) else (
            element info {}
          )
        };