package SrvMngr::Controller::Ibays;
#----------------------------------------------------------------------
# heading : Network
# description : Shared areas (was ibays)
# navigation : 6000 100
#
#
# routes : end
#----------------------------------------------------------------------
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use Locale::gettext;
use SrvMngr::I18N;
use SrvMngr qw( theme_list init_session is_normal_password );
use esmith::AccountsDB;
use esmith::ConfigDB;
use esmith::DomainsDB;
#use esmith::FormMagick::Panel::ibays;
our $adb = esmith::AccountsDB->open || die "Couldn't open accounts db";
our $cdb = esmith::ConfigDB->open() || die "Couldn't open config db";
sub main {
my $c = shift;
$c->app->log->info($c->log_req);
my %iba_datas = ();
my $title = $c->l('iba_FORM_TITLE');
$iba_datas{'trt'} = 'LIST';
my @ibays;
if ($adb)
{
@ibays = $adb->ibays();
}
$c->stash( title => $title, iba_datas => \%iba_datas, ibays => \@ibays );
$c->render(template => 'ibays');
};
sub do_display {
my $c = shift;
my $rt = $c->current_route;
my $trt = ($c->param('trt') || 'LIST');
my $ibay = $c->param('ibay') || '';
#$trt = 'DEL' if ( $ibay );
#$trt = 'ADD' if ( $rt eq 'ibayadd' );
my %iba_datas = ();
my $title = $c->l('iba_FORM_TITLE');
my $modul = '';
$iba_datas{'trt'} = $trt;
if ( $trt eq 'ADD' ) {
$iba_datas{ibay} = '';
$iba_datas{description} = '';
$iba_datas{group} = '';
$iba_datas{userAccess} = '';
$iba_datas{publicAccess} = '';
$iba_datas{CgiBin} = '';
$iba_datas{SSL} = '';
}
if ( $trt eq 'UPD' ) {
my $rec = $adb->get($ibay);
if ($rec and $rec->prop('type') eq 'ibay') {
$iba_datas{ibay} = $ibay;
$iba_datas{description} = $rec->prop('Name') || '';
$iba_datas{group} = $rec->prop('Group') || '';
$iba_datas{userAccess} = $rec->prop('UserAccess') || '';
$iba_datas{publicAccess} = $rec->prop('PublicAccess') || '';
$iba_datas{CgiBin} = $rec->prop('CgiBin') || 'disabled';
$iba_datas{SSL} = $rec->prop('SSL') || 'disabled';
}
}
if ( $trt eq 'DEL' ) {
my $rec = $adb->get($ibay);
if ($rec and $rec->prop('type') eq 'ibay') {
$iba_datas{ibay} = $ibay;
$iba_datas{description} = $rec->prop('Name') || '';
$modul .= print_vhost_message( $c, $ibay );
}
}
if ( $trt eq 'PWD' ) {
my $rec = $adb->get($ibay);
if ($rec and $rec->prop('type') eq 'ibay') {
$iba_datas{ibay} = $ibay;
$iba_datas{description} = $rec->prop('Name') || '';
}
}
if ( $trt eq 'LIST' ) {
my @ibays;
$adb = esmith::AccountsDB->open || die "Couldn't open accounts db";
if ($adb)
{
@ibays = $adb->ibays();
}
$c->stash( ibays => \@ibays );
}
$c->stash( title => $title, modul => $modul, iba_datas => \%iba_datas );
$c->render( template => 'ibays' );
};
sub do_update {
my $c = shift;
$c->app->log->info($c->log_req);
my $rt = $c->current_route;
my $trt = ($c->param('trt') || 'LIST');
my %iba_datas = ();
my $title = $c->l('iba_FORM_TITLE');
$iba_datas{'trt'} = $trt;
my $result = '';
my $res;
if ( $trt eq 'ADD' ) {
my $name = ($c->param('ibay') || '');
# controls
$res = validate_ibay( $c, $name );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$res = create_ibay( $c, $name );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('iba_SUCCESSFULLY_CREATED_IBAY') . ' ' . $name;
$iba_datas{trt} = 'LST';
}
}
}
if ( $trt eq 'UPD' ) {
my $name = ($c->param('ibay') || '');
# controls
$res = '';
if ( ! $result ) {
$res = modify_ibay( $c, $name );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('iba_SUCCESSFULLY_MODIFIED_IBAY') . ' ' . $name;
$iba_datas{trt} = 'LST';
}
}
}
if ( $trt eq 'PWD' ) {
my $ibay = ($c->param('ibay') || '');
my $pass1 = ($c->param('newPass') || '');
my $pass2 = ($c->param('newPassVerify') || '');
# controls
unless ($pass1 eq $pass2) {
$result .= $c->l('iba_IBAY_PASSWD_VERIFY_ERROR') . ' - ';
}
$res = check_password( $c, $pass1 );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$res = reset_password( $c, $ibay, $pass1 );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('iba_SUCCESSFULLY_RESET_PASSWORD') . ' ' . $ibay;
$iba_datas{trt} = 'LST';
}
}
}
if ( $trt eq 'DEL' ) {
my $ibay = $c->param ('ibay');
if ($ibay =~ /^([a-z][a-z0-9]*)$/) {
$ibay = $1;
} else {
$result .= $c->l('iba_ERR_INTERNAL_FAILURE') . ':' . $ibay;
}
if ( ! $result ) {
$res = remove_ibay( $c, $ibay );
$result .= $res unless $res eq 'OK';
if ( ! $result ) {
$result = $c->l('iba_SUCCESSFULLY_DELETED_IBAY') . ' ' . $ibay;
$iba_datas{trt} = 'LST';
}
}
}
# common parts
if ($res ne 'OK') {
$c->stash( error => $result );
$c->stash( title => $title, iba_datas => \%iba_datas );
return $c->render('ibays');
}
my $message = "'Ibays' updates ($trt) DONE";
$c->app->log->info($message);
$c->flash( success => $result );
$c->redirect_to('/ibays');
};
sub validate_ibay {
my ($c, $name) = @_;
my $msg = validate_name($c, $name);
unless ($msg eq "OK")
{
return ($msg);
}
$msg = max_ibay_name_length($c, $name);
unless ($msg eq "OK")
{
return ($msg);
}
$msg = conflict_check($c, $name);
unless ($msg eq "OK")
{
return ($msg);
}
return ('OK');
}
sub create_ibay {
my ($c, $name) = @_;
my $msg;
my $uid = $adb->get_next_uid();
if (my $acct = $adb->new_record($name, {
Name => $c->param('ibayDesc'),
CgiBin => $c->param('CgiBin'),
Group => $c->param('group'),
PublicAccess => $c->param('publicAccess'),
SSL => $c->param('SSL'),
UserAccess => $c->param('userAccess'),
Uid => $uid,
Gid => $uid,
PasswordSet => 'no',
type => 'ibay',
}) )
{
# Untaint $name before use in system()
$name =~ /(.+)/; $name = $1;
if (system ("/sbin/e-smith/signal-event", "ibay-create", $name) == 0) {
$msg = 'OK';
} else {
$msg = $c->l('iba_ERROR_WHILE_CREATING_IBAY');
}
} else {
$msg = $c->l('iba_CANT_CREATE_IBAY');
}
return $msg;
}
sub modify_ibay {
my ($c, $name) = @_;
my $msg;
if (my $acct = $adb->get($name)) {
if ($acct->prop('type') eq 'ibay') {
$acct->merge_props(
Name => $c->param('ibayDesc'),
CgiBin => $c->param('CgiBin'),
Group => $c->param('group'),
PublicAccess => $c->param('publicAccess'),
SSL => $c->param('SSL'),
UserAccess => $c->param('userAccess'),
);
# Untaint $name before use in system()
$name =~ /(.+)/; $name = $1;
if (system ("/sbin/e-smith/signal-event", "ibay-modify",
$name) == 0)
{
$msg = 'OK';
} else {
$msg = $c->l('iba_ERROR_WHILE_MODIFYING_IBAY');
}
} else {
$msg = $c->l('iba_CANT_FIND_IBAY');
}
} else {
$msg = $c->l('iba_CANT_FIND_IBAY');
}
return $msg;
}
sub print_vhost_message {
my $c = shift;
my $name = $c->param('ibay');
my $result = '';
my $domaindb = esmith::DomainsDB->open();
my @domains = $domaindb->get_all_by_prop(Content => $name);
my $vhostListItems = join "\n",
(map ($_->key." ".$_->prop('Description'), @domains));
if ($vhostListItems)
{
$result = $c->l('iba_VHOST_MESSAGE') . "