我正在尝试创建用户注册表单并在注册时创建集合。 我尝试了以下代码,但是当我尝试执行时出现此错误: 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 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 和现有数据库的初学者。提前致谢!
最佳答案
我明白了,如果您不在权限数据库组中,则无法注册新用户。 因此,需要做的是为您的集合创建一个新用户,并给予他正确的权限,然后您需要使用该用户登录才能注册/创建新用户。
xmldb:login('/db/yourcollection', $admin, $password)
.
.
.
your registration code
问题在于您将管理员用户和密码放入 .xql 文档中 - 这不是一个好主意。但你可以使用:
util:base64-encode
和
util:base64-decode
对您的密码进行编码和解码。所以我是如何做到的:
system:as-user((util:base64-decode("bXBhcmF2YWM=")),(util:base64-decode("cGFzcw==")), (
if (not(xdb:exists-user($user))) then (
xdb:create-collection($database-uri, $user),
xdb:create-user($user, $pass, '用户', ()),
响应:重定向到($redirect-uri)
) 别的
用户存在!
) )
还有另一种方法可以做到这一点,甚至更安全,看看 this example
关于xml - 使用 xquery 存在数据库用户注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530635/