summaryrefslogtreecommitdiff
path: root/bin/pg-readonly
blob: b5cde4d6ecf66f1fafbad82450845bbe13b48e09 (plain)
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
#!/usr/bin/perl
#
# hack to update/add read-only permissions for a user on the db
#
# usage: pg-readonly freesideuser readonlyuser

use strict;
use DBI;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(dbdef);

my $user = shift or die &usage;
my $rouser = shift or die &usage;

my $dbh = adminsuidsetup $user;

foreach my $table ( dbdef->tables ) {
  $dbh->do("GRANT SELECT ON $table TO $rouser");
  $dbh->commit();
  if ( my $pkey = dbdef->table($table)->primary_key ) {
    $dbh->do("GRANT SELECT ON ${table}_${pkey}_seq TO $rouser");
    $dbh->commit();
  }
}

my @rt_tables = qw(
Attachments
Queues
Links
Principals
Groups
ScripConditions
Transactions
Scrips
ACL
GroupMembers
CachedGroupMembers
Users
Tickets
ScripActions
Templates
ObjectCustomFieldValues
CustomFields
ObjectCustomFields
CustomFieldValues
Attributes
sessions
);

foreach my $table ( @rt_tables ) {
  $dbh->do("GRANT SELECT ON $table TO $rouser");
  $dbh->commit();
  $dbh->do("GRANT SELECT ON ${table}_id_seq TO $rouser");
  $dbh->commit();
}