-
Notifications
You must be signed in to change notification settings - Fork 9
Script to import and export layout group permissions in bulk [D: 1825] #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| use FindBin; | ||
| use lib "$FindBin::Bin/../lib"; | ||
|
|
||
| use Dancer2; | ||
| use Dancer2::Plugin::DBIC; | ||
| use Getopt::Long; | ||
| use Text::CSV; | ||
| use feature 'say'; | ||
|
|
||
| my ($action); | ||
|
|
||
| GetOptions( | ||
| 'action=s' => \$action | ||
| ) or exit; | ||
|
|
||
| $action or die "ERROR: Please state if you want to import/export group permissions with --action"; | ||
|
|
||
| if ($action eq 'import') { | ||
| Import_permissions(); | ||
| } | ||
| elsif ($action eq 'export') { | ||
| Export_permissions(); | ||
| } | ||
| else { | ||
| die "ERROR: You must either import or export within --action"; | ||
| } | ||
|
|
||
| sub Import_permissions { | ||
| say "Enter the file you want to import from:"; | ||
| my $file = <STDIN>; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this not be a script argument? |
||
| chomp($file); | ||
|
|
||
| -f $file or die "ERROR: File '$file' does not exist"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
|
|
||
| $file or die "Usage: $0 filename"; | ||
| my $csv = Text::CSV->new({ binary => 1 }) | ||
| or die "Cannot use CSV: ".Text::CSV->error_diag (); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
| open my $fh, "<:encoding(utf8)", $file or die "$file: $!"; | ||
| my $guard = schema->txn_scope_guard; | ||
|
|
||
| while (my $row = $csv->getline($fh)) { | ||
| my ($layout_id, $group_id, $permission) = @$row; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes CSV is set up correctly - you can import the rows as a hash containing |
||
| my $RowCount = $csv->record_number; | ||
|
|
||
| unless (defined $layout_id && $layout_id =~ /^\d+$/) { | ||
| die "ERROR: Invalid Layout ID '$layout_id' on row $RowCount.\n"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
| } | ||
| unless (defined $group_id && $group_id =~ /^\d+$/) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
| 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)$/) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That permission is a left over from GADS 1.0 and is no longer used |
||
| } | ||
|
|
||
| rset('Layout')->find($layout_id) or die "ERROR: Layout ID '$layout_id' does not exist\n"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| rset('Group')->find($group_id) or die "ERROR: Group ID '$group_id' does not exist\n"; | ||
|
|
||
| my $existing = rset('LayoutGroup')->find({ | ||
| layout_id => $layout_id, | ||
| group_id => $group_id, | ||
| permission => $permission | ||
| }); | ||
|
|
||
| if ($existing) { | ||
| say "Skipping permission '$permission' for group '$group_id' on layout '$layout_id' because this permission already exists"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to say anything here? Could just put |
||
| next; | ||
| } | ||
|
|
||
| rset('LayoutGroup')->create({ | ||
| layout_id => $layout_id, | ||
| group_id => $group_id, | ||
| permission => $permission, | ||
| }); | ||
| say "Will add permission '$permission' to group '$group_id' for layout '$layout_id'"; | ||
| } | ||
| $guard->commit; | ||
| say "Import complete"; | ||
| } | ||
|
|
||
| sub Export_permissions { | ||
| my $export_file = './Group_permission_export.csv'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be beneficial for the filename to be unique? E.g. it could have the group IDs or the current datetime appended.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter? use |
||
| ! -f $export_file or die "ERROR: $export_file already exists, unable to overwrite file!"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| say "Enter the group IDs you're looking to export (e.g. 1,2,3): "; | ||
| my $input = <STDIN>; | ||
| 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"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| } | ||
|
|
||
| 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: $!"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
|
|
||
| my @layout_groups = rset('LayoutGroup')->search( | ||
| { | ||
| 'me.group_id' => { '-in' => \@groups }, | ||
| }, | ||
| { | ||
| prefetch => [ 'layout' ], | ||
| } | ||
| )->all; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth moving the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
|
|
||
| $csv->say($fh, [ 'Instance ID','Layout Name','Layout ID', 'Group ID','Permission' ]); | ||
|
|
||
| foreach my $lg (@layout_groups) { | ||
| my $layout = $lg->layout; | ||
| my $instance_id = $layout->instance_id; | ||
| my $layout_name = $layout->name ; | ||
| my $group_id = $lg->group_id; | ||
| my $layout_id = $lg->layout_id; | ||
| my $permission = $lg->permission; | ||
|
|
||
| $csv->say($fh, [ $instance_id, $layout_name, $layout_id, $group_id, $permission ]); | ||
| } | ||
| close($fh); | ||
| say "Successfully exported permissions to '$export_file'"; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.