跳至內容

XRX/登入和會話管理

來自Wikibooks,開放世界中的開放書籍
< XRX

您希望使用XForms應用程式獲取登入資訊並設定伺服器會話變數。

XForms標準有一個“secret”屬性用於收集密碼。使用者填寫完登入表單後,它會被POST到伺服器。

請注意,您不應將HTTP GET用於密碼,因為密碼將作為URL引數出現在Web日誌檔案中。

eXist系統有幾個用於設定會話變數的函式。使用者登入後,應設定這些會話變數,並且所有後續的XQuery在訪問安全資源時都可以使用這些會話變數。

請注意,設定會話變數超出了W3C XQuery 1.0標準的範圍,每個伺服器可能使用這些函式的細微變化。但概念應該非常相似。

最常見的是,會話變數用於將使用者與一個或多個角色關聯起來。這被稱為基於角色的訪問控制(RBAC)。這允許您的XQuery根據使用者的角色設定條件行為,並避免必須根據可能頻繁更改的使用者名稱硬編碼XQuery。一個典型的角色是“管理員”角色或“文件審批者”角色。eXist使用UNIX風格的組,可以將其與集合或檔案關聯。如果您注意到集合或檔案在任何時間只能與一個組關聯,則可以使用這些組進行安全控制。就像在UNIX中使用者可以屬於多個組一樣,使用者在會話期間經常與多個角色關聯。

XForms登入介面示例

[編輯 | 編輯原始碼]

此處提供了一個示例登入XForms應用程式

XForms登入介面

此表單可以自定義,以包含任何關於使用未經授權系統的合法免責宣告。然後,它可以包裝在一個XQuery函式中,例如local:display-login(),並在使用者訪問他們無權訪問的頁面時呼叫。

檢查登入的XQuery示例

[編輯 | 編輯原始碼]
(: This is juat a rough outline based on the admin.xql program.  Needs more work... :)

let $user := xdb:get-current-user()
let $pass := local:get-pass()
let $logout := local:get-login()

let $isLoggedIn :=  if($user eq "guest")
   then
    (
    (: is this a login attempt? :)
        if($user and not(empty($pass)))
    then
        (
         if($user eq "guest")
          then
            (
                (: prevent the guest user from accessing the admin webapp :)
                false()
            )
            else
            (
                (: try and log the user in :)
                xdb:login("/db", $user, $pass)
            )
        )
        else
        (
            (: prevent the guest user from accessing the admin webapp :)
            false()
        )
    )
    else
    (
        (: if we are already logged in, are we logging out - i.e. set permissions back to guest :)
        if  ($logout)
           then
        (
        	let $null := xdb:login("/db", "guest", "guest") return
        	    false()
        )
        else
        (
             (: we are already logged in and we are not the guest user :)
            true()
        )
    )
return
<html>
...
</html>

顯示會話資訊的XQuery函式

[編輯 | 編輯原始碼]

使用者登入後,可以使用以下函式在螢幕右上角顯示會話資訊。

(: 
    Display the version, SVN revision and user info in the top right corner 
:)
declare function admin:info-header() as element()
{
    <div class="info">
        <ul>
            <li>Version: { util:system-property( "product-version" ) }</li>
            <li>SVN Revision: { util:system-property( "svn-revision" ) }</li>
            <li>Build: {util:system-property("product-build")}</li>
            <li>User: { xdb:get-current-user() }</li>
        </ul>
    </div>
};

上一頁:儲存檔案對話方塊 下一頁:檔案鎖定

華夏公益教科書