xquery version "3.0"; declare variable $exist:path external; declare variable $exist:resource external; declare variable $exist:prefix external; declare variable $exist:controller external; (: Determine if the persistent login module is available :) declare variable $login := let $tryImport := try { util:import-module(xs:anyURI("http://exist-db.org/xquery/login"), "login", xs:anyURI("resource:org/exist/xquery/modules/persistentlogin/login.xql")), true() } catch * { false() } return if ($tryImport) then function-lookup(xs:QName("login:set-user"), 3) else local:fallback-login#3 ; (:~ Fallback login function used when the persistent login module is not available. Stores user/password in the HTTP session. :) declare function local:fallback-login($domain as xs:string, $maxAge as xs:dayTimeDuration?, $asDba as xs:boolean) { let $durationParam := request:get-parameter("duration", ()) let $user := request:get-parameter("user", ()) let $password := request:get-parameter("password", ()) let $logout := request:get-parameter("logout", ()) return if ($durationParam) then error(xs:QName("login"), "Persistent login module not enabled in this version of eXist-db") else if ($logout) then session:invalidate() else if ($user) then let $isLoggedIn := xmldb:login("/db", $user, $password, true()) return if ($isLoggedIn and (not($asDba) or xmldb:is-admin-user($user))) then ( session:set-attribute("eXide.user", $user), session:set-attribute("eXide.password", $password), request:set-attribute($domain || ".user", $user), request:set-attribute("xquery.user", $user), request:set-attribute("xquery.password", $password) ) else () else let $user := session:get-attribute("eXide.user") let $password := session:get-attribute("eXide.password") return ( request:set-attribute($domain || ".user", $user), request:set-attribute("xquery.user", $user), request:set-attribute("xquery.password", $password) ) }; if ($exist:path eq '/') then (: : Login a user via AJAX. Just returns a 401 if login fails. :) else if ($exist:resource = 'login') then let $loggedIn := $login("org.exist.login", (), false()) return try { util:declare-option("exist:serialize", "method=json"), if (request:get-attribute("org.exist.login.user")) then (:status>{request:get-attribute("org.exist.login.user")} lorna added below:) {request:get-attribute("org.exist.login.user")} { xmldb:is-admin-user((request:get-attribute("org.exist.login.user"),request:get-attribute("xquery.user"), 'nobody')[1]) } else ( (:LORNA -change response:set-status-code(401), fail:) null null ) } catch * { response:set-status-code(401), {$err:description} } else if ($exist:resource eq "index.html") then else if ($exist:resource eq 'execute') then let $query := request:get-parameter("qu", ()) let $base := request:get-parameter("base", ()) let $startTime := util:system-time() return {$login("org.exist.login", (), false())} (: Retrieve an item from the query results stored in the HTTP session. The : format of the URL will be /sandbox/results/X, where X is the number of the : item in the result set :) else if (starts-with($exist:path, '/results/')) then {$login("org.exist.login", (), false())} else if ($exist:resource eq "outline") then let $query := request:get-parameter("qu", ()) let $base := request:get-parameter("base", ()) return {$login("org.exist.login", (), false())} else if ($exist:resource eq "debug") then {$login("org.exist.login", (), false())} else if (ends-with($exist:path, ".xql")) then {$login("org.exist.login", (), false())} else if (contains($exist:path, "/$shared/")) then else (: everything else is passed through :)