[ignore] update Users admin panel to use new location & structure for users (/db/system/security/exist/accounts/ instead of /db/system/users.xml)

svn path=/trunk/eXist/; revision=13878

xquery version "1.0";
(: $Id$ :)
(:
    Module: display and edit users.
:)

module namespace users="http://exist-db.org/xquery/admin-interface/users";

declare namespace util="http://exist-db.org/xquery/util";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace config="http://exist-db.org/Configuration";

declare function users:main() as element()
{

User Management

Note: this is work in progress. Don't use this page to manage
        users in a production environment. Use the Java client instead.

        {
            if(not(xdb:is-admin-user(xdb:get-current-user()))) then
            (

                    Only users of the "dba" group can change user settings.

            )
            else
            (
                users:process()
            )
        }

};

declare function users:process() as element()*
{
    let $action := request:get-parameter("action", "") return
        if($action eq "Create") then
        (
            users:new-user()
        )
        else if($action eq "Change") then
        (
            users:update-user()
        )
        else if($action eq "Remove") then
        (
            users:remove-user()
        )
        else
        (
            users:display()
        )
};

declare function users:remove-user() as element()*
{
    let $uid := request:get-parameter("uid", ()),
    $name := collection("/db/system/security/exist/accounts")/config:account[util:collection-name(.) ne '/db/system/security/exist/accounts/removed'][@id = $uid]/config:name return

        if($name eq xdb:get-current-user()) then
        (

You cannot remove the current user!

        )
        else
        (
            xdb:delete-user($name),

User {$name} removed.

        ),
        users:display()
};

declare function users:new-user() as element()*
{
    let $name := request:get-parameter("name", ()),
    $grp := request:get-parameter("groups", ()),
    $groups := if ($grp) then
        (
            if(contains($grp, ",")) then
            (
                tokenize($grp, "\s*,\s*")
            )
            else
            (
                $grp
            )
         ) else(),
    $pass1 := request:get-parameter("pass1", ""),
    $pass2 := request:get-parameter("pass2", ""),
    $home := request:get-parameter("home", ()) return

        if($pass1 != $pass2) then
        (

Passwords are not identical.
,
            users:correct-user(-1, $name, $grp, $home)
        )
        else if(empty($groups)) then
        (

Please specify one group at least.
,
            users:correct-user(-1, $name, $grp, $home)
        )
        else
        (
            xdb:create-user($name, $pass1, $groups, $home),
            users:display()
        )
};

declare function users:update-user() as element()*
{
    let $name := request:get-parameter("name", ()),
    $grp := request:get-parameter("groups", ()),
    $groups := if ($grp) then
        (
            if(contains($grp, ",")) then
            (
                tokenize($grp, "\s*,\s*")
            )
            else
            (
                $grp
            )
        )else(),
    $pass1 := request:get-parameter("pass1", ""),
    $pass2 := request:get-parameter("pass2", ""),
    $nopass := request:get-parameter("nopass", ()),
    $pass := if($nopass) then () else $pass1,
    $home := request:get-parameter("home", ()),
    $uid := request:get-parameter("uid", "") return

        if(not($nopass) and $pass1 ne $pass2)then
        (

Passwords are not identical.
,
            users:correct-user($uid, $name, $grp, $home)
        )
        else if(empty($groups)) then
        (

Please specify one group at least.
,
            users:correct-user($uid, $name, $grp, $home)
        )
        else
        (
            xdb:change-user($name, $pass, $groups, $home),

            if(xdb:get-current-user() eq $name) then
            (
                session:set-attribute("password", $pass)
            )else(),

            users:display()
        )
};

