Xml 使用xquery注册exist db用户
我正在尝试创建一个用户注册表单,并在注册时创建集合。 我尝试了以下代码,但当我尝试执行时,出现以下错误: HTTP 400错误请求,无法显示页面。代码如下:Xml 使用xquery注册exist db用户,xml,xquery,exist-db,Xml,Xquery,Exist Db,我正在尝试创建一个用户注册表单,并在注册时创建集合。 我尝试了以下代码,但当我尝试执行时,出现以下错误: HTTP 400错误请求,无法显示页面。代码如下: xquery version "1.0"; declare namespace request="http://exist-db.org/xquery/request"; declare namespace response="http://exist-db.org/xquery/response"; declare namespace
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace response="http://exist-db.org/xquery/response";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
declare variable $database-uri as xs:string { "xmldb:exist:///db" };
declare variable $redirect-uri as xs:anyURI { xs:anyURI("login.xql") };
declare function local:header() as node(){
<div style="background-color:#b0c4de; padding: 20px; border: 1px solid black;">
<h1>test</h1>
</div>
};
declare function local:footer() as node(){
<div style="background color:#eee; height: 1%; padding:20px; border: 1px solid black;">
<p>PROJECT</p>
</div>
};
declare function local:reg($user as xs: string) as element()?
{
let $password := request:get-parameter("password", "")
let $password2 := request:get-parameter("password2", "")
return
if (not(xdb:exists-user($user))) then (
xdb:create-collection($database-uri, $user),
xdb:create-user($user, $password, "guest", ()),
response:redirect-to($redirect-uri)
) else
<p>User already exists!</p>
};
declare function local:do-reg() as element()?
{
let $user := request:get-parameter("user", ())
return
<p>{$user}</p>
if($user) then
local:reg($user)
else ()
};
<html>
<head><title>Registration</title>
</head>
<body>
{local:header()}
<h1>Forma for reg</h1>
<form action="{request:get-uri()}">
User Name: <br />
<input type="text" name="user" size="20" />
<br />
Password: <br />
<input type="password" name="password" size="20" />
<br />
Confirm: <br />
<input type="password" name="password2" size="20" />
<br />
<input type="submit" />
<input type="reset" name="reset" value="Clear" />
</form>
{local:do-reg()}
{local:footer()}
</body>
</html>
xquery版本“1.0”;
声明命名空间请求=”http://exist-db.org/xquery/request";
声明命名空间响应=”http://exist-db.org/xquery/response";
声明命名空间xdb=”http://exist-db.org/xquery/xmldb";
声明选项exist:serialize“method=xhtml媒体类型=text/html缩进=yes”;
将变量$database uri声明为xs:string{“xmldb:exist:///db" };
将变量$redirect uri声明为xs:anyURI{xs:anyURI(“login.xql”)};
将函数local:header()声明为节点(){
测试
};
将函数local:footer()声明为节点(){
计划
};
将函数local:reg($user as xs:string)声明为元素()?
{
let$password:=请求:获取参数(“password”,“”)
让$password2:=请求:获取参数(“password2”,“”)
返回
如果(不是(xdb:exists user($user))),则(
xdb:创建集合($databaseuri,$user),
xdb:创建用户($user,$password,“guest”,()),
响应:重定向到($redirect uri)
)否则
用户已经存在
};
将函数local:do-reg()声明为元素()?
{
let$user:=请求:获取参数(“用户”,())
返回
{$user}
如果($user)那么
本地:注册($user)
else()
};
登记处
{local:header()}
注册表格
用户名:
密码:
确认:
{local:do-reg()}
{local:footer()}
请帮助,我是xquery和exist db的初学者。提前谢谢 我明白了,如果你不在预授权的db组中,你就不能注册新用户。 因此,需要做的是为您的集合创建一个新用户,并向他提供正确的预授权,然后您需要使用该用户登录,以便注册/创建新用户
xmldb:login('/db/yourcollection', $admin, $password)
.
.
.
your registration code
问题是您在.xql文档中添加了管理员用户和密码,这不是一个好主意。但您可以使用:
util:base64编码
及
对您的密码进行编码和解码。所以我是这样做的:
系统:作为用户((util:base64解码(“bXBhcmF2YWM=”),(util:base64解码(“cGFzcw=”)),(如果(不是(xdb:exists user($user))),则( xdb:创建集合($databaseuri,$user), xdb:创建用户($user,$pass,'user',()),
响应:重定向到($redirect uri) )否则 用户存在 )) 还有另一种方法可以做到这一点,甚至更安全,看看
util:base64-decode