aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/update_council_user_permissions
blob: 0463db8b7a8258b2483b67e746373b2426875853 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env perl
#
# script to add/remove a list of permissions to all staff belonging to a body.

use strict;
use warnings;
use v5.14;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../../setenv.pl";
}

use FixMyStreet::DB;

use Getopt::Long::Descriptive;

my ($opts, $usage) = describe_options(
    '%c %o',
    ['commit', 'whether to commit changes to the database'],
    ['permissions=s', 'comma seperated list of permissions to add/delete', { required => 1 }],
    ['council=s', 'name of council to update', { required => 1 }],
    ['mode' => 'hidden' => { one_of => [
        ['add', 'add permissions to council\'s users'],
        ['remove', 'remove permissions from council\'s users'],
    ], required => 1 }],
    ['help|h', 'print usage message and exit']
);
$usage->die if $opts->help;

if (!$opts->commit) {
    say "*** DRY RUN ***";
}

my $body = FixMyStreet::DB->resultset("Body")->find({ name => $opts->council});

if ($body) {
    my @permissions_list = map { Utils::trim_text($_) } split(',', $opts->permissions);

    my $staff = FixMyStreet::DB->resultset("User")->search({ from_body => $body->id });
    for my $user ( $staff->all ) {
        my $permissions = $user->user_body_permissions->search({
            body_id => $body->id,
            permission_type => { in => \@permissions_list}
        });
        if ( $opts->mode eq 'remove') {
            next unless $permissions->count;
            if ($opts->commit) {
                $permissions->delete;
            }
        } elsif ( $opts->mode eq 'add' ) {
            my %existing = map { $_->permission_type => 1 } $permissions->all;
            my @permissions_to_add = grep { !$existing{$_} } @permissions_list;
            next unless @permissions_to_add;
            if ($opts->commit) {
                for my $permission ( @permissions_to_add ) {
                    $user->user_body_permissions->create({
                        body_id => $body->id,
                        permission_type => $permission
                    });
                }
            }
        }
        say "updated permissions for user id " . $user->id;
    }
} else {
    say STDERR "Could not find " . $opts->council;
}