<h3>Admin Panel - User Control</h3>
<& SELF:error &>
% if ($cmd eq 'Delete' and !$confirm) {
<& SELF:confirm_del &>
% } else {
<& SELF:list_users &>
<& SELF:add_user &>
%}
<%args>
$cmd   => undef
$user  => undef
$port  => 0
$admin => 0
$confirm => 0
$pw    => undef
$fullname => undef
$note  => undef
</%args>
<%shared>
my ($this_user,$users,$is_admin,%user_hash,$arg_user);
my $odd    = 0;
my $err    = '';
my $secure = &is_secure;
my $userip = $r->connection->remote_ip; 
</%shared>
<%init>
$this_user = $m->session->{user};
$is_admin  = $m->session->{user_admin};
$arg_user = $user;

# Send to https if not secure.
unless ($secure){
    my $url = url_secure($r->uri);
    $m->redirect($url);
}

# Send to login if not admin.
unless ($is_admin) {
    my $url = $m->interp->apply_escapes( $r->uri, 'u' );
    $m->redirect("login.html?done=$url");
}

# Fetch users from db.
$m->comp('SELF:load_users');

# Add 
if ($cmd eq 'add'){
    $user = lc($user);
    $user =~  s/^\s*//;
    $user =~  s/\s+.*//;
    unless ($user){
        $err .= "Select a user name.\n";
    }
    unless ($pw){
        $err .= "Enter a password for this user.\n";
    }
    if (defined $user_hash{$user}) {
        $err .= "There is already a user by that name.\n";
    }
    unless ($err){
        # Add
        my $rv = netdisco::user_add($user,(
                                    'pw' => $pw,
                                    'admin' => $admin eq '1' ? 1 : 0,
                                    'port'  => $port eq  '1' ? 1 : 0,
                                    'fullname' => $fullname,
                                    'note' => $note,
                                ));
        $err = $rv ? "User $user added.\n" : "Could not Add user!\n";

        # Log addition
        insert_or_update('user_log',{},
                {'username'=>$m->session->{user},'event'=>'user_add',
                 'userip'=>$userip,'details'=>"User: $user - $err"});
        
        # Reload hash
        $m->comp('SELF:load_users');
    }
}

if ($cmd eq 'Change'){
    unless (defined $user_hash{$user}){
        $err .= "User $user not found.\n";
    } 
    my %user_change;
    unless ($err){
        # Check for PW
        if (defined $pw and length($pw)){
            $user_change{pw}=$pw;
        }

        # Check for Admin Change
        if ($admin != $user_hash{$user}->{admin}) { 
            $user_change{admin} = $admin;
        }

        # Check for Port Change
        if ($port != $user_hash{$user}->{port_control}) {
            $user_change{port} = $port;
        }

        # Check for Note Change
        if ($note ne $user_hash{$user}->{note}) {
            $user_change{note} = $note;
        }

        # Check for Full Name Change
        if ($fullname ne $user_hash{$user}->{fullname}) {
            $user_change{fullname} = $fullname;
        }

        if (scalar keys %user_change){
            my $rv = netdisco::user_add($user,%user_change);
            $err .= $rv ?
                    "Could not modify user! $rv\n"
                        :
                    "Changed ".join(',',keys %user_change)." for user $user.\n";

            # Log Change
            insert_or_update('user_log',{},
                    {'username'=>$m->session->{user},'event'=>'user_change',
                     'userip'=>$userip,'details'=>"User: $user - $err"});

            $m->comp('SELF:load_users');
        } 

    }

}

if ($cmd eq 'Delete'){
    unless (defined $user_hash{$user}){
        $err .= "User $user not found.\n";
    } 

    if ($user eq $this_user) {
        $err .= "You cannot delete yourself.\n";
    }

    if (!$err and $confirm){
        my $rv = netdisco::user_del($user);

        if (defined $rv){
            $err .= "Deleted user $user.\n";
        } else {
            $err .= "Could not delete user $user.\n";
        }

        # Log Change
        insert_or_update('user_log',{},
                {'username'=>$m->session->{user},'event'=>'user_del',
                 'userip'=>$userip,'details'=> "User: $user - $err"});

        # Reload hash
        $m->comp('SELF:load_users');
    } 
}
 
</%init>
<%method list_users>
<h4>Existing Users</h4>
<TABLE WIDTH=100% CELLSPACING=0 BORDER=0>
<TR>
    <TH>User</TH>
    <TH>Port<BR>Control</TH>
    <TH>Admin<BR>Control</TH>
    <TH>Full Name</TH>
    <TH>Note</TH>
    <TH>Created /<BR>Last Login</TH>
    <TH>New Password</TH>
    <TH>&nbsp;</TH>
