2

我正在尝试创建用户注册表单并在注册时创建集合。我尝试了以下代码,但是当我尝试执行时出现此错误: HTTP 400 Bad Request and page cannot be displayed。这是代码:

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 的初学者并且存在数据库。提前致谢!

4

1 回答 1

2

我明白了,如果您不在 db 权限组中,则无法注册新用户。所以需要做的是为您的收藏创建一个新用户,并给他正确的权限,然后您需要使用该用户登录才能注册/创建新用户。

xmldb:login('/db/yourcollection', $admin, $password)
                           .
                           .
                           .
                    your registration code

问题在于您将管理员用户和密码放入 .xql 文档中 - 这不是一个好主意。但是你可以使用:

util:base64 编码

util:base64-decode

对您的密码进行编码和解码。所以我是怎么做到的:

system:as-user((util:base64-decode("bXBhcmF2YWM=")),(util:base64-decode("cGFzcw==")), (
if (not(xdb:exists-user($user)) ) 然后 ( xdb:create-collection($database-uri, $user), xdb:create-user($user, $pass, 'user', ()),
response:redirect-to($redirect-uri) )别的

用户存在!

) )

还有另一种方法可以做到这一点,更安全,看看这个例子

于 2012-11-27T18:54:22.163 回答