<h1 class="pagehead">Admin Panel - User Control</h1>
<& SELF:error &>
% if ($cmd eq 'Delete' and !$confirm) {
<& SELF:confirm_del &>
% } elsif ($cmd eq 'Edit' and !$err) {
<& SELF:add_user, action => 'Change' &>
% } else {
<& SELF:list_users &>
<& SELF:add_user &>
%}
<%args>
$cmd   => undef
$user  => undef
$port  => 0
$admin => 0
$ldap  => 0
$confirm => 0
$pw    => undef
$fullname => undef
$note  => undef
</%args>
<%shared>
my ($this_user,$users,%user_hash,$arg_user);
my $odd    = 0;
my $err    = '';
my $userip = $r->connection->client_ip; 
</%shared>
<%init>
# Enforce secure + admin
$m->comp('admin.html:force_secure_admin');

$this_user = $m->session->{user};
$arg_user = $user;

# 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 || $ldap){
        $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,
                                    'ldap'  => $ldap 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 Ldap Change
        if ($ldap != $user_hash{$user}->{ldap}) {
            $user_change{ldap} = $ldap;
        }

        # 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 'Edit'){
    unless (defined $user_hash{$user}){
        $err .= "User $user not found.\n";
    } 
}

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>
<h2 class="subheader">Existing Users</h2>
<TABLE CLASS="adm-q">
<TR>
    <TH>User</TH>
    <TH>Port<BR>Control</TH>
    <TH>Admin<BR>Control</TH>
    <TH>LDAP</TH>
    <TH>Full Name</TH>
    <TH>Note</TH>
    <TH>Created /<BR>Last Login</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';
%   $odd++;
<TR>
    <TD CLASS="match-<%$odd%2%>"><B><%$user_entry->{username}%></B></TD>
    <TD CLASS="match-<%$odd%2%>"><%$user_entry->{port_control} ? 'Yes' : 'No'%></TD>
    <TD CLASS="match-<%$odd%2%>"><%$user_entry->{admin} ? 'Yes' : 'No'%></TD>
    <TD CLASS="match-<%$odd%2%>"><%$user_entry->{ldap} ? 'Yes' : 'No'%></TD>
    <TD CLASS="match-<%$odd%2%>"><%$user_entry->{fullname} |h%></TD>
    <TD CLASS="match-<%$odd%2%>"><%$user_entry->{note} |h%></TD>
    <TD CLASS="match-<%$odd%2%>"><span class="nobr"><%scalar localtime($user_entry->{creation})%></span><BR>
        <span class="nobr"><%$last_on%></span></TD>
    <TD CLASS="match-<%$odd%2%>">
        <FORM ACTION="<%$r->uri%>" METHOD=POST>
        <DIV>
        <INPUT TYPE="hidden" NAME="user" VALUE="<%$user_entry->{username}|h%>">
        <INPUT TYPE="submit" CLASS="navbutton" NAME="cmd" VALUE="Delete">
        <INPUT TYPE="submit" CLASS="navbutton" NAME="cmd" VALUE="Edit">
        </DIV>
        </FORM>
    </TD>
</TR>
% }
</TABLE>
<%scalar @$users%> Users.
</%method>
<%method add_user>
% my $user = ($action eq 'Change') ? $user_hash{$arg_user} : undef;
<h2 class="subheader"><%$action%> User</h2>
<FORM ACTION="<%$r->uri%>" METHOD=POST>
<DIV>
<INPUT TYPE="HIDDEN" NAME="cmd" VALUE="<%$action%>">
<TABLE CLASS="adm-search">
<TR>
    <TH>Username:</TH>
% if ($action eq 'Add') {
    <TD><INPUT TYPE="text" NAME="user" SIZE="20" MAXLENGTH="50"></TD>
    <TD CLASS="note">Lower case, alphanumeric only.</TD>
% } else {
    <TD><INPUT TYPE="HIDDEN" NAME="user" VALUE="<%$arg_user|h%>"><%$arg_user%></TD>
    <TD CLASS="note">&nbsp;</TD>
% }
</TR>
<TR>
    <TH>Password:</TH>
    <TD><INPUT TYPE="password" NAME="pw" SIZE="20" MAXLENGTH="50"></TD>
    <TD CLASS="note">Case Sensitive.</TD>
</TR>
<TR>
    <TH>LDAP:</TH>
    <TD><INPUT NAME="ldap" TYPE="checkbox" VALUE="1" <%$user->{ldap} ? 'CHECKED' : ''%>></TD>
    <TD CLASS="note">Use LDAP for Login</TD>
</TR>
<TR>
    <TH>Full Name:</TH>
    <TD><INPUT TYPE="text" NAME="fullname" SIZE=35 MAXLENGTH=255 VALUE="<%$user->{fullname} |h%>"></TD>
    <TD CLASS="note">(optional)</TD>
</TR>
<TR>
    <TH>Note:</TH>
    <TD><INPUT TYPE="text" NAME="note" SIZE=35 MAXLENGTH=255 VALUE="<%$user->{note} |h%>"></TD>
    <TD CLASS="note">(optional)</TD>
</TR>
<TR>
    <TH>Port Control:</TH>
    <TD><INPUT NAME="port" TYPE="checkbox" VALUE="1" <%$user->{port_control} ? 'CHECKED' : ''%>></TD>
    <TD CLASS="note">Gives user ability to turn non-uplink ports on and off.</TD>
<TR>
    <TH>Admin:</TH>
    <TD><INPUT NAME="admin" TYPE="checkbox" VALUE="1" <%$user->{admin} ? 'CHECKED' : ''%>></TD>
    <TD CLASS="note">Gives user ability to control device and users.</TD>
</TR>
<TR>
    <TH>&nbsp;</TH>
    <TD COLSPAN=2>
        <INPUT TYPE="submit" CLASS="navbutton" VALUE="<%$action%> User">
% if ($action eq 'Change') {
        <A CLASS="navbutton" HREF="<%$r->uri%>">Don't Change</A>
% }
    </TD>
</TR>
</TABLE>
</DIV>
</FORM>
<%args>
$action => 'Add'
</%args>
</%method>
<%method confirm_del>
<FORM ACTION="<%$r->uri%>" METHOD=POST>
<DIV>
<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" CLASS="navbutton" VALUE="Delete <%$arg_user|h%>">
<A CLASS="navbutton" HREF="<%$r->uri%>">Cancel</A>
</DIV>
</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','ldap']
                 );

%user_hash = map {$_->{username} => $_} @$users;
</%perl>
</%method>
<%method error>
% return unless $err;
<h1 class="pagehead"><%$err%></h1>
</%method>
<%method title>
- Admin Panel - Users \
</%method>
%# $Id$
%# vim:syntax=mason
