Merge branch 'main' of gitlab.beuth-hochschule.de:innovisionlab/ldapbodge

This commit is contained in:
Kai Kriegel 2021-12-17 14:51:46 +01:00
commit 2c37d1bc9c
2 changed files with 25 additions and 4 deletions

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# Usage
```shell
$ python3 main.py "ldaps://rsw011.beuth-hochschule.de" "xxxxxxxx@beuth-hochschule.de" "********"
```

23
main.py
View File

@ -1,7 +1,8 @@
import ldap import ldap
import toml import toml
import sys import sys
import secrets
import string
def init_ldap(server, user, pw): def init_ldap(server, user, pw):
con = ldap.initialize(server) con = ldap.initialize(server)
@ -15,5 +16,21 @@ if __name__ == '__main__':
con = init_ldap(server, user, pw) con = init_ldap(server, user, pw)
result = con.search_s('dc=beuth-hochschule,dc=de', result = con.search_s('dc=beuth-hochschule,dc=de',
ldap.SCOPE_SUBTREE, ldap.SCOPE_SUBTREE,
'|(title=Mechatronik Bachelor)(title=Mechatronik Master)', '(|(title=Mechatronik Bachelor)(title=Mechatronik Master))',
['cn']) ['uid'])
mtnrs = [matnr[1]['uid'][0].decode("utf-8") for matnr in result[:-3]]
pw = []
for i in range(len(mtnrs)):
alphabet = string.ascii_lowercase + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(8))
userdict = dict()
userdict["passwd"] = password
userdict["roles"] = ["default/internal", "open/internal"]
userdict["priority"] = 0
pw.append(userdict)
with open("users.toml", "w") as f:
toml.dump(dict(zip(mtnrs, pw)), f)