Created
May 10, 2018 22:37
-
-
Save bocon13/9725fa7bc95b0e94fe688c03b373a9a0 to your computer and use it in GitHub Desktop.
Script for bulk removing users from a group in Crowd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import base64 | |
import json | |
import os | |
import sys | |
import urllib2 | |
if len(sys.argv) < 3: | |
print 'USAGE: %s <crowd group> <user filter>' % (os.path.basename(__file__)) | |
sys.exit(1) | |
URL_BASE = 'http://<Crowd domain or IP>:<Crowd port>/crowd/rest/usermanagement/1' | |
USERNAME = 'app-name' # Set up the application in the Crowd console | |
PASSWORD = 'app-password' | |
GROUP_NAME = sys.argv[1] | |
MATCH_STRING = sys.argv[2] | |
print 'Removing users from %s group whose emails contain "%s"' % (GROUP_NAME, MATCH_STRING) | |
def make_request(path, method='GET'): | |
url = URL_BASE + path if path.startswith('/') else path | |
req = urllib2.Request(url) | |
req.get_method = lambda: method | |
auth = base64.b64encode('%s:%s' % (USERNAME, PASSWORD)) | |
req.add_header('Authorization', "Basic %s" % auth) | |
req.add_header('Content-Type', "application/json") | |
req.add_header('Accept', "application/json") | |
return urllib2.urlopen(req) | |
# Get the users in the group | |
users = json.load(make_request('/group/user/direct?groupname=%s' % GROUP_NAME)) | |
for user in users['users']: | |
user_details = json.load(make_request(user['link']['href'])) | |
username = user['name'] | |
name = user_details['display-name'] | |
email = user_details['email'] | |
# Filter users by email address | |
if MATCH_STRING in email: | |
# Remove users that match the filter | |
print "%s <%s> (username: %s)" % (name, email, username) | |
path = '/group/user/direct?groupname=%s&username=%s' % (GROUP_NAME, username) | |
make_request(path, 'DELETE') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment