-
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 1 commit
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>; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| chomp($file); | ||
|
|
||
| -f $file or die "ERROR: File '$file' does not exist"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
|
|
||
| $file or die "Usage: $0 filename"; | ||
| my $csv = Text::CSV->new({ binary => 1 }) | ||
| or die "Cannot use CSV: ".Text::CSV->error_diag (); | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| 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; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| my $RowCount = $csv->record_number; | ||
|
|
||
| unless (defined $layout_id && $layout_id =~ /^\d+$/) { | ||
| die "ERROR: Invalid Layout ID '$layout_id' on row $RowCount.\n"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| } | ||
| unless (defined $group_id && $group_id =~ /^\d+$/) { | ||
|
Oliver-ctrlo marked this conversation as resolved.
|
||
| 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)$/) { | ||
|
Oliver-ctrlo marked this conversation as resolved.
|
||
| die "ERROR: Invalid Permission '$permission' on row $RowCount. Value must be read, write_new, write_existing, or write_existing_no_approval.\n"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| rset('Layout')->find($layout_id) or die "ERROR: Layout ID '$layout_id' does not exist\n"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| 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"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| 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
Author
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. Changed to a script argument instead so it would be the same for both import and export.
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| ! -f $export_file or die "ERROR: $export_file already exists, unable to overwrite file!"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| 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"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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: $!"; | ||
|
Oliver-ctrlo marked this conversation as resolved.
Outdated
|
||
|
|
||
| my @layout_groups = rset('LayoutGroup')->search( | ||
| { | ||
| 'me.group_id' => { '-in' => \@groups }, | ||
| }, | ||
| { | ||
| prefetch => [ 'layout' ], | ||
| } | ||
| )->all; | ||
|
Oliver-ctrlo marked this conversation as resolved.
|
||
|
|
||
| $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'"; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.