Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Imports
- from gitlab3 import GitLab, ACCESS_LEVEL_DEVELOPER
- import simpleldap, ldap
- # Connect to LDAP and get members of a specific LDAP group
- conn = simpleldap.Connection('ldap-example.com',dn='cn=binduser,dc=example,dc=com',password='****')
- conn.set_search_defaults(base_dn='dc=example,dc=com',scope=ldap.SCOPE_SUBTREE)
- inldap = conn.get('cn=admincloud')['memberuid']
- # Connect to Gitlab and list existing users
- gl = GitLab('http://localhost')
- gl.login('root','*****')
- users = gl.users()
- ingitlab = [ str(user.username) for user in users ]
- # Get the GitLab Group
- g = gl.get_group('admincloud')
- # List members of Git Lab group (except standard users)
- ingroup = [ user.id for user in g.members() ]
- ingroup = map(lambda x: gl.user(x), ingroup)
- ingroup = [ user for user in ingroup if len(user.identities) > 0 ]
- ingroup = map(lambda x: str(x.username), ingroup)
- # List users which are in ldap but not in Gitlab and create them
- tocreate = [ user for user in inldap if user not in ingitlab ]
- for user in tocreate:
- usr = conn.get('uid=%s' % user)
- u = gl.add_user(usr['mail'][0],'123456789',usr['uid'][0],usr['cn'][0],extern_uid=usr.dn,provider='ldapmain')
- # List users which are in Gitlab group but not in the ldap Group
- toremove = [ user for user in ingroup if user not in inldap ]
- for user in toremove:
- g.delete_member(g.find_member(username=user))
- # List users which are in ldap group but not in the Gitlab group
- toadd = [ user for user in inldap if user not in ingroup ]
- for user in toadd:
- g.add_member(gl.find_user(username=user).id,ACCESS_LEVEL_DEVELOPER)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement