277 lines
8.1 KiB
Perl
277 lines
8.1 KiB
Perl
#
|
|
# Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-04-23 10:18:18
|
|
#
|
|
#
|
|
# Routines to be edited by the developer to provide content and validation for parameters
|
|
# and provison of the control data for table(s)
|
|
#
|
|
use esmith::util;
|
|
use esmith::util::network;
|
|
use esmith::ConfigDB;
|
|
use esmith::HostsDB;
|
|
use esmith::AccountsDB;
|
|
use esmith::NetworksDB;
|
|
use esmith::DomainsDB;
|
|
|
|
use constant FALSE => 0;
|
|
use constant TRUE => 1;
|
|
|
|
|
|
#The most common ones
|
|
#my $cdb
|
|
my $adb;
|
|
#my $ndb
|
|
#my $hdb
|
|
#my $ddb
|
|
|
|
# Validation routines - parameters for each panel
|
|
|
|
sub validate_USERTABLE {
|
|
my $c = shift;
|
|
my $usp_data = shift; #Data hash as parameter
|
|
# Validation for each field
|
|
my $ret = "";
|
|
|
|
if ($ret eq "") {$ret = 'ok';}
|
|
return $ret;
|
|
}
|
|
|
|
sub validate_PANELTABLE {
|
|
my $c = shift;
|
|
my $usp_data = shift; #Data hash as parameter
|
|
# Validation for each field
|
|
my $ret = "";
|
|
|
|
if ($ret eq "") {$ret = 'ok';}
|
|
return $ret;
|
|
}
|
|
|
|
|
|
# Get singleton data for each panel
|
|
|
|
sub get_data_for_panel_USERTABLE {
|
|
# Return a hash with the fields required which will be loaded into the shared data
|
|
my $c = shift;
|
|
my %ret = (
|
|
'Data1'=>'Data for USERTABLE', #Example
|
|
# fields from Inputs in USERTABLE $fields['USERTABLE']
|
|
|
|
);
|
|
return %ret;
|
|
}
|
|
|
|
sub get_data_for_panel_PANELTABLE {
|
|
# Return a hash with the fields required which will be loaded into the shared data
|
|
my $c = shift;
|
|
my %ret = (
|
|
'Data1'=>'Data for PANELTABLE', #Example
|
|
# fields from Inputs in PANELTABLE $fields['PANELTABLE']
|
|
|
|
);
|
|
return %ret;
|
|
}
|
|
|
|
|
|
|
|
use constant users_FIELD_MAPPING => (
|
|
'Account' => 'Name', # Output => Source
|
|
'Description' => 'Description',
|
|
'Modify' => 'Modify',
|
|
);
|
|
|
|
sub actual_users {
|
|
my @ret = ();
|
|
|
|
my $adb = esmith::AccountsDB->open() or die("Couldn't open Accounts db");
|
|
|
|
# Optionally add the local admin user
|
|
push @ret, {
|
|
'Name' => 'admin',
|
|
'Description' => 'Local Admin',
|
|
'Modify' => "<a href='userpanelsd?trt=PANELTABLE&Selected=admin'>Modify</a>"
|
|
};
|
|
|
|
my @users = $adb->users();
|
|
my @groups = $adb->groups();
|
|
my @accounts = (@users, @groups); # Combine into one array
|
|
|
|
foreach my $i (@accounts) {
|
|
my $username = $i->key();
|
|
my $userdesc = join(' ', grep { $_ } $i->prop('FirstName'), $i->prop('LastName'));
|
|
$userdesc ||= $i->prop('Description') || ''; # For groups, use 'Description' if no names
|
|
my $actionModify = "<a href='userpanelsd?trt=PANELTABLE&Selected=$username'>Modify</a>";
|
|
|
|
my %hash = (
|
|
'Name' => $username,
|
|
'Description' => $userdesc,
|
|
'Modify' => $actionModify
|
|
);
|
|
push @ret, \%hash;
|
|
}
|
|
return @ret;
|
|
}
|
|
|
|
sub get_users {
|
|
my $c = shift;
|
|
my @source_records = $c->actual_users();
|
|
my @transformed_records;
|
|
my %Field_Mapping = users_FIELD_MAPPING();
|
|
|
|
for my $source_record (@source_records) {
|
|
my %transformed_record;
|
|
while (my ($target, $source) = each %Field_Mapping) {
|
|
if (exists $source_record->{$source}) {
|
|
$transformed_record{$target} = $source_record->{$source};
|
|
}
|
|
}
|
|
push @transformed_records, \%transformed_record if %transformed_record;
|
|
}
|
|
return \@transformed_records;
|
|
}
|
|
use constant panels_FIELD_MAPPING => (
|
|
'Panel' => 'name',
|
|
'Description' => 'description',
|
|
'Select' => 'selection'
|
|
# Add more mappings as needed
|
|
);
|
|
|
|
sub actual_panels {
|
|
my $c = shift;
|
|
my %usp_data = shift;
|
|
my $adb = esmith::AccountsDB->open() or die("Couldn't open Accounts db");
|
|
my $accounts_key = $c->param('Selected');
|
|
# Pass it back to the template and then back to the perform routine via a hidden field
|
|
$c->stash(accounts_key=>$accounts_key);
|
|
#my $account = $adb->get_rec($accounts_key) || undef;
|
|
my $AdminPanels = $adb->get_prop($accounts_key,'AdminPanels') || ''; #"Backup,Quota"; #
|
|
|
|
#$c->log->info("Key:$accounts_key AdminPanels:$AdminPanels usp_data:$usp_data{accounts_key}");
|
|
|
|
# Clean and split AdminPanel string (handles comma, whitespace, and quotes)
|
|
$AdminPanels =~ s/["']//g; # Remove quotes
|
|
my %admin_panels = map { $_ => 1 } grep { length } split(/\s*,\s*/, $AdminPanels);
|
|
my $dir = '/usr/share/smanager/lib/SrvMngr/Controller';
|
|
my @panels;
|
|
|
|
opendir(my $dh, $dir) or die "Can't open $dir: $!";
|
|
FILE:
|
|
for my $file (grep { /\.pm$/ } readdir($dh)) {
|
|
#Skip the ones not real panels.
|
|
next FILE if $file =~ /-Custom/;
|
|
next FILE if $file =~ /^(Modules|Swttheme|Request)\.pm$/i;
|
|
#Pick up description from header in controller file
|
|
my $path = "$dir/$file";
|
|
open(my $fh, '<', $path) or next;
|
|
my $description = '';
|
|
while (my $line = <$fh>) {
|
|
if ($line =~ /^\s*#\s*description\s*:\s*(.+?)\s*$/i) {
|
|
$description = $1;
|
|
last;
|
|
}
|
|
}
|
|
close $fh;
|
|
|
|
# Remove .pm extension
|
|
(my $panelname = $file) =~ s/\.pm$//;
|
|
|
|
# Checkbox: checked if in admin_panels
|
|
my $checkbox = exists $admin_panels{$panelname}
|
|
? "<%= check_box 'panelAccess', value => '$panelname', checked => 'checked' %>"
|
|
: "<%= check_box 'panelAccess', value => '$panelname' %>";
|
|
|
|
push @panels, {
|
|
name => $panelname,
|
|
description => $description,
|
|
selection => $checkbox
|
|
};
|
|
}
|
|
closedir $dh;
|
|
return @panels;
|
|
}
|
|
|
|
|
|
sub get_panels {
|
|
my $c = shift;
|
|
# Return an arrayref of hashes for each panel with mapped fields
|
|
my @source_records = $c->actual_panels();
|
|
my @transformed_records;
|
|
my %Field_Mapping = panels_FIELD_MAPPING();
|
|
|
|
for my $source_record (@source_records) {
|
|
my %transformed_record;
|
|
while (my ($target, $source) = each %Field_Mapping) {
|
|
if (exists $source_record->{$source}) {
|
|
$transformed_record{$target} = $source_record->{$source};
|
|
}
|
|
}
|
|
push @transformed_records, \%transformed_record if %transformed_record;
|
|
}
|
|
#$c->log->info("get_panel:$usp_data{account_key}");
|
|
return \@transformed_records;
|
|
}
|
|
|
|
|
|
|
|
# Return hash with values from row in which link clicked on table
|
|
|
|
sub get_selected_USERTABLE {
|
|
my $c = shift;
|
|
my $selected = shift; #Parameter is name of selected row.
|
|
my $is_new_record = shift; #Indicates new record required (defaults)
|
|
my %ret = {};
|
|
return %ret;
|
|
}
|
|
|
|
sub get_selected_PANELTABLE {
|
|
my $c = shift;
|
|
my $selected = shift; #Parameter is name of selected row.
|
|
my $is_new_record = shift; #Indicates new record required (defaults)
|
|
my %ret = {};
|
|
return %ret;
|
|
}
|
|
|
|
|
|
#after sucessful modify or create or whatever and submit then perfom (if the params validate)
|
|
|
|
sub perform_USERTABLE {
|
|
my $c = shift;
|
|
my $usp_data = shift; #Data hash as parameter
|
|
my $ret = "";
|
|
my $db = $cdb; #maybe one of the others
|
|
my $dbkey = 'ChangeThis';
|
|
# To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g."
|
|
|
|
if ($ret eq "") {$ret = 'ok';}
|
|
return $ret;
|
|
}
|
|
|
|
sub perform_PANELTABLE {
|
|
my $c = shift;
|
|
my $usp_data = shift; #Data hash as parameter
|
|
my $ret = "";
|
|
my $db = $adb; #maybe one of the others
|
|
my $dbkey = 'ChangeThis';
|
|
# To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g."
|
|
# Return from the panel table is an array of the values for the checked boxes.
|
|
my @selected = @{$c->every_param('panelAccess')};
|
|
# my $count_of_checked = scalar @selected;
|
|
# $c->log->info("No of checked: $count_of_checked ");
|
|
my $AdminPanels = join(',', @selected);
|
|
#$c->log->info("Selected IDs: $joined");
|
|
#Write back to the property AdminPanels
|
|
my $adb = esmith::AccountsDB->open() or die("Couldn't open Accounts db");
|
|
my $accounts_key = $c->param('accounts_key'); #hidden field in form
|
|
#$c->log->info("AccKey: $accounts_key");
|
|
$adb->set_prop($accounts_key,'AdminPanels',$AdminPanels);
|
|
if ($ret eq "") {$ret = 'ok';}
|
|
return $ret;
|
|
}
|
|
|
|
|
|
sub create_link{
|
|
# WIP
|
|
my ($c,$route, $panel, $index) = @_;
|
|
my $link = "$route?trt=$panel&Selected=$index";
|
|
return $link;
|
|
}
|
|
1; |