Main Page » XQuery » Functions » User Functions

User Functions

This module contains functions for creating and administering database users. The User Management article provides more information on database users and permissions.

Conventions

All functions and errors are in the http://basex.org/modules/user namespace, to which the user prefix is statically bound.

Read Operations

user:current

Signature
user:current() as xs:string
SummaryReturns the name of the currently logged-in user.
Examples
user:current()
Always returns admin if the standalone mode or the GUI is used.

user:list

Signature
user:list() as xs:string*
SummaryReturns the names of all registered users who are visible to the current user, sorted by name.
Examples
user:list()
Only returns admin after a fresh installation.

user:list-details

Signature
user:list-details(  $name  as xs:string  := ()) as element(user)*admin
SummaryReturns an element sequence, containing all registered users who are visible to the current user, sorted by name. In addition to the SHOW USERS command, encoded password strings and database permissions will be output. A user $name can be specified to filter the results in advance. admin permissions are required, unless the addressed user is the current one.
Errors
unknownThe specified user does not exist.
Examples
user:list-details()
Returns output similar to this one after a fresh installation. The result:
<user name="admin" permission="admin">
  <password algorithm="salted-sha256">
    <salt>***</salt>
    <hash>***</hash>
  </password>
</user>

user:exists

Signature
user:exists(  $name  as xs:string) as xs:booleanadmin
SummaryChecks if a user with the specified $name exists.
Errors
nameThe specified username is invalid.
Examples
user:exists('admin')
Always yields true.

user:check

Signature
user:check(  $name      as xs:string,  $password  as xs:string) as empty-sequence()admin
SummaryChecks if the specified user and password are correct. Raises errors otherwise. admin permissions are required, unless the addressed user is the current one.
Errors
nameThe specified username is invalid.
passwordThe specified password is wrong.
unknownThe specified user does not exist.
Examples
user:check('admin', '')
Raises an error if the password of the admin user is a non-empty string.

user:info

Signature
user:info(  $name  as xs:string  := ()) as element(info)admin
SummaryReturns an info element, which may contain application-specific data. If a user $name is supplied, a user-specific element is returned. By default, the returned element has no contents. It can be modified via user:update-info. admin permissions are required, unless the addressed user is the current one.
Examples
user:info()
Returns <info/> after a fresh installation.

Updates

Important note: All functions in this section are updating functions: they will not be immediately executed, but queued on the Pending Update List, which will be processed after the actual query has been evaluated. This means that the order in which the functions are specified in the query usually does not reflect the order in which the code will be evaluated.

user:create

Signature
user:create(  $name         as xs:string,  $password     as xs:string,  $permissions  as xs:string*  := (),  $patterns     as xs:string*  := (),  $info         as element(info)  := ()) as empty-sequence()admin
SummaryCreates a new user with the specified $name, $password, and $permissions:
  • Local permissions are granted with non-empty glob $patterns.
  • An $info element with application-specific information can be supplied.
  • The default global permission (none) can be overridden with an empty pattern or by omitting the $patterns argument.
  • Existing users will be overwritten.
Errors
adminThe "admin" user cannot be modified.
logged-inThe specified user is currently logged in.
nameThe specified username is invalid.
permissionThe specified permission is invalid.
updateThe operation can only be performed once per user or database pattern.
Examples
user:create('John', '7e$j#!187?aZX81', 'admin')
Creates a new user John with admin permissions.
user:create('Jack', 'top!secret', ('read', 'write'), ('data*', 'index*'))
Creates a new user Jack with read permissions for databases starting with the letters data, and write permissions for databases starting with index.

user:grant

Signature
user:grant(  $name         as xs:string,  $permissions  as xs:string*,  $patterns     as xs:string*  := ()) as empty-sequence()admin
SummaryGrants global or local $permissions to a user with the specified $name. Local permissions are granted with non-empty glob $patterns.
Errors
adminThe "admin" user cannot be modified.
localA local permission can only be 'none', 'read' or 'write'.
logged-inThe specified user is currently logged in.
nameThe specified username is invalid.
patternThe specified database name is invalid.
permissionThe specified permission is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.
Examples
user:grant('John', 'create')
Grants create permissions to the user 'John'.
user:grant('John', ('read','write'), ('index*','unit*'))
Allows John to read all databases starting with the letters 'index', and to write to all databases starting with 'unit'.

user:drop

Signature
user:drop(  $name      as xs:string,  $patterns  as xs:string*  := ()) as empty-sequence()admin
SummaryDrops a user with the specified $name. If non-empty glob $patterns are specified, only the database patterns will be removed. admin permissions are required, unless the addressed user is the current one.
Errors
adminThe "admin" user cannot be modified.
conflictA user cannot be both altered and dropped.
logged-inThe specified user is currently logged in.
nameThe specified username is invalid.
patternThe specified database name is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.
Examples
user:drop('John')
Drops the user 'John'.
user:drop('John', 'unit*')
Removes the unit* database pattern. If John accesses any of these databases, their global permission will be checked again.

user:alter

Signature
user:alter(  $name     as xs:string,  $newname  as xs:string) as empty-sequence()admin
SummaryRenames a user with the specified $name to $newname.
Errors
adminThe "admin" user cannot be modified.
conflictA user cannot be both altered and dropped.
logged-inThe specified user is currently logged in.
nameThe specified username is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.
Examples
user:alter('John', 'Jack')
Renames the user 'John' to 'Jack'.

user:password

Signature
user:password(  $name      as xs:string,  $password  as xs:string) as empty-sequence()admin
SummaryChanges the password of a user with the specified $name. admin permissions are required, unless the addressed user is the current one.
Errors
nameThe specified username is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.
Examples
user:password('John', '')
Assigns user 'John' an empty password string.

user:update-info

Signature
user:update-info(  $info  as element(info),  $name  as xs:string  := ()) as empty-sequence()admin
SummaryAssigns the specified $info element to the user management or, if $name is supplied, to a specific user. This function can be used to manage application-specific data (groups, enhanced user info, etc.). admin permissions are required, unless the addressed user is the current one.
Errors
infoThe global info element can only be updated once.
nameThe specified username is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.
Examples
user:update-info(element info {
  for $group in ('editor', 'author', 'writer')
  return element group { $group }
})
Stores initial groups information.
user:update-info(<info group='editor'/>, 'john')
Adds a group to a specific user.

Errors

CodeDescription
adminThe "admin" user cannot be modified.
conflictA user cannot be both altered and dropped.
infoThe global info element can only be updated once.
localA local permission can only be 'none', 'read' or 'write'.
logged-inThe specified user is currently logged in.
nameThe specified username is invalid.
passwordThe specified password is wrong.
patternThe specified database name is invalid.
permissionThe specified permission is invalid.
unknownThe specified user does not exist.
updateThe operation can only be performed once per user or database pattern.

Changelog

Version 8.6Version 8.4Version 8.1Version 8.0
  • Added: New module added.

⚡Generated with XQuery