declare function users:display() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="GET">

                Name
                Groups
                Home

            {
                for $user in collection("/db/system/security/exist/accounts")/config:account[util:collection-name(.) ne '/db/system/security/exist/accounts/removed']
                    let $name := $user/config:name/text(),
                    $groups := string-join($user/config:group/@name, ", "),
                    $home := if ($user/config:home ne "") then $user/config:home/text() else "not set"
                order by $name return

                        <input type="radio" name="uid" value="{$user/@id}"/>
                        {$name}
                        {$groups}
                        {$home}

            }

                    <input type="submit" name="action" value="Edit"/>
                    <input type="submit" name="action" value="New User"/>
                    <input type="submit" name="action" value="Remove"/>

        {
                let $action := request:get-parameter("action", ""),
                $uid := request:get-parameter("uid", "") return
                    if ($action eq "Edit") then
                    (
                        let $user := collection("/db/system/security/exist/accounts")/config:account[util:collection-name(.) ne '/db/system/security/exist/accounts/removed'][@id = $uid] return
                            users:edit-user(xs:integer($uid), xs:string($user/config:name), string-join($user/config:group/@name, ", "), xs:string($user/config:home))
                    )
                    else if ($action eq "New User") then
                    (
                        users:edit-user(-1, "", "", ())
                    )else()
        }
        <input type="hidden" name="panel" value="users"/>
    </form>
};

declare function users:correct-user($uid as xs:integer, $name as xs:string, $groups as xs:string, $home as xs:string?) as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="GET">
        {users:edit-user($uid, $name, $groups, $home)}
        <input type="hidden" name="panel" value="users"/>
    </form>
};

declare function users:edit-user($uid as xs:integer, $name as xs:string, $groups as xs:string, $home as xs:string?) as element()
{

            User:
            {
                if($name eq "") then
                (
                    <input type="text" name="name"/>
                )
                else
                (
                    <input type="hidden" name="name" value="{$name}"/>{$name}
                )
            }

            Groups:
            <input type="text" name="groups" value="{$groups}"/>
            A comma-separated list of groups.
            Note: non-existing groups will be created automatically.

            Password:
            <input type="password" name="pass1"/>

            Repeat:
            <input type="password" name="pass2"/>

        {
            if($uid ge 0) then
            (
                <input type="checkbox" name="nopass"/> Leave password unchanged.
            )else()
        }

            Home Collection:
            <input type="text" name="home" value="{$home}"/>
            Optional: assign a home collection, e.g. /db/home/me.
            The user will be the owner of this collection.
            Note: non-existing collections will be created automatically.

        {
            if($uid lt 0) then
            (
                <input type="submit" name="action" value="Create"/>
            )
            else
            (
                <input type="submit" name="action" value="Change"/>
            )
        }

        <input type="hidden" name="uid" value="{$uid}"/>

};

xquery version "1.0";
(: $Id$ :)
(:
    Module: display and edit users.
:)

module namespace users="http://exist-db.org/xquery/admin-interface/users";

declare namespace util="http://exist-db.org/xquery/util";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

declare function users:main() as element()
{

User Management

Note: this is work in progress. Don't use this page to manage
        users in a production environment. Use the Java client instead.

        {
            if(not(xdb:is-admin-user(xdb:get-current-user()))) then
            (

                    Only users of the "dba" group can change user settings.

            )
            else
            (
                users:process()
            )
        }

};

declare function users:process() as element()*
{
    let $action := request:get-parameter("action", "") return
        if($action eq "Create") then
        (
            users:new-user()
        )
        else if($action eq "Change") then
        (
            users:update-user()
        )
        else if($action eq "Remove") then
        (
            users:remove-user()
        )
        else
        (
            users:display()
        )
};

declare function users:remove-user() as element()*
{
    let $uid := request:get-parameter("uid", ()),
    $name := doc("/db/system/users.xml")//user[@uid = $uid]/@name cast as xs:string return

        if($name eq xdb:get-current-user()) then
        (

You cannot remove the current user!

        )
        else
        (
            xdb:delete-user($name),

User {$name} removed.

        ),
        users:display()
};

declare function users:new-user() as element()*
{
    let $name := request:get-parameter("name", ()),
    $grp := request:get-parameter("groups", ()),
    $groups := if ($grp) then
        (
            if(contains($grp, ",")) then
            (
                tokenize($grp, "\s*,\s*")
            )
            else
            (
                $grp
            )
         ) else(),
    $pass1 := request:get-parameter("pass1", ""),
    $pass2 := request:get-parameter("pass2", ""),
    $home := request:get-parameter("home", ()) return

        if($pass1 != $pass2) then
        (

Passwords are not identical.
,
            users:correct-user(-1, $name, $grp, $home)
        )
        else if(empty($groups)) then
        (

Please specify one group at least.
,
            users:correct-user(-1, $name, $grp, $home)
        )
        else
        (
            xdb:create-user($name, $pass1, $groups, $home),
            users:display()
        )
};

declare function users:update-user() as element()*
{
    let $name := request:get-parameter("name", ()),
    $grp := request:get-parameter("groups", ()),
    $groups := if ($grp) then
        (
            if(contains($grp, ",")) then
            (
                tokenize($grp, "\s*,\s*")
            )
            else
            (
                $grp
            )
        )else(),
    $pass1 := request:get-parameter("pass1", ""),
    $pass2 := request:get-parameter("pass2", ""),
    $nopass := request:get-parameter("nopass", ()),
    $pass := if($nopass) then () else $pass1,
    $home := request:get-parameter("home", ()),
    $uid := request:get-parameter("uid", "") return

        if(not($nopass) and $pass1 ne $pass2)then
        (

Passwords are not identical.
,
            users:correct-user($uid, $name, $grp, $home)
        )
        else if(empty($groups)) then
        (

Please specify one group at least.
,
            users:correct-user($uid, $name, $grp, $home)
        )
        else
        (
            xdb:change-user($name, $pass, $groups, $home),

            if(xdb:get-current-user() eq $name) then
            (
                session:set-attribute("password", $pass)
            )else(),

            users:display()
        )
};

declare function users:display() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="GET">

                Name
                Groups
                Home

            {
                for $user in doc("/db/system/users.xml")//users/user
                    let $name := xs:string($user/@name),
                    $groups := string-join($user/group, ", "),
                    $home := if($user/@home) then xs:string($user/@home) else "not set"
                order by $name return

                        <input type="radio" name="uid" value="{$user/@uid}"/>
                        {$name}
                        {$groups}
                        {$home}

            }

                    <input type="submit" name="action" value="Edit"/>
                    <input type="submit" name="action" value="New User"/>
                    <input type="submit" name="action" value="Remove"/>

        {
                let $action := request:get-parameter("action", ""),
                $uid := request:get-parameter("uid", "") return
                    if($action eq "Edit") then
                    (
                        let $user := doc("/db/system/users.xml")//users/user[@uid = $uid] return
                            users:edit-user(xs:integer($uid), $user/@name, string-join($user/group, ", "), $user/@home)
                    )
                    else if($action eq "New User") then
                    (
                        users:edit-user(-1, "", "", ())
                    )else()
        }
        <input type="hidden" name="panel" value="users"/>
    </form>
};

declare function users:correct-user($uid as xs:integer, $name as xs:string, $groups as xs:string, $home as xs:string?) as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="GET">
        {users:edit-user($uid, $name, $groups, $home)}
        <input type="hidden" name="panel" value="users"/>
    </form>
};

declare function users:edit-user($uid as xs:integer, $name as xs:string, $groups as xs:string, $home as xs:string?) as element()
{

            User:
            {
                if($name eq "") then
                (
                    <input type="text" name="name"/>
                )
                else
                (
                    <input type="hidden" name="name" value="{$name}"/>{$name}
                )
            }

            Groups:
            <input type="text" name="groups" value="{$groups}"/>
            A comma-separated list of groups.
            Note: non-existing groups will be created automatically.

            Password:
            <input type="password" name="pass1"/>

            Repeat:
            <input type="password" name="pass2"/>

        {
            if($uid ge 0) then
            (
                <input type="checkbox" name="nopass"/> Leave password unchanged.
            )else()
        }

            Home Collection:
            <input type="text" name="home" value="{$home}"/>
            Optional: assign a home collection, e.g. /db/home/me.
            The user will be the owner of this collection.

        {
            if($uid lt 0) then
            (
                <input type="submit" name="action" value="Create"/>
            )
            else
            (
                <input type="submit" name="action" value="Change"/>
            )
        }

        <input type="hidden" name="uid" value="{$uid}"/>

};