forked from KohaSuomi/Koha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermission_rewrite_compat.pl
175 lines (147 loc) · 5.6 KB
/
permission_rewrite_compat.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/perl
use Modern::Perl;
use Getopt::Long;
use Text::CSV;
my $csv = Text::CSV->new({binary => 1, allow_whitespace => 1, always_quote => 1, quote_char => "'", escape_char => "'",});
my @moduleBitToModuleName;
my ($help, $verbose, $revert, $dryrun);
GetOptions(
'h|help' => \$help,
'r|revert' => \$revert,
'v|verbose' => \$verbose,
'd|dry-run' => \$dryrun,
);
if ($help) {
print <<HELP;
For feature Bug 14540 - PermissionManager,
Refactors userflags.sql and userpermissions.sql
to the new Koha::Auth::PermissionManager -model
so you don't have to...
Makes backups with .orig-extension
-r --revert Revert backups
-d --dry-run Make changes to files but don't push to DB
-h --help This dandy help
HELP
exit;
}
my $quiet = ($verbose) ? '' : ' -q ';
my $userflags_file = `find -name userflags.sql -not -path "*/blib/*"`;
chomp $userflags_file;
my $userperms_file = `find -name userpermissions.sql -not -path "*/blib/*"`;
chomp $userperms_file;
die "File $userflags_file is not writable" unless (-w $userflags_file);
die "File $userperms_file is not writable" unless (-w $userperms_file);
## First process userflags.
sub userflags {
print `wget $quiet https://raw.githubusercontent.com/Koha-Community/Koha/master/installer/data/mysql/userflags.sql -O $userflags_file` or die "Couldn't get userflags.sql from github";
open(my $FH, '<', $userflags_file) or die $!;
my $userflags_sql_header = 'INSERT IGNORE INTO permission_modules (module, description) VALUES';
my @userflags_sql;
while (<$FH>) {
if ($_ =~ /^INSERT INTO/) { #replace the first INSERT INTO row with the new header instead
print sprintf("%4d: %10s '%s'", $., 'Skipping', $_) if $verbose;
next;
}
elsif ($_ =~ /^\s*\((.+?)\),?$/) {
print sprintf("%4d: %10s '%s'", $., 'Parsing', $_) if $verbose;
#example row (0,'superlibrarian','Access to all librarian functions',0),
#we want this ('superlibrarian','Access to all librarian functions'),
my $status = $csv->parse( $1 );
my @columns = $csv->fields();
unless (@columns) {
die "Line $.: ".$csv->error_diag();
}
if (@columns == 2) {
die "Line $.: Only 2 columns found from $userflags_file, row:\n$_\nLooks like this file is already transformed?";
}
$status = $csv->combine($columns[1],$columns[2]);
my $line = $csv->string();
unless ($line) {
die "Line $.: ".$csv->error_diag();
}
push(@userflags_sql, "($line)");
$moduleBitToModuleName[$columns[0]] = $columns[1]; #Translation table for permissions
}
elsif ($_ =~ /^;/) {
print sprintf("%4d: %10s '%s'", $., 'Closing', $_) if $verbose;
last;
}
else {
die "Line $.: Unknown row\n$_";
}
}
close($FH);
my $userflags_sql = $userflags_sql_header."\n".join(",\n", @userflags_sql)."\n"."; SHOW WARNINGS;";
print `mv $userflags_file $userflags_file.orig`;
open($FH, '>', $userflags_file);
print $FH $userflags_sql;
close($FH);
unless ($dryrun) {
print sprintf("%4d: %10s '%s'\n", $., 'Importing', $userflags_file) if $verbose;
`mysql < $userflags_file` or die "Pushing userflags to DB failed";
}
}
## Then process userpermissions.sql
sub userpermissions {
print `wget $quiet https://raw.githubusercontent.com/Koha-Community/Koha/master/installer/data/mysql/userpermissions.sql -O $userperms_file` or die "Couldn't get userpermissions.sql from github";
open(my $FH, '<', $userperms_file) or die $!;
my $userpermissions_sql_header = 'INSERT IGNORE INTO permissions (module, code, description) VALUES';
my @userpermissions_sql;
while (<$FH>) {
if ($_ =~ /^INSERT INTO/) { #replace the first INSERT INTO row with the new header instead
print sprintf("%4d: %10s '%s'", $., 'Skipping', $_) if $verbose;
next;
}
elsif ($_ =~ /^\s*\((.+?)\),?$/) {
print sprintf("%4d: %10s '%s'", $., 'Parsing', $_) if $verbose;
#example row ( 1, 'circulate_remaining_permissions', 'Remaining circulation permissions'),
#we want this ( 'circulate', 'circulate_remaining_permissions', 'Remaining circulation permissions'),
my $status = $csv->parse( $1 );
my @columns = $csv->fields();
unless (@columns) {
die "Line $.: ".$csv->error_diag();
}
if ($columns[0] !~ /^\d+$/) {
die "Line $.: First column is not a digit in $userperms_file, row:\n$_\nLooks like this file is already transformed?";
}
unless ($columns[0]) {
die "Line $.: \$columns[0] is undef at line '$_'";
}
my $moduleName = $moduleBitToModuleName[$columns[0]];
$status = $csv->combine($moduleName,$columns[1],$columns[2]);
my $line = $csv->string();
unless ($line) {
die "Line $.: ".$csv->error_diag();
}
push(@userpermissions_sql, "($line)");
}
elsif ($_ =~ /^;/) {
print sprintf("%4d: %10s '%s'", $., 'Closing', $_) if $verbose;
last;
}
else {
die "Line $.: Unknown row\n$_";
}
}
close($FH);
my $userpermissions_sql = $userpermissions_sql_header."\n".join(",\n", @userpermissions_sql)."\n"."; SHOW WARNINGS;";
print `mv $userperms_file $userperms_file.orig`;
open($FH, '>', $userperms_file);
print $FH $userpermissions_sql;
close($FH);
unless ($dryrun) {
print sprintf("%4d: %10s '%s'\n", $., 'Importing', $userperms_file) if $verbose;
`mysql < $userflags_file` or die "Pushing userpermissions to DB failed";
}
}
sub revert {
print `mv $userperms_file.orig $userperms_file`;
print `mv $userflags_file.orig $userflags_file`;
}
if ($revert) {
revert;
}
else {
userflags;
userpermissions;
}