smeserver-domains/root/usr/share/smanager/lib/SrvMngr/Controller/Dungog_domains.pm

408 lines
11 KiB
Perl

package SrvMngr::Controller::Dungog_domains;
##----------------------------------------------------------------------
## heading : Network
## description : Domain pseudonyms
## navigation : 6000 736
##
## name : Dungog_domainsget, method : get, url : /dungog_domains, ctlact : Dungog_domains##main
## name : Dungog_domainspost,method : post, url : /dungog_domains, ctlact : Dungog_domains##do_Save
## name : Dungog_domains1, method : get, url : /dungog_domains1, ctlact : Dungog_domains##do_Email
## name : Dungog_domains2, method : get, url : /dungog_domains2, ctlact : Dungog_domains##do_Options
## name : Dungog_domains3, method : get, url : /dungog_domains3, ctlact : Dungog_domains##do_AddPseudo
## name : Dungog_domains4, method : get, url : /dungog_domains4, ctlact : Dungog_domains##do_DelPseudo
## routes : end
##
## Documentation : https://wiki.contribs.org/Domains
##----------------------------------------------------------------------
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use Locale::gettext;
use SrvMngr::I18N;
use SrvMngr qw(theme_list init_session);
use Data::Dumper;
use esmith::util;
use esmith::HostsDB;
use esmith::AccountsDB;
our $db = esmith::ConfigDB->open();
our $adb = esmith::AccountsDB->open();
use constant FALSE => 0;
use constant TRUE => 1;
our $domaindb = esmith::ConfigDB->open('/home/e-smith/db/domains')
or die "Unable to open domains db";
our $dungogdb = esmith::ConfigDB->open('/home/e-smith/db/dungog')
or die("Unable to open dungog db"); #Created by rpm
my %ddo_datas = ();
sub main {
#
# Main Panel
#
my $c = shift;
$c->app->log->info( $c->log_req );
my $title = $c->l('ddo_title');
my $modul = '';
$ddo_datas{first} = 'ddo_Intro';
$ddo_datas{trt} = 'LIST';
my @domains = get_domain_table($c);
my $empty = ( scalar @domains == 0 ); #Always be at least one?
$c->stash(
title => $title,
modul => $modul,
ddo_datas => \%ddo_datas,
domains => \@domains,
empty => $empty
);
$c->render( template => 'dungog_domains' );
}
sub do_Save {
#
# Save pressed
#
# Just Save data, restart email and log success
# .. and return to main panel
#
my $c = shift;
$c->app->log->info( $c->log_req );
my $title = $c->l('ddo_title');
my $modul = '';
my $domain = $c->param("domain");
#die("Save:$domain");
# Save it in here..
my $trt = $c->param("trt");
if ( $trt eq "SERVER" ) {
my $MailServer = $c->param('MailServer') || '';
#die($MailServer);
unless ( $MailServer eq 'skipMS' ) {
if ( $MailServer eq '' ) {
$domaindb->delete_prop( $domain, 'MailServer' );
}
else {
$domaindb->set_prop( $domain, 'MailServer', $MailServer );
}
}
system( "/sbin/e-smith/signal-event", "email-update" ) == 0
or die("Error occurred while restarting email.\n");
}
elsif ( $trt eq 'OPTIONS' ) {
my $owner = $c->param('Owner');
my $group = $c->param('Group');
#Check exists and create if necessary
my $rec = $dungogdb->get($domain)
|| $dungogdb->new_record( $domain, { type => 'domain' } );
$dungogdb->set_prop( $domain, 'owner', $owner );
$dungogdb->set_prop( $domain, 'group', $group );
}
elsif ( $trt eq 'EMAIL' ) {
my $resmsg = perform_pseudo_save($c);
if ( $resmsg ne "ok" ) {
$ddo_datas{error} = $resmsg; #Error Message
do_AddPseudo($c);
return
} else {
$ddo_datas{success} = 'ddo_SUCCESS';
do_Email($c);
return
}
}
$ddo_datas{success} = 'ddo_SUCCESS';
$ddo_datas{trt} = 'LIST';
my @domains = get_domain_table($c);
my $empty = ( scalar @domains == 0 ); #Always be at least one?
$c->stash(
title => $title,
modul => $modul,
ddo_datas => \%ddo_datas,
domains => \@domains,
empty => $empty
);
$c->render( template => 'dungog_domains' );
}
sub perform_pseudo_save {
#
# Save or create a new pseudonymn
# mode = new or modify
my $c = shift;
my $domain = $c->param('domain');
my $acct = $c->param('LocalAccount');
my $pseu = $c->param('pseudo');
my $Pseudonym = "$pseu\@$domain";
my $mode = $c->param('mode') | 'modify';
my $oldpseudo = $c->param('oldpseudo');
# get all pseudonyms for this domain
my @vdemail = ();
foreach my $account ( $adb->get_all_by_prop($domain,type=>'pseudonym') ) {
push( @vdemail, $account->key );
}
#don't overwite existing users
if ( $mode eq 'new' ) {
foreach (@vdemail) {
if ( $_ eq $pseu ) {
return 'ddo_pseudo_already_exists';
}
}
}
if ( $pseu =~ /^([a-zA-Z0-9][\-\_\.a-zA-Z0-9]*)$/ ) {
$pseu = $1;
}
else {
return $c->l( 'ddo_pseudo_unexpected_character', $pseu );
}
if ( $acct eq '' ) {
return 'ddo_need_local_account';
}
#Create it if necessary
my $rec = $adb->get($Pseudonym)
|| $adb->new_record( $Pseudonym, { type => 'pseudonym' } );
$adb->set_prop( $Pseudonym, 'Account', $acct );
#we changed the Pseudonym, so delete the old
if ($oldpseudo ne ''){
if ( $mode eq 'modify' and $oldpseudo ne $pseu) {
my $delpseudo = "$oldpseudo\@$domain";
if (my $delkey = $adb->get($delpseudo)) {
$delkey->delete or die("Unable to delete $Pseudonym from accounts:".$delkey->key);
}
}
}
return "ok";
}
sub do_Email {
my $c = shift;
$c->app->log->info( $c->log_req );
my %ddo_datas = ();
my $title = $c->l('ddo_Email_Title');
my $modul = '';
my $domain = $c->param("domain");
my $MailServer = $domaindb->get_prop($domain, 'MailServer');
my @pseudos = get_domain_pseudonyms( $c, $domain );
my $empty = ( scalar @pseudos == 0 );
$ddo_datas{trt} = 'EMAIL';
$ddo_datas{mailserver} = $MailServer;
$c->stash(
title => $title,
modul => $modul,
ddo_datas => \%ddo_datas,
pseudos => \@pseudos,
empty => $empty
);
$c->render( template => 'dungog_domains' );
}
sub do_Options {
my $c = shift;
$c->app->log->info( $c->log_req );
my $title = $c->l('ddo_modify_options_TITLE');
my $modul = '';
#Data for selection controls
my $domain = $c->param("domain");
my $owner = $dungogdb->get_prop( $domain, "owner" ) || 'admin';
my $group = $dungogdb->get_prop( $domain, "group" ) || 'everyone';
if ( $group eq 'Administrator' ) {
$group = $owner;
}
my @userlist = ( 'admin', 'everyone' );
my @grouplist = ( 'everyone', 'Administrator' );
push( @userlist, '-- groups --' );
foreach ( sort $adb->get_all_by_prop( type => 'group' ) ) {
push( @userlist, $_->key );
push( @grouplist, $_->key );
}
push( @userlist, '-- users --' );
foreach ( sort $adb->get_all_by_prop( type => 'user' ) ) {
push( @userlist, $_->key );
}
$ddo_datas{trt} = 'OPTIONS';
$ddo_datas{owner} = $owner;
$ddo_datas{group} = $group;
$c->stash(
title => $title,
modul => $modul,
ddo_datas => \%ddo_datas,
grouplist => \@grouplist,
userlist => \@userlist
);
$c->render( template => 'dungog_domains' );
}
sub do_DelPseudo {
#
# Delete pseudonym
#
my $c = shift;
$c->app->log->info( $c->log_req );
my $title = $c->l('ddo_title');
my $modul = '';
# Delete it in here..
my $pseu = $c->param('pseudo');
my $domain = $c->param('domain');
my $Pseudonym = "$pseu\@$domain";
my $delkey = $adb->get("$Pseudonym") or die("Unable to find $Pseudonym in accounts");
$delkey->delete or die("Unable to delete $Pseudonym from accounts:".$delkey->key);
$ddo_datas{success} = 'ddo_Delete_successful';
$ddo_datas{trt} = 'EMAIL';
do_Email($c);
}
sub do_AddPseudo {
#
# Add or edit pseudonym
#
my $c = shift;
$c->app->log->info( $c->log_req );
my $title = $c->l('ddo_Edit_Title');
my $modul = '';
my $domain = $c->param("domain");
my @accts = get_accts( $c, $domain );
$c->stash(
title => $title,
modul => $modul,
ddo_datas => \%ddo_datas,
accts => \@accts
);
$ddo_datas{trt} = 'ADD';
$c->render( template => 'dungog_domains' );
}
sub get_domain_pseudonyms {
my ( $c, $domain ) = @_;
# get all pseudonyms
my @vdemail = ();
foreach my $record ( $adb->get_all_by_prop( type => 'pseudonym' ) ) {
my $key = $record->key;
push( @vdemail, $key );
}
# find email for this domain
my @vdemail2 = ();
foreach (@vdemail) {
( my $u, my $d ) = split( /@/, $_ );
my $acct = $adb->get_prop( "$_", "Account" ) || '';
my %thisrow = ( 'Pseudonym' => $u, "user" => $acct );
push( @vdemail2, \%thisrow ) if ( $d eq $domain );
}
return @vdemail2;
}
sub get_domain_table {
#
# Return full table of all domains OR just the details for the specified one
#
my ( $c, $findDomain ) = @_; #$c and domain to find
$findDomain = FALSE if ( scalar(@_) <= 1 );
my $key;
my @virtualDomains = ();
foreach my $domain ( $domaindb->get_all_by_prop( type => 'domain' ) ) {
$key = $domain->key;
push( @virtualDomains, $key );
}
my $numDomains = @virtualDomains;
if ( $numDomains == 0 ) { return () }
else {
my $domain;
my @virtualDomains2d = ();
foreach $domain ( sort @virtualDomains ) {
my %properties = $domaindb->get($domain)->props;
my $test = $domaindb->get_prop( $domain, "dns" ) || '';
my $owner = $dungogdb->get_prop( $domain, "owner" ) || 'admin';
my $group = $dungogdb->get_prop( $domain, "group" ) || 'everyone';
my %thisrow;
%thisrow = (
"Domain" => $domain,
"Description" => $properties{'Description'},
#"Content" => $content,
"Delegation" => "$owner" . '::' . "$group"
);
push( @virtualDomains2d, \%thisrow );
if ($findDomain) {
if ( $domain eq $findDomain ) {
@virtualDomains2d = (); #Just return matching one
push( @virtualDomains2d, \%thisrow );
last;
}
} #Found the one needed
}
return @virtualDomains2d; #Return the lot
}
}
sub get_accts {
my ( $c, $domain ) = @_;
my $owner = $dungogdb->get_prop( $domain, "owner" ) || '';
my $group = $dungogdb->get_prop( $domain, "group" ) || 'everyone';
my @selected = [];
my $userlist = $adb->get_prop($group,"Members") || '';
if ( $group eq 'everyone' ) {
my @acctrecs = $adb->get_all_by_prop( type => "user" );
foreach my $acct (@acctrecs) {
push( @selected, $acct->key )
}
}
elsif ( $group eq 'Administrator' ) {
@selected = ($owner);
}
else {
@selected = split( /,/, $userlist );
}
return sort @selected;
}
1;