dba/login
library moduleRPA
Summary
Code for logging in and out.
- Authors
- Christian Grün, BaseX Team 2005-23, BSD License
Imports
This module is imported by 0 modules. It imports 2 modules.
Variables
None
Functions
4.1 dba:accept
Arities: #2P
dba:accept
(
$name as xs:string, $page as xs:string? ) as element(rest:response)- name
asxs:stringentered username - page
asxs:string?page to redirect to (optional)
element(rest:response)redirection
Invoked by 0 functions from 0 modules
Annotations (1)
%private | () |
Source ( 11 lines)
function dba:accept(
$name as xs:string,
$page as xs:string?
) as element(rest:response) {
(: register user, write log entry :)
session:set($config:SESSION-KEY, $name),
admin:write-log('Login: ' || $name, 'DBA'),
(: redirect to supplied page or main page :)
web:redirect(if($page) then $page else 'logs', html:parameters())
}4.2 dba:check
Arities: #1A
dba:check
(
$perm as map(*) ) as element(rest:response)?- perm
asmap(*)permission data
element(rest:response) ?redirection to login page or empty sequence
Invoked by 0 functions from 0 modules
Annotations (1)
%perm:check | ('/dba','{$perm}') |
Source ( 18 lines)
function dba:check(
$perm as map(*)
) as element(rest:response)? {
let $path := $perm?path
let $allow := $perm?allow
return if ($allow = 'public') then (
(: public function, register id for better log entries :)
request:set-attribute('id', $allow)
) else if (session:get($config:SESSION-KEY)) then (
(: everything fine, user is logged in :)
) else (
(: normalize login path :)
let $target := if(ends-with($path, '/dba')) then 'dba/login' else 'login'
(: last visited page to redirect to (if there was one) :)
let $page := replace($path, '^.*dba/?', '')[.]
return web:redirect($target, html:parameters(map { 'page': $page }))
)
}4.3 dba:login
Arities: #3RA
dba:login
(
$name as xs:string?, $error as xs:string?, $page as xs:string? ) as element()- name
asxs:string?username (optional) - error
asxs:string?error string (optional) - page
asxs:string?page to redirect to (optional)
element()login page or redirection to main page
Invoked by 0 functions from 0 modules
Annotations (6)
%rest:path | ('/dba/login') |
%rest:query-param | ('_name','{$name}') |
%rest:query-param | ('_error','{$error}') |
%rest:query-param | ('_page','{$page}') |
%output:method | ('html') |
%perm:allow | ('public') |
Source ( 41 lines)
function dba:login(
$name as xs:string?,
$error as xs:string?,
$page as xs:string?
) as element() {
(: user is already logged in: redirect to main page :)
if(session:get($config:SESSION-KEY)) then web:redirect('/dba') else
html:wrap(map { 'error': $error },
<tr>
<td>
<form action='login-check' method='post'>
<input type='hidden' name='_page' value='{ $page }'/>
{
map:for-each(html:parameters(), function($key, $value) {
<input type='hidden' name='{ $key }' value='{ $value }'/>
})
}
<div class='small'/>
<table>
<tr>
<td><b>Name:</b></td>
<td>
<input type='text' name='_name' value='{ $name }' id='user'/>
{ html:focus('user') }
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td>{
<input type='password' name='_pass'/>,
' ',
<input type='submit' value='Login'/>
}</td>
</tr>
</table>
</form>
</td>
</tr>
)
}4.4 dba:login-check
Arities: #3RA
dba:login-check
(
$name as xs:string, $pass as xs:string, $page as xs:string? ) as element(rest:response)- name
asxs:stringusername - pass
asxs:stringpassword - page
asxs:string?page to redirect to (optional)
element(rest:response)redirection
Invoked by 0 functions from 0 modules
Annotations (6)
%rest:POST | () |
%rest:path | ('/dba/login-check') |
%rest:query-param | ('_name','{$name}') |
%rest:query-param | ('_pass','{$pass}') |
%rest:query-param | ('_page','{$page}') |
%perm:allow | ('public') |
Source ( 16 lines)
function dba:login-check(
$name as xs:string,
$pass as xs:string,
$page as xs:string?
) as element(rest:response) {
try {
user:check($name, $pass),
if(user:list-details($name)/@permission != 'admin') then (
dba:reject($name, 'Admin credentials required', $page)
) else (
dba:accept($name, $page)
)
} catch user:* {
dba:reject($name, 'Please check your login data', $page)
}
}4.5 dba:logout
Arities: #0R
dba:logout
(
) as element(rest:response)element(rest:response)redirection
Invoked by 0 functions from 0 modules
Annotations (1)
%rest:path | ('/dba/logout') |
Source ( 11 lines)
function dba:logout(
) as element(rest:response) {
let $user := session:get($config:SESSION-KEY)
return (
(: write log entry, redirect to login page :)
admin:write-log('Logout: ' || $user, 'DBA'),
web:redirect('/dba/login', map { '_name': $user })
),
(: deletes the session key :)
session:delete($config:SESSION-KEY)
}4.6 dba:reject
Arities: #3P
dba:reject
(
$name as xs:string, $error as xs:string, $page as xs:string? ) as element(rest:response)- name
asxs:stringentered username - error
asxs:stringerror message - page
asxs:string?page to redirect to (optional)
element(rest:response)redirection
Invoked by 0 functions from 0 modules
Annotations (1)
%private | () |
Source ( 12 lines)
function dba:reject(
$name as xs:string,
$error as xs:string,
$page as xs:string?
) as element(rest:response) {
(: write log entry, redirect to login page :)
admin:write-log('Login denied: ' || $name, 'DBA'),
web:redirect(
'login',
html:parameters(map { 'name': $name, 'error': $error, 'page': $page })
)
}Namespaces
The following namespaces are defined:
| Prefix | Uri |
|---|---|
| admin | http://basex.org/modules/admin |
| config | dba/config |
| dba | dba/login |
| html | dba/html |
| map | http://www.w3.org/2005/xpath-functions/map |
| output | http://www.w3.org/2010/xslt-xquery-serialization |
| perm | http://basex.org/modules/perm |
| rest | http://exquery.org/ns/restxq |
| session | http://basex.org/modules/session |
| user | http://basex.org/modules/user |
| web | http://basex.org/modules/web |
| xs | http://www.w3.org/2001/XMLSchema |
6 RestXQ
Paths defined 3.
| Path | Method | Function |
|---|---|---|
| /dba/login | dba:login#3 | |
| /dba/login-check | POST | dba:login-check#3 |
| /dba/logout | dba:logout#0 |
Source Code
(:~
: Code for logging in and out.
:
: @author Christian Grün, BaseX Team 2005-23, BSD License
:)
module namespace dba = 'dba/login';
import module namespace config = 'dba/config' at 'lib/config.xqm';
import module namespace html = 'dba/html' at 'lib/html.xqm';
(:~
: Permissions: checks the user credentials.
: Redirects to the login page if a user is not logged in, or if the page is not public.
: @param $perm permission data
: @return redirection to login page or empty sequence
:)
declare
%perm:check('/dba', '{$perm}')
function dba:check(
$perm as map(*)
) as element(rest:response)? {
let $path := $perm?path
let $allow := $perm?allow
return if ($allow = 'public') then (
(: public function, register id for better log entries :)
request:set-attribute('id', $allow)
) else if (session:get($config:SESSION-KEY)) then (
(: everything fine, user is logged in :)
) else (
(: normalize login path :)
let $target := if(ends-with($path, '/dba')) then 'dba/login' else 'login'
(: last visited page to redirect to (if there was one) :)
let $page := replace($path, '^.*dba/?', '')[.]
return web:redirect($target, html:parameters(map { 'page': $page }))
)
};
(:~
: Login page.
: @param $name username (optional)
: @param $error error string (optional)
: @param $page page to redirect to (optional)
: @return login page or redirection to main page
:)
declare
%rest:path('/dba/login')
%rest:query-param('_name', '{$name}')
%rest:query-param('_error', '{$error}')
%rest:query-param('_page', '{$page}')
%output:method('html')
%perm:allow('public')
function dba:login(
$name as xs:string?,
$error as xs:string?,
$page as xs:string?
) as element() {
(: user is already logged in: redirect to main page :)
if(session:get($config:SESSION-KEY)) then web:redirect('/dba') else
html:wrap(map { 'error': $error },
<tr>
<td>
<form action='login-check' method='post'>
<input type='hidden' name='_page' value='{ $page }'/>
{
map:for-each(html:parameters(), function($key, $value) {
<input type='hidden' name='{ $key }' value='{ $value }'/>
})
}
<div class='small'/>
<table>
<tr>
<td><b>Name:</b></td>
<td>
<input type='text' name='_name' value='{ $name }' id='user'/>
{ html:focus('user') }
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td>{
<input type='password' name='_pass'/>,
' ',
<input type='submit' value='Login'/>
}</td>
</tr>
</table>
</form>
</td>
</tr>
)
};
(:~
: Checks the user input and redirects to the main page, or back to the login page.
: @param $name username
: @param $pass password
: @param $page page to redirect to (optional)
: @return redirection
:)
declare
%rest:POST
%rest:path('/dba/login-check')
%rest:query-param('_name', '{$name}')
%rest:query-param('_pass', '{$pass}')
%rest:query-param('_page', '{$page}')
%perm:allow('public')
function dba:login-check(
$name as xs:string,
$pass as xs:string,
$page as xs:string?
) as element(rest:response) {
try {
user:check($name, $pass),
if(user:list-details($name)/@permission != 'admin') then (
dba:reject($name, 'Admin credentials required', $page)
) else (
dba:accept($name, $page)
)
} catch user:* {
dba:reject($name, 'Please check your login data', $page)
}
};
(:~
: Ends a session and redirects to the login page.
: @return redirection
:)
declare
%rest:path('/dba/logout')
function dba:logout(
) as element(rest:response) {
let $user := session:get($config:SESSION-KEY)
return (
(: write log entry, redirect to login page :)
admin:write-log('Logout: ' || $user, 'DBA'),
web:redirect('/dba/login', map { '_name': $user })
),
(: deletes the session key :)
session:delete($config:SESSION-KEY)
};
(:~
: Registers a user and redirects to the main page.
: @param $name entered username
: @param $page page to redirect to (optional)
: @return redirection
:)
declare %private function dba:accept(
$name as xs:string,
$page as xs:string?
) as element(rest:response) {
(: register user, write log entry :)
session:set($config:SESSION-KEY, $name),
admin:write-log('Login: ' || $name, 'DBA'),
(: redirect to supplied page or main page :)
web:redirect(if($page) then $page else 'logs', html:parameters())
};
(:~
: Rejects a user and redirects to the login page.
: @param $name entered username
: @param $error error message
: @param $page page to redirect to (optional)
: @return redirection
:)
declare %private function dba:reject(
$name as xs:string,
$error as xs:string,
$page as xs:string?
) as element(rest:response) {
(: write log entry, redirect to login page :)
admin:write-log('Login denied: ' || $name, 'DBA'),
web:redirect(
'login',
html:parameters(map { 'name': $name, 'error': $error, 'page': $page })
)
};