Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions bin/bulk_layout_permissions.pl
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:";
Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated
my $file = <STDIN>;
Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated
chomp($file);

-f $file or die "ERROR: File '$file' does not exist";
Comment thread
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 ();
Comment thread
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;
Comment thread
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";
Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated
}
unless (defined $group_id && $group_id =~ /^\d+$/) {
Comment thread
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)$/) {
Comment thread
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";
Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated
}

rset('Layout')->find($layout_id) or die "ERROR: Layout ID '$layout_id' does not exist\n";
Comment thread
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";
Comment thread
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';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated
! -f $export_file or die "ERROR: $export_file already exists, unable to overwrite file!";
Comment thread
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";
Comment thread
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: $!";
Comment thread
Oliver-ctrlo marked this conversation as resolved.
Outdated

my @layout_groups = rset('LayoutGroup')->search(
{
'me.group_id' => { '-in' => \@groups },
},
{
prefetch => [ 'layout' ],
}
)->all;
Comment thread
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'";
}
Loading