From 3d3043ce6b27ed615e77e5244febaf3469af561f Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Thu, 2 Jun 2022 01:49:40 -0400 Subject: [PATCH] add one-time script --- .../unsubscribe_expired_members.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 one_time_scripts/unsubscribe_expired_members.py diff --git a/one_time_scripts/unsubscribe_expired_members.py b/one_time_scripts/unsubscribe_expired_members.py new file mode 100755 index 0000000..ccfc491 --- /dev/null +++ b/one_time_scripts/unsubscribe_expired_members.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +""" +This is a script which unsubscribes expired members from csc-general. + +GSSAPI is used for SPNEGO authentication, so make sure to run `kinit` first. +Also, make sure to run this script from the top-level of the git directory +(see the sys.path hack below). +""" +import os +import sys + +import ldap3 +from zope import component + +sys.path.append(os.getcwd()) +from ceo_common.errors import UserNotSubscribedError +from ceo_common.interfaces import IConfig, IHTTPClient +from ceo_common.model import Config, HTTPClient, RemoteMailmanService + +# modify as necessary +CONFIG_FILE = '/etc/csc/ceod.ini' +NEW_MEMBER_LIST = 'csc-general' + +cfg = Config(CONFIG_FILE) +component.provideUtility(cfg, IConfig) +http_client = HTTPClient() +component.provideUtility(http_client, IHTTPClient) +mailman_srv = RemoteMailmanService() +LDAP_URI = cfg.get('ldap_server_url') +LDAP_MEMBERS_BASE = cfg.get('ldap_users_base') + +conn = ldap3.Connection(LDAP_URI, auto_bind=True, raise_exceptions=True) +conn.search(LDAP_MEMBERS_BASE, '(shadowExpire=1)', attributes=['uid']) +total_unsubscribed = 0 +for entry in conn.entries: + uid = entry.uid.value + try: + mailman_srv.unsubscribe(uid, NEW_MEMBER_LIST) + print(f'Unsubscribed {uid}') + total_unsubscribed += 1 + except UserNotSubscribedError: + print(f'{uid} is already unsubscribed') +print(f'Total unsubscribed: {total_unsubscribed}')