#!/usr/bin/perl -T
# $Id: usermanage.cgi,v 1.8 2023/02/23 11:19:51 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2005              ###
###                                                      ###
############################################################
############################################################
#$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;
use Sys::Hostname;

my $hostname = hostname;
my $pwdhost = get_cv('passwds','passwd_server');
my $acmode = get_cv('global','access_mode');
$acmode = 'celltest' unless ($acmode);
my @list = ($acmode);

my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};

if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid && $$ref{'admin'})
    {
        print process('main_nav.tt');
	print "<div style='margin:auto; padding:50px;'>\n";
        $_ = param('action');
      CASE:
        {
	    /^new_user$/ and do{&new_user_form; last CASE;};
	    /^Create user$/ and do {&cusr;};
	    /^select_user$/ and do {&manage_user; last CASE;};
	    /^set_su$/ and do {&set_su; &manage_user; last CASE;};
	    /^del_user$/ and do {&del_user;};
	    /^set_per$/ and do {&set_per; &manage_user; last CASE;};
	    /^reset_user$/ and do {&reset_pwd; &manage_user; last CASE;};
	    ### Default response ###
	    print p();
	    print h1("User administration center");
	    print "<div class='content-container fit-content' style='padding:50px;text-align:center;'>\n";
	    print start_form;
	    print h3({-class=>'Bl'},"Choose which user you want to change permissions for");
	    my @users = sort(&RFC::RFCCGI::get_users);
	    print '<table style="margin:auto;">';
	    print '<tr><th>';
	    print popup_menu(-name=>'m_user',-value=>[@users]);
	    print '</th><th width=200>'; 
	    &action('select_user');
	    &print_hidden;
	    print submit(-class=>'s0',-name=>'test',-value=>'Select user');
	    print '</th></tr></table>';
	    print end_form;
	    print p('');
	    print start_form;
	    &print_hidden;
	    &action('new_user');
	    print submit(-class=>'s3',-name=>'test',-value=>'Create new user',-style=>'width:200px;');
	    print end_form;
	    print '</div>';
        }
	print '</div>';
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;

sub del_user
{
    my $muser = '';
    $muser = param('m_user') if defined (param('m_user'));
    $muser =~ /(\w*)/;
    $muser = $1;
    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my $res = &RFC::Main::passwd_client('deleteuser',$user,$pass,$muser);
    print h3({-class=>'Alert'},"User could not be deleted!") if (!$res);
}

sub set_su
{ 
    my $muser = '';
    $muser = param('m_user') if defined (param('m_user'));
    $muser =~ /(\w*)/;
    $muser = $1;
    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my $rb = 0;
    $rb = 1 if (defined(param(' ')));
    my $res = &RFC::Main::passwd_client('root_access',$user,$pass,$muser,$rb);
    print h3({-class=>'Alert'},"Superuser bit could not be set!") if (!$res);
}

sub reset_pwd
{ 
    my $muser = '';
    $muser = param('m_user') if defined (param('m_user'));
    $muser =~ /(\w*)/;
    $muser = $1;
    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my $rb = 0;
    $rb = 1 if (defined(param(' ')));
    my $res = &RFC::Main::passwd_client('resetpwd',$user,$pass,$muser,$rb);
    print h3({-class=>'Alert'},"Passwd could not be reset!") if (!$res);
    print center(h3("Password has been reset!")) if ($res);
}

sub manage_user
{
    my $mode = 1;
    $mode = 0 if (&get_CGI_value('manage_mode') =~ /table/i);
    my $muser = '';
    $muser = param('m_user') if defined (param('m_user'));
    $muser =~ /(\w*)/;
    $muser = $1;
    if (&RFC::RFCCGI::is_user($muser))
    {
	print '<center><table border=0 cellspacing=0 cellpadding=0>';
	print '<tr><th class="heading" style="font-size:14;color:red;">';
	print "<h2>Managing user $muser</h2>";
	print '</th><th width=30>';
	print '</th><th>';
	print '</th></tr></table>';
	print '<table border=0 cellspacing=5 cellpadding=0>';
	print '<tr>';
	print start_form(-class=>'form');
	print '<th align=left>';
	&print_hidden;
	print hidden(-name=>'m_user',-value=>$muser,-force=>'1');
	&action('set_su');
	print 'Superuser: ';
	print '</th><th>';
	my ($u,$p) = &get_id();
	print checkbox(-name=>' ',-checked=>&RFC::Main::passwd_client('get_admin',$u,$p,$muser),value=>'is_su');
	print '</th><th>';
	print submit(-class=>'s4',-name=>'test',-value=>'Set superuser status',-style=>'width:100%;');
	print '</th>';
	print end_form;

	print '<th>';
	print start_form(-class=>'form');
	&print_hidden;
	print hidden(-name=>'m_user',-value=>$muser,-force=>'1');
	&action('del_user');
	print submit(-class=>'s2',-name=>'test',-value=>'Delete user',-style=>'width:100%;');
	print end_form;
	print '</th>';

	print '<th>';
	print start_form(-class=>'form');
	&print_hidden;
	print hidden(-name=>'m_user',-value=>$muser,-force=>'1');
	&action('reset_user');
	print submit(-class=>'s3',-name=>'test',-value=>'Reset password',-style=>'width:100%;');
	print end_form;
	print '</th>';

	print '</tr>';
	print '</table>';
	print center(h2({-class=>'C'},"Permisions for systems"));
	print start_form;
	print '<table border=0 cellspacing=0 cellpadding=0>';
	print '</tr><th>';

	print '<table border=0 cellspacing=5 cellpadding=0>';
	for my $table (@list)
	{
	    if ($table =~ /\w/)
	    {
		print '<tr><th align=left>';
		print ucfirst($table);
		print '</th><th>';
		my $p = &get_per($muser,$table);
		print radio_group(-name=>'table_'.$table,-values=>[('none','read','create','modify')],-default=>$p,-force=>'1');
		print '</th></tr>';
	    }
	}
	print '</table>';
	print '</th><th valigh=center>';
	&print_hidden;
	print hidden(-name=>'m_user',-value=>$muser,-force=>'1');
	print hidden(-name=>'manage_mode');
	&action('set_per');
	print center(submit(-name=>'test',-value=>'Set permissions',-class=>'s9'));
	print '</table>';
	print end_form;
	print '</center>';
    }
    else
    {
	print center(h3({-class=>'Alert'},"Error: no user selected or user not exsistent!"));
    }
}


sub set_per
{
    print '<center><h3>Setting permissions . . .';
    my $user = RFC::RFCCGI::get_user;
    my $pass = RFC::RFCCGI::get_pass;
    my $muser = '';
    $muser = param('m_user') if defined(param('m_user'));
    $muser =~ /(\w+)/;
    $muser = $1;
    my $mode = 1;
    for my $s (@list)
    {
	my $per = '';
	$per = param('table_'.$s) if defined(param('table_'.$s));
	my $m = 0;
	$m = 1 if ($per =~ /^r/i);
	$m = 2 if ($per =~ /^c/i);
	$m = 3 if ($per =~ /^m/i);
	my $res = &RFC::Main::passwd_client('user_auth',$user,$pass,$muser,$s,$m);
	print h3({-class=>'Alert'},"Permission for $s could not be set!") unless ($res > 0);
    }
    print '  Done. </h3></center>';
}

sub get_per($$)
{
    my $nu = $_[0];
    my $t = $_[1];
    my ($u,$p) = &get_id();
    my $m = &RFC::Main::passwd_client('get_per',$u,$p,$nu,$t,1);
    my $res = 'none';
    $res = 'read' if ($m == 1);
    $res = 'create' if ($m == 2);
    $res = 'modify' if ($m == 3);
    return $res;
}


sub new_user_form
{
    print center(h3("Creating new user for $name"));
    print center(h3("Usernames must only be 4 characters in length (Risoe ID!)"));
    print start_form;
    &print_hidden;
    print '<center>';
    print '<table border=0 cellspacing=10 cellpadding=0>';
    print '<tr><th>';
    print p("Username");
    print '</th><th>';
    print textfield(-name=>'nuser',-value=>'',-force=>'1');
    print '</th></tr><tr><th>';
    print p("Real name");
    print '</th><th>';
    print textfield(-name=>'nname',-value=>'',-force=>'1');
    print '</th></tr><tr><th>';
    print p("Email address");
    print '</th><th>';
    print textfield(-name=>'email',-value=>'',-force=>'1');
    print '</th></tr></table>';
    print submit(-name=>'action',-value=>'Create user',-class=>'s4',-style=>'width:200px;');
    print end_form;
    print p();
    print '<center>';
    &ret;
    print '</center>';
}

sub cusr
{
    my ($user,$pass) = &get_id;
    my $nuser = '';
    $nuser = param('nuser') if (defined(param('nuser')));
    $nuser =~ /(\w*)/;
    $nuser = $1;
    my $nname = param('nname') if (defined(param('nname')));
    my $email = undef;
    $email = param('email') if (defined(param('email')));
    if ($email =~ /([\w\.\-]*\@[\w\.\-]*)/)
    {
	$email = $1;
    }
    if (RFC::RFCCGI::is_user($nuser))
    {
	print center(h3({-class=>'Alert'},"User already exsists!"));
    }
    else
    {
	if (length($nuser) == 4)
	{
	    my $res = RFC::Main::passwd_client('adduser',$nuser,$nname,$email);
	    if ($res > 0)
	    {
		print center(h3({-class=>'Bl'},"User $nuser created, password will be sent to $email within a few minutes"));
	    }
	    else
	    {
		print center(h3({-class=>'Bl'},"User Not created, error: $res"));
	    }
	}
	else
	{
	    print center(h3({-class=>'Alert'},"Username not 4 characters!!"));
	}
    }
}

