71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import re
|
|
from phi.logging import get_logger
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
def make_user_dict(client, entry):
|
|
attributes = entry["attributes"]
|
|
|
|
user = {}
|
|
user["uid"] = attributes["uid"][0]
|
|
user["dn"] = "uid={},ou=Hackers,{}".format(user["uid"], client.base_dn)
|
|
user["cn"] = attributes["cn"][0]
|
|
user["sn"] = attributes["sn"][0]
|
|
user["mail"] = attributes["mail"][0]
|
|
user["password"] = attributes["userPassword"][0]
|
|
|
|
return user
|
|
|
|
|
|
def get_uid_from_dn(client, dn):
|
|
uid = re.search("uid=(.+?),ou=Hackers,{}".format(client.base_dn), dn).group(1)
|
|
return uid
|
|
|
|
|
|
def make_group_dict(client, entry):
|
|
attributes = entry["attributes"]
|
|
|
|
cn = attributes["cn"][0]
|
|
dn = "cn={},ou=Groups,{}".format(cn, client.base_dn)
|
|
members = [get_uid_from_dn(client, u_dn) for u_dn in attributes["member"]]
|
|
|
|
group = {}
|
|
group["dn"] = dn
|
|
group["cn"] = cn
|
|
group["members"] = members
|
|
|
|
return group
|
|
|
|
|
|
def get_response(client, response_id):
|
|
response, result, request = client.connection.get_response(
|
|
response_id, get_request=True
|
|
)
|
|
|
|
log.debug("Request: {}".format(request))
|
|
log.debug("Response: {}".format(response))
|
|
log.debug("Result: {}".format(result))
|
|
|
|
if result["description"] is not "success":
|
|
raise Exception(result["description"])
|
|
|
|
return response
|
|
|
|
|
|
def add_entry(client, dn, attributes):
|
|
log.info(
|
|
"Adding entry with distinguiscet name: {}"
|
|
"and attributes {}".format(dn, attributes)
|
|
)
|
|
response_id = client.connection.add(dn, attributes=attributes)
|
|
response = get_response(client, response_id)
|
|
return response
|
|
|
|
|
|
def delete_entry(client, dn):
|
|
log.info("Deleting entry with distinguiscet name: {}")
|
|
response_id = client.connection.delete(dn)
|
|
response = get_response(client, response_id)
|
|
return response
|