Script to import and export layout group permissions in bulk [D: 1825]#641
Script to import and export layout group permissions in bulk [D: 1825]#641Oliver-ctrlo wants to merge 1 commit into
Conversation
This script allows bulk importing and exporting group layout permissions.
pwlodarski-ctrlo
left a comment
There was a problem hiding this comment.
PR reviewed - couple comments added for consideration.
| die "ERROR: Invalid Group ID '$group_id' on row $RowCount.\n"; | ||
| } | ||
| unless (defined $permission && $permission =~ /^(?:read|write_new|write_existing|write_existing_no_approval|write_new_no_approval)$/) { | ||
| die "ERROR: Invalid Permission '$permission' on row $RowCount. Value must be read, write_new, write_existing, or write_existing_no_approval.\n"; |
There was a problem hiding this comment.
Would write_new_no_approval be included too?
There was a problem hiding this comment.
That permission is a left over from GADS 1.0 and is no longer used
| } | ||
|
|
||
| sub Export_permissions { | ||
| my $export_file = './Group_permission_export.csv'; |
There was a problem hiding this comment.
Would it be beneficial for the filename to be unique? E.g. it could have the group IDs or the current datetime appended.
| { | ||
| prefetch => [ 'layout' ], | ||
| } | ||
| )->all; |
There was a problem hiding this comment.
Would it be worth moving the $guard->commit; to below this search, just in case any exceptions are thrown?
There was a problem hiding this comment.
No - we want the guard to fall out of scope and therefore roll back any and all changes, rather than doing a partial commit if only some changes are made before the error.
droberts-ctrlo
left a comment
There was a problem hiding this comment.
Various observations - the majority of which are from the use of die rather than error or fault - you can also use info rather than say, but that's a matter of personal preference and not an issue.
|
|
||
| sub Import_permissions { | ||
| say "Enter the file you want to import from:"; | ||
| my $file = <STDIN>; |
There was a problem hiding this comment.
Could this not be a script argument?
| my $guard = schema->txn_scope_guard; | ||
|
|
||
| while (my $row = $csv->getline($fh)) { | ||
| my ($layout_id, $group_id, $permission) = @$row; |
There was a problem hiding this comment.
This assumes CSV is set up correctly - you can import the rows as a hash containing {column1 => value, column2 => value ... } where column1, column2, etc are the header of the file which would mitigate if the file data is in the incorrect order (currently, it will assume it's correct and at worst, import the data incorrectly, at best, die)
| } | ||
|
|
||
| sub Import_permissions { | ||
| say "Enter the file you want to import from:"; |
There was a problem hiding this comment.
Maybe set up the option to allow for logging here as well? Output information to a log file to show what's been done? Otherwise there's no audit trail and if something goes wrong we won't easily know what's borken.
| }); | ||
|
|
||
| if ($existing) { | ||
| say "Skipping permission '$permission' for group '$group_id' on layout '$layout_id' because this permission already exists"; |
There was a problem hiding this comment.
Do we need to say anything here? Could just put next if $existing
| my $file = <STDIN>; | ||
| chomp($file); | ||
|
|
||
| -f $file or die "ERROR: File '$file' does not exist"; |
There was a problem hiding this comment.
Should use error rather than die here (remember to import logreport)
| die "ERROR: Invalid Permission '$permission' on row $RowCount. Value must be read, write_new, write_existing, or write_existing_no_approval.\n"; | ||
| } | ||
|
|
||
| rset('Layout')->find($layout_id) or die "ERROR: Layout ID '$layout_id' does not exist\n"; |
There was a problem hiding this comment.
Should be error, not die
| } | ||
|
|
||
| sub Export_permissions { | ||
| my $export_file = './Group_permission_export.csv'; |
There was a problem hiding this comment.
Parameter? use --file or something and only set this if it's not set as a parameter (i.e. $export_file //= './Group_permission_export.csv'
|
|
||
| sub Export_permissions { | ||
| my $export_file = './Group_permission_export.csv'; | ||
| ! -f $export_file or die "ERROR: $export_file already exists, unable to overwrite file!"; |
There was a problem hiding this comment.
Should be error rather than die
| chomp($input); | ||
|
|
||
| if (!$input || $input !~ /^\s*\d+(?:\s*,\s*\d+)*\s*$/) { | ||
| die "Error: You need to enter a number or a comma-separated list of numbers.\n"; |
There was a problem hiding this comment.
Should be error rather than die
| my @groups = split(/\s*,\s*/, $input); | ||
|
|
||
| my $csv = Text::CSV->new({ binary => 1, auto_diag => 1 }); | ||
| open my $fh, ">:encoding(utf8)", $export_file or die "$export_file: $!"; |
There was a problem hiding this comment.
Should be fault rather than die - this will also remove the need for $!
This script allows bulk importing and exporting group layout permissions.