Skip to content

Instantly share code, notes, and snippets.

@discentem
Created October 28, 2017 23:27
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 discentem/b5e7fbe3824a80c3bc9dd6bd3b369522 to your computer and use it in GitHub Desktop.
Save discentem/b5e7fbe3824a80c3bc9dd6bd3b369522 to your computer and use it in GitHub Desktop.
# pylint: disable=missing-docstring
# pylint: disable=E1101
# pylint: disable=W1401
import os
import json
import subprocess
import requests
from slacker import Slacker
def get_headers(token=os.environ['SLACK_TOKEN']):
return {'Accept' : 'application/json',
'Authorization' : '{0} {1}'.format('Bearer', token)}
def slacker_object(token=os.environ['SLACK_TOKEN']):
return Slacker(token)
def get_activeuserlist(slack_obj):
response = slack_obj.users.list()
members = response.body['members']
activemembers = []
for user in members:
if user['deleted'] == False:
activemembers.append(user)
return activemembers
def get_email(slack_userdict):
return slack_userdict['profile']['email']
def get_scimuserdictionary(email):
base_url = 'https://api.slack.com/scim/v1/Users?filter=emails'
user_part = "eq \"{0}\"".format(email)
full_url = "{0} {1}".format(base_url, user_part)
response = requests.get(full_url, headers=get_headers())
return json.loads(response.text)
def get_emaillist(slack_users):
emails = []
for user in slack_users:
try:
email = get_email(user)
emails.append(str(email))
except KeyError:
pass
return emails
def activeslackuser(email):
user = get_scimuserdictionary(email)
return bool(user['Resources'][0]['active'])
def get_userurl(email):
user_attributes = get_scimuserdictionary(email)
return user_attributes['Resources'][0]['meta']['location']
def get_disabledadusers():
powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
cmd = "(Get-ADUser -Filter {Enabled -eq $false} -Properties mail).mail"
p = subprocess.Popen([powershell, cmd], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return (p.communicate()[0].decode("utf-8").replace('\r', '')).split('\n')
def main():
slack = slacker_object()
activeemails = get_emaillist(get_activeuserlist(slack))
disabledadusers = get_disabledadusers()
print(disabledadusers)
for email in activeemails:
if email in disabledadusers:
print(email, 'is orphaned.', '\n')
main()
# RESPONSE = requests.delete(get_userurl('<user>'),
# headers=get_headers()
# )
# print RESPONSE.status_code
# if RESPONSE.status_code == 200:
# print "User successfully deleted."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment