Skip to content

Instantly share code, notes, and snippets.

@martinvonwittich
Created May 24, 2019 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinvonwittich/58a36617d9f32c985efcf2636d5a6c1f to your computer and use it in GitHub Desktop.
Save martinvonwittich/58a36617d9f32c985efcf2636d5a6c1f to your computer and use it in GitHub Desktop.
A simple Perl script to disable notifications for a set of Zammad users
#!/usr/bin/perl -CSDAL
use warnings;
use strict;
use utf8;
use Getopt::Long;
use JSON;
use Path::Tiny;
use REST::Client;
use URI;
#use DDP;
my $opt = {};
my $zammad_users;
my $zammad_roles = {};
my $zammad_rolenames = {};
# Zammad REST API token
my $token = path("zammad.auth")->slurp_utf8
or die "Cannot read token from zammad.auth!\n";
my $zammad_url = "https://zammad.example.tld";
my $zammad_user_pattern = 'email:@example.tld';
# default notification settings for Zammad
my $default_notification_matrix = {
create => {
channel => {
email => $JSON::false,
online => $JSON::true,
},
criteria => {
owned_by_me => $JSON::true,
},
},
escalation => {
channel => {
email => $JSON::false,
online => $JSON::true,
},
criteria => {
owned_by_me => $JSON::true,
},
},
reminder_reached => {
channel => {
email => $JSON::false,
online => $JSON::true,
},
criteria => {
owned_by_me => $JSON::true,
},
},
update => {
channel => {
email => $JSON::false,
online => $JSON::true,
},
criteria => {
owned_by_me => $JSON::true,
},
},
};
# Zammad REST client
my $client = REST::Client->new;
$client->addHeader("Authorization", "Token token=$token");
$client->addHeader("Content-Type", "application/json");
$client->setHost($zammad_url);
# generic Zammad REST query
sub query_zammad($$;$$)
{
my ($method, $url, $arguments, $contents) = @_;
my $uri = URI->new($url);
if (defined $arguments)
{
$uri->query_form($uri->query_form,
%$arguments);
}
my $data = [];
my $current_page = 1;
my $per_page = 500;
while(1)
{
my $paged_uri = $uri->clone;
$paged_uri->query_form($paged_uri->query_form,
page => $current_page,
per_page => $per_page,
) if $method eq "GET";
my $res = $client->request($method, $paged_uri, $contents);
if ($res->responseCode ne "200")
{
die sprintf("unexpected response %s while querying Zammad for %s %s",
$res->responseCode, $method, $paged_uri);
}
my $partial_data = decode_json($res->responseContent);
if (ref $partial_data eq "ARRAY")
{
last if !@$partial_data;
$data = [ @$data, @$partial_data ];
}
else
{
return $partial_data;
}
$current_page++;
}
return $data;
}
# query Zammad users
sub query_zammad_users()
{
my $users = query_zammad("GET", "/api/v1/users/search",
{ query => $zammad_user_pattern });
for my $user (@$users)
{
$zammad_users->{$user->{email}} = {
email => $user->{email},
roles => { map { $zammad_roles->{$_} => undef } @{$user->{role_ids}} },
id => $user->{id},
user => $user,
};
}
}
# query Zammad roles
sub query_zammad_roles()
{
my $roles = query_zammad("GET", "/api/v1/roles");
# create a { role_id => role_name } hash
for my $role (@$roles)
{
$zammad_roles->{$role->{id}} = $role->{name};
}
# create a { role_name => role_id } hash
for my $role_id (keys %$zammad_roles)
{
$zammad_rolenames->{$zammad_roles->{$role_id}} = $role_id;
}
}
sub disable_notifications_for_all_users
{
for my $email (keys %$zammad_users)
{
my $user = $zammad_users->{$email};
# limit effect to a single user for testing
#next if $email ne 'my.account@example.tld';
printf "config %s notifications\n", $email;
my $cfg = {
preferences => {
notification_config => {
matrix => $default_notification_matrix,
},
},
};
if ($opt->{repair})
{
my $res = query_zammad "PUT",
sprintf("/api/v1/users/%s", $user->{id}),
undef, encode_json($cfg);
}
}
}
GetOptions($opt,
"repair|r",
"help|h"
);
if ($opt->{help})
{
print "Usage: $0 [--repair]\n";
print "\n";
print " --repair, -r apply changes to Zammad\n";
print " --help, -h this help\n";
exit;
}
query_zammad_roles;
query_zammad_users;
#p $zammad_roles;
#p $zammad_users;
disable_notifications_for_all_users;
put_token_here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment