#!/usr/bin/perl -T
# $Id: task.cgi,v 1.7 2023/02/23 11:19:27 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2015              ###
###                                                      ###
############################################################
############################################################
#$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;

my $acmode = get_cv('global','access_mode');
$acmode = 'celltest' unless ($acmode);
my @list = ($acmode);
my $id = get_CGI_value_clean('task');


my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};
my $admin = $$ref{'admin'};
my $cert = 0;
my $user = $$ref{'name'};
my $pass = $$ref{'pass'};


if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {

	my $authorisation = get_CGI_value_clean('task');
	my $avail = param('avail');
        print process('main_nav.tt');
	print p();
	print h1('Authorisation setup');
	print p();
        $_ = param('action');
      CASE:
        {
            ### Insert case statements ###
	    /Change name/ and do {&RFC::RFCCGI::passwd_client('task_name',$user,$pass,$authorisation,param('task_name')); &main_page(); last CASE;};
	    /Set status/ and do {&set_valid(); &main_page(); last CASE;};
	    /Set certification/ and do {&RFC::RFCCGI::passwd_client('set_task_cert',$user,$pass,$avail,$authorisation); &main_page(); last CASE;};
	    /Set authorisation/ and do {&RFC::RFCCGI::passwd_client('set_task_auth',$user,$pass,$avail,$authorisation); &main_page(); last CASE;};
	    /Revoke certification/ and do {&RFC::RFCCGI::passwd_client('rev_task_cert',$user,$pass,param('users'),$authorisation); &main_page(); last CASE;};
	    /Revoke authorisation/ and do {&RFC::RFCCGI::passwd_client('rev_task_auth',$user,$pass,param('users'),$authorisation); &main_page(); last CASE;};
	    /Create new task/ and do {&new_task(); &main_page(); last CASE;};
	    ### Default response ###
	    &main_page();
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;

sub new_task()
{
    my $taskname = param('task_name');
    return unless ($admin);
    $id = &RFC::RFCCGI::passwd_client('new_task',$user,$pass,$taskname);
}

sub set_valid()
{
    my $status = get_CGI_value_clean('valid');
    $status = 0 unless ($status);
    &RFC::RFCCGI::passwd_client('task_valid',get_id(),$id,$status);
}

sub main_page()
{
    my $str = &RFC::RFCCGI::passwd_client('get_task_names',get_id());
    my @list = ('');
    my %keys = (''=>'');
    if ($str && $str =~ /\w/)
    {
	foreach my $item (split(/\n/,$str))
	{
	    my ($id,$name) = split(/\t/,$item);
	    push @list,$id;
	    $keys{$id} = $name;
	}
    }
    print '<center>';
    print '<h3>Curent authorisations: ';
    print start_form(-name=>'task_select');
    &print_hidden();
    print popup_menu(-name=>'task',-values=>[@list],-default=>$id,-labels=>\%keys,-onchange=>'document.task_select.submit()');
    print end_form();
    print '</h3>',"\n";
    print p('&nbsp;');
    if ($id)
    {
	my @users = split(/\n/,&RFC::RFCCGI::passwd_client('get_users',get_id()));
	$cert = &RFC::RFCCGI::passwd_client('check_task_cert',get_user(),$id);
	print '<table class="fit-content" style="text-align:left;"><tr>';
	print '<td width=20%></td>';
	print '<td width=15%></td>';
	print '<td width=15%></td>';
	print '<td width=15%></td>';
	print '<td width=15%></td>';
	print '<td width=20%></td></tr>';
	print '<tr><th> ID </th><th colspan=4>';
	print $id;
	print '</th></tr>';
	my $name = &RFC::RFCCGI::passwd_client('task_name',get_id(),$id);
	my $valid = &RFC::RFCCGI::passwd_client('task_valid',get_id(),$id);
	if ($admin)
	{
	    print start_form();
	    &print_hidden();
	    print hidden(-name=>'task',-value=>$id,-force=>1);
	    print '<tr><th> Name </th><td colspan=2>';
	    print textfield(-name=>'task_name',-value=>$name,-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Change name',-class=>'rfcbutton');
	    print '</td></tr>';
	    print end_form();
	    print start_form();
	    &print_hidden();
	    print hidden(-name=>'task',-value=>$id,-force=>1);
	    print '<tr><th> Valid </th><td>';
	    print checkbox(-name=>'valid',-value=>'1',-label=>'',-checked=>$valid,-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Set status',-class=>'rfcbutton');
	    print '</td></tr>';
	    print end_form();
	    my $str = &RFC::RFCCGI::passwd_client('list_cert',get_id(),$id);
	    my @cert = ();
	    @cert = split(/\n/,$str) if ($str && $str =~ /\w/);
	    print start_form();
	    &print_hidden();
	    print hidden(-name=>'task',-value=>$id,-force=>1);
	    print '<tr><th colspan=4> &nbsp;</th></tr>';
	    print '<tr><th colspan=4> Certified users: </th></tr>';
	    print '<tr><th> Current users </th><td>';
	    print popup_menu(-name=>'users',-values=>['',@cert],-default=>'',force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Revoke certification',-class=>'rfcbutton');
	    print '</td><th> Users avaliable </th><td>';
	    my @list = ();
	  USER:
	    foreach my $u (@users)
	    {
		foreach my $c (@cert)
		{
		    next USER if ($c eq $u);
		}
		push @list,$u;
	    }
	    print popup_menu(-name=>'avail',-values=>['',@list],-default=>'',-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Set certification',-class=>'rfcbutton');
	    print '</td></tr>';
	    print end_form();
	}
	else
	{
	    print '<tr><th> Name </th><th colspan=4>',$name,'</th></tr>';
	    print '<tr><th> Valid </th><th>';
	    if ($valid)
	    {
		print 'Yes';
	    }
	    else
	    {
		print 'No';
	    }
	    print '</th></tr>';
	}
	if ($valid && ($cert || $admin))
	{
	    my @auth = ();
	    my $str = &RFC::RFCCGI::passwd_client('list_auth',get_id(),$id);
	    @auth = split(/\n/,$str) if ($str && $str =~ /\w/);
	    print start_form();
	    &print_hidden();
	    print hidden(-name=>'task',-value=>$id,-force=>1);
	    print '<tr><th colspan=4> &nbsp;</th></tr>';
	    print '<tr><th colspan=4> Authorised users: </th></tr>';
	    print '<tr><th> Current users </th><td>';
	    print popup_menu(-name=>'users',-values=>['',@auth],-default=>'',-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Revoke authorisation',-class=>'rfcbutton');
	    print '</td><th> Users avaliable </th><td>';
	    @list = ();
	  USER:
	    foreach my $u (@users)
	    {
		foreach my $c (@auth)
		{
		    next USER if ($c eq $u);
		}
		push @list,$u;
	    }
	    print popup_menu(-name=>'avail',-values=>['',@list],-default=>'',-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print submit(-name=>'action',-value=>'Set authorisation',-class=>'rfcbutton');
	    print '</td></tr>';
	    print end_form();
	}
	if ($admin)
	{
	    print '<tr><td height=100></td></tr>';
	    print start_form(-name=>'new_task',-onsubmit=>'return checknew()');
	    print_hidden();
	    print '<tr><th colspan=2>Create new authorisation </th><th>Name:</th><td colspan=2>';
	    print textfield(-name=>'task_name',-value=>'',-force=>1,-class=>'textfield');
	    print '</td><td>';
	    print hidden(-name=>'action',-value=>'Create new task');
	    print submit(-value=>'Create',-class=>'rfcbutton');
	    print '</td></tr>';
	    print end_form();
	}
	print '</table>';
    }
    elsif ($admin)
    {
	print '<table width=60%><tr>';
	print '<tr><td height=50></td></tr>';
	print start_form(-name=>'new_task',-onsubmit=>'return checknew()');
	print_hidden();
	print '<tr><th colspan=2>Create new authorisation </th><th>Name:</th><td colspan=2>';
	print textfield(-name=>'task_name',-value=>'',-force=>1,-class=>'textfield');
	print '</td><td>';
	print hidden(-name=>'action',-value=>'Create new task');
	print submit(-value=>'Create',-class=>'rfcbutton');
	print '</td></tr>';
	print end_form();
	print '</table>';
    }
    print '</center>';
    print <<END

<style>

.textfield {
  width:100%;
}

</style>


<script>

function checknew()
{
    if (confirm("Create new task"))
    {
	var n = document.new_task.task_name.value;
	if (n != null && n != '')
	{
	    return true;
	}
	else
	{
	    alert('No name for the new task was specified!');
	}
    }
    return false;
}

</script>

END

}
