# # Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-05-15 12:40:04 # # # 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 $rua_data = shift; #Data hash as parameter # Validation for each field my $ret = ""; if ($ret eq "") {$ret = 'ok';} return $ret; } sub validate_PARAMS { my $c = shift; my $rua_data = shift; #Data hash as parameter # Validation for each field my $ret = ""; if (! TRUE) #validate $c->param('Account') {$ret .= 'Validation for Account failed';} if (! TRUE) #validate $c->param('User_Name') {$ret .= 'Validation for User_Name failed';} if (! TRUE) #validate $c->param('Shell') {$ret .= 'Validation for Shell failed';} if (! TRUE) #validate $c->param('Sudoer') {$ret .= 'Validation for Sudoer failed';} if (! TRUE) #validate $c->param('VPNClientAccess') {$ret .= 'Validation for VPNClientAccess failed';} if (! TRUE) #validate $c->param('ChrootDir2') {$ret .= 'Validation for ChrootDir2 failed';} if (! TRUE) #validate $c->param('ChrootDir') {$ret .= 'Validation for ChrootDir failed';} if (! TRUE) #validate $c->param('sshKeys') {$ret .= 'Validation for sshKeys failed';} 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_PARAMS { # Return a hash with the fields required which will be loaded into the shared data my $c = shift; my %ret = ( 'Data1'=>'Data for PARAMS', #Example # fields from Inputs in PARAMS $fields['PARAMS'] 'Account'=>'Account contents', 'User_Name'=>'User_Name contents', 'Shell'=>'Shell contents', 'Sudoer'=>'Sudoer contents', 'VPNClientAccess'=>'VPNClientAccess contents', 'ChrootDir2'=>'ChrootDir2 contents', 'ChrootDir'=>'ChrootDir contents', 'sshKeys'=>'sshKeys contents', ); return %ret; } # Get control data for table(s) # Define a constant hash for field name mapping use constant getAllUsers_FIELD_MAPPING => ( 'Account' => 'Source-for-Account', 'User_Name' => 'Source-for-User_Name', 'Shell_Access' => 'Source-for-Shell_Access', 'Sudo' => 'Source-for-Sudo', 'SSH_Keys' => 'Source-for-SSH_Keys', 'Chroot_Path' => 'Source-for-Chroot_Path', 'RSSH_+_VPN_Access' => 'Source-for-RSSH_+_VPN_Access', 'Modify' => 'Source-for-Modify' #'target_field2' => 'source_field2', # Add more mappings as needed ); sub actual_getAllUsers { my $c = shift; # Actual code for extracting getAllUsers $cdb = esmith::ConfigDB->open(); $adb = esmith::AccountsDB->open(); my @data = (); my @users = $adb->users; return $c->l("ACCOUNT_USER_NONE") if (@users == 0); for my $user (@users) { my $username = $user->key; # make clearer by only showing yes and localise my $vpn = $user->prop('VPNClientAccess') || ''; if ($vpn eq 'yes') { $vpn = 'YES'; } else { $vpn = ''; } my $sudo = $user->prop('Sudoer') || ''; if ($sudo eq 'yes') { $sudo = 'YES'; } else { $sudo = ''; } my $keys = ''; my $file = "/home/e-smith/files/users/$username/.ssh/authorized_keys2"; if (( -e $file ) && (! -z $file )) { $keys = 'YES'; } my $shell = $user->prop('Shell') || ''; if ($shell eq '/usr/bin/rssh') { $shell = ''; } my $ChrootDir = $user->prop('ChrootDir') || ""; $ChrootDir =~ s:/home/e-smith/files/ibays/::; $ChrootDir =~ s:/home/e-smith/files/users/$username/home:home:; push @data, { User => $user->key, FullName => $user->prop('FirstName') . " " . $user->prop('LastName'), Sudoer => $user->prop('Sudoer') || 'no', VPNClientAccess => $user->prop('VPNClientAccess') || 'no', shell => $shell, chroot => $ChrootDir, ChrootDir => $user->prop('ChrootDir') || "/home/e-smith/files/users/$username/home", sudo => $c->l($sudo), keys => $c->l($keys), vpn => $c->l($vpn), Modify => $c->l('MODIFY'), } } return @data; } sub get_getAllUsers { # Return an array of hashes of the contents for each row and column for getAllUsers my $c = shift; my @source_records = $c->actual_getAllUsers(); my @transformed_records; my %Field_Mapping = getAllUsers_FIELD_MAPPING(); # Iterate over each record in the source array for my $source_record (@source_records) { my %transformed_record; # Iterate over each key-value pair in the $Field_Mapping constant while (my ($target, $source) = each %Field_Mapping) { # Check if the source field exists in the source record if (exists $source_record->{$source}) { # Assign the source field value to the target field in the transformed record $transformed_record{$target} = $source_record->{$source}; } } # Add transformed record to the array if it's not empty push @transformed_records, \%transformed_record if %transformed_record; } 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_PARAMS { 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 $rua_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_PARAMS { my $c = shift; my $rua_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 (! TRUE) #copy or perform with value: Account e.g. $db->set_prop($dbkey,'Account',$c->param('Account'),type=>'service')) {$ret .= 'Perform/save failed for Account';} if (! TRUE) #copy or perform with value: User_Name e.g. $db->set_prop($dbkey,'User_Name',$c->param('User_Name'),type=>'service')) {$ret .= 'Perform/save failed for User_Name';} if (! TRUE) #copy or perform with value: Shell e.g. $db->set_prop($dbkey,'Shell',$c->param('Shell'),type=>'service')) {$ret .= 'Perform/save failed for Shell';} if (! TRUE) #copy or perform with value: Sudoer e.g. $db->set_prop($dbkey,'Sudoer',$c->param('Sudoer'),type=>'service')) {$ret .= 'Perform/save failed for Sudoer';} if (! TRUE) #copy or perform with value: VPNClientAccess e.g. $db->set_prop($dbkey,'VPNClientAccess',$c->param('VPNClientAccess'),type=>'service')) {$ret .= 'Perform/save failed for VPNClientAccess';} if (! TRUE) #copy or perform with value: ChrootDir2 e.g. $db->set_prop($dbkey,'ChrootDir2',$c->param('ChrootDir2'),type=>'service')) {$ret .= 'Perform/save failed for ChrootDir2';} if (! TRUE) #copy or perform with value: ChrootDir e.g. $db->set_prop($dbkey,'ChrootDir',$c->param('ChrootDir'),type=>'service')) {$ret .= 'Perform/save failed for ChrootDir';} if (! TRUE) #copy or perform with value: sshKeys e.g. $db->set_prop($dbkey,'sshKeys',$c->param('sshKeys'),type=>'service')) {$ret .= 'Perform/save failed for sshKeys';} 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;