Advertisement
Guest User

LDAP Subscriptions Discord Prototype

a guest
May 21st, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | Source Code | 0 0
  1. import ldap3
  2. import discord
  3. from discord.ext import commands
  4.  
  5.  
  6. # LDAP server configuration
  7. LDAP_SERVER = 'ldap://lldap.slothflix.xyz:25628'
  8. LDAP_ADMIN_DN = 'uid=admin,ou=people,dc=example,dc=com'
  9. LDAP_ADMIN_PASSWORD = 'Password'
  10. LDAP_USERS_OU = 'ou=people,dc=example,dc=com'
  11. LDAP_GROUPS_OU = 'ou=groups,dc=example,dc=com'
  12.  
  13. # Discord bot configuration
  14. DISCORD_BOT_TOKEN = 'Token'
  15. SPECIFIED_ROLE = '900655128418349057'
  16. GROUP_TO_ADD = 'Subscriber'
  17. IGNORED_ROLES = ['900655128418349057', '900655128418349057']
  18. DISCORD_CHANNEL = '900643903525503058'
  19.  
  20. # Initialize LDAP server connection
  21. server = ldap3.Server(LDAP_SERVER, get_info=ldap3.ALL)
  22. admin_connection = ldap3.Connection(server, user=LDAP_ADMIN_DN, password=LDAP_ADMIN_PASSWORD, auto_bind=True)
  23.  
  24. # Initialize Discord bot
  25. intents = discord.Intents.all()
  26. intents.members = True
  27. bot = commands.Bot(command_prefix='/', intents=intents)
  28.  
  29. print('The Bot is running')
  30.  
  31. # Link LDAP user to Discord user
  32. @bot.command(name='link')
  33. @commands.has_role(900655128418349057)
  34. async def link(ctx, discord_username, ldap_username):
  35.     try:
  36.         # Check if LDAP user exists
  37.         admin_connection.search(LDAP_USERS_OU, '(uid={})'.format(ldap_username), attributes=['uid'])
  38.         if len(admin_connection.entries) == 0:
  39.             await ctx.send('The specified LDAP user does not exist.')
  40.             return
  41.  
  42.         # Link Discord user to LDAP user
  43.         user = ctx.guild.get_member(int(discord_username))
  44.         if user is None:
  45.             await ctx.send('The specified Discord user does not exist.')
  46.             return
  47.         else:
  48.             await ctx.send('Linking {} to LDAP user {}.'.format(user.name, ldap_username))
  49.             user_link_attribute = 'linkedLdapUser'
  50.             user_link_attribute_value = ldap_username
  51.             user_roles = [role.name for role in user.roles]
  52.  
  53.             # Check if user already has the link attribute
  54.             if user_link_attribute in user.public_flags:
  55.                 await ctx.send('This Discord user is already linked to an LDAP user.')
  56.                 return
  57.  
  58.             # Add link attribute to user
  59.             user_edit_dict = {user_link_attribute: user_link_attribute_value}
  60.             linked_ldap_user_flag = 1  # Choose a custom integer value for the flag
  61.             linked_ldap_user_role = discord.utils.get(ctx.guild.roles, name='linkedLdapUser')
  62.             if linked_ldap_user_role:
  63.                 await user.add_roles(linked_ldap_user_role)
  64.             else:
  65.                 await ctx.send('The linkedLdapUser role does not exist. Please create the role in the server.')
  66.  
  67.  
  68.  
  69.             # Add user to specified group if necessary
  70.         if SPECIFIED_ROLE not in user_roles:
  71.             for ignored_role in IGNORED_ROLES:
  72.                 if ignored_role in user_roles:
  73.                     await ctx.send('The specified user is an admin and will not be added to the group.')
  74.                     return
  75.  
  76.             linked_ldap_user_role = discord.utils.get(ctx.guild.roles, name='linkedLdapUser')
  77.             if linked_ldap_user_role:
  78.                 await user.add_roles(linked_ldap_user_role)
  79.             else:
  80.                 await ctx.send('The linkedLdapUser role does not exist. Please create the role in the server.')
  81.  
  82.             group_dn = 'cn={},{}'.format(GROUP_TO_ADD, LDAP_GROUPS_OU)
  83.             user_dn = 'uid={},{}'.format(ldap_username, LDAP_USERS_OU)
  84.             admin_connection.modify(group_dn, {'member': [(ldap3.MODIFY_ADD, [user_dn])]})
  85.             await ctx.send('Added user to {} group.'.format(GROUP_TO_ADD))
  86.     except Exception as e:
  87.         await ctx.send('An error occurred while linking the user. Error message: {}'.format(str(e)))
  88.  
  89.  
  90.  
  91. # Run the Discord bot
  92. bot.run(DISCORD_BOT_TOKEN)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement