#!/usr/bin/perl

do "ldap_users.pl";
&connect ();
&ReadParse();

$sort_on = ($in{sort_on}) ? $in{sort_on} : "uid";

&header ("LDAP Users", "", "intro", 1, 1);
print "<HR noshade size=2>\n";
print "<P><B>LDAP Users (All OUs)</B> -- Sorted by ";

@all_users = &list_users ("");
if ($sort_on eq "uidnumber") {
    @users = sort {$a->{$sort_on} <=> $b->{$sort_on}} @all_users;
    print $text{uidnumber} . "\n";
}
elsif ($sort_on eq "uid") {
    @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
    print $text{uid} . "\n";
}
elsif ($sort_on eq "cn") {
    @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
    print $text{cn} . "\n";
}
else {
    @users = sort {($a->{department} . $a->{cn}) cmp
       ($b->{department} . $b->{cn})} @all_users;
    print $text{department} . "\n";
}

print "<P><A href=\"edit_user.cgi\">" . $text{header_4} . "</A><P>";

print "<TABLE border width=100% $cb>\n";
print "<TR $tb>\n";
print "<TD><B><A href=\"index.cgi?sort_on=uid\">" .
    $text{uid} . "</A></B>\n";
print "<TD><B><A href=\"index.cgi?sort_on=uidnumber\">" .
    $text{uidnumber} . "</A></B>\n";
print "<TD><B><A href=\"index.cgi?sort_on=cn\">" .
    $text{cn} . "</A></B>\n";
print "<TD><B><A href=\"index.cgi?sort_on=department\">" .
    $text{department} . "</A></B>\n";
# do not show DN until we can select and edit OUs
#print "<TD><B><A href=\"index.cgi?sort_on=dn\">DN</A></B>\n";

if ($#users < 0) {
    print "<TR><TD colspan=4>" . $text{msg_1} . "\n";
}
else {
    $i = 0;
    foreach $user (@users) {
        $dn = $user->{dn};
        print "<TR>";
        print "<TD><A href=\"edit_user.cgi?sort_on=$sort_on&dn=$dn\">" .
            $user->{uid} . "</A>";
        print "<TD>" . $user->{uidnumber};
        print "<TD>" . $user->{cn};
        print "<TD>&nbsp;" . $user->{department};
        #print "<TD>" . $user->{dn};
        print "\n";
    }
}
print "</TABLE>\n";

print "<BR>\n";
&footer ("/", "index");
do "footer.pl";