</TR>
% foreach my $user_entry (sort { $a->{username} cmp $b->{username} } @$users){
%   my $last_on =  $user_entry->{last_on} ?
%                   scalar(localtime($user_entry->{last_on})) 
%                   : 'Never logged in';
<TR CLASS="match-<% ++$odd % 2 %>">
    <TD>
        <FORM ACTION="<%$r->uri%>" METHOD=POST> 
        <INPUT TYPE="HIDDEN" NAME="user" VALUE="<%$user_entry->{username}|h%>">
        <B><%$user_entry->{username}%></B>
    </TD>
    <TD>
        <INPUT TYPE="checkbox" NAME="port" VALUE="1" <%$user_entry->{port_control} ? 'CHECKED' : ''%>>
    </TD>
    <TD><INPUT TYPE="checkbox" NAME="admin" VALUE="1" <%$user_entry->{admin} ? 'CHECKED' : ''%>></TD>
    <TD>
        <INPUT TYPE="text" NAME="fullname" SIZE=20 MAXLENGTH=200 VALUE="<%$user_entry->{fullname} |h%>">
    </TD>
    <TD>
        <INPUT TYPE="text" NAME="note" SIZE=40 MAXLENGTH=255 VALUE="<%$user_entry->{note} |h%>">
    </TD>
    <TD><NOBR><%scalar localtime($user_entry->{creation})%></NOBR><BR>
        <NOBR><%$last_on%></NOBR></TD>
    <TD><INPUT TYPE="password" NAME="pw" SIZE=20 MAXLENGTH=50></TD>
    <TD><INPUT TYPE="submit" NAME="cmd" VALUE="Delete">
        <INPUT TYPE="submit" NAME="cmd" VALUE="Change">
        </FORM>
    </TD>
</TR>
% }
</TABLE>
<%scalar @$users%> Users.
</%method>
<%method add_user>
<h4>Add User</h4>
<FORM ACTION="<%$r->uri%>" METHOD=POST>
<INPUT TYPE="HIDDEN" NAME="cmd" VALUE="add">
<TABLE CLASS="box" CELLSPACING=0 CELLPADDING=3 BORDER=0>
<TR>
    <TD>Username:</TD>
    <TD><INPUT TYPE="text" NAME="user" SIZE="20" MAXLENGTH="50"></TD>
    <TD>Lower case, alphanumeric only.</TD>
</TR>
<TR>
    <TD>Password:</TD>
    <TD><INPUT TYPE="password" NAME="pw" SIZE="20" MAXLENGTH="50"></TD>
    <TD>Case Sensitive.</TD>
</TR>
<TR>
    <TD>Full Name:</TD>
    <TD><INPUT TYPE="text" NAME="fullname" SIZE=35 MAXLENGTH=255></TD>
    <TD>(optional)</TD>
</TR>
<TR>
    <TD>Note:</TD>
    <TD><INPUT TYPE="text" NAME="note" SIZE=35 MAXLENGTH=255></TD>
    <TD>(optional)</TD>
</TR>
<TR>
    <TD>Port Control:</TD>
    <TD><INPUT NAME="port" TYPE="checkbox" VALUE="1"></TD>
    <TD>Gives user ability to turn non-uplink ports on and off.</TD>
<TR>
    <TD>Admin : </TD>
    <TD><INPUT NAME="admin" TYPE="checkbox" VALUE="1"></TD>
    <TD>Gives user ability to control device and users.</TD>
</TR>
<TR>
    <TD COLSPAN=3>
        <P> 
        <P> 
        <INPUT TYPE="submit" VALUE="Add User">
    </TD>
</TR>
</TABLE>
</FORM>
</%method>
<%method confirm_del>
<FORM ACTION="<%$r->uri%>" METHOD=POST>
<INPUT TYPE="hidden" NAME="cmd" VALUE="Delete">
<INPUT TYPE="hidden" NAME="user" VALUE="<%$arg_user|h%>"> 
Confirm Deletion of <%$arg_user |h%> : 
<INPUT TYPE="checkbox" NAME="confirm" VALUE="1">
<BR>
<INPUT TYPE="submit" VALUE="Delete <%$arg_user|h%>">
[<A HREF="<%$r->uri%>">Cancel</A>]
</FORM> 
</%method>
<%method load_users>
<%perl>
$users = sql_rows('users',['username','password','extract(epoch from creation) as creation',
                           'extract(epoch from last_on) as last_on','port_control','admin',
                           'note','fullname']
                 );

%user_hash = map {$_->{username} => $_} @$users;
</%perl>
</%method>
<%method error>
% return unless $err;
<h3><%$err%></h3>
</%method>
<%method title>
- Admin Panel - Users \
</%method>
%# $Id: admin_user.html,v 1.5 2004/09/25 03:56:13 maxbaker Exp $
%# vim:syntax=mason
