Extending model.

This commit is contained in:
sfigato 2019-04-28 15:34:14 +02:00
parent b1837f80e4
commit 4e2cadaa92
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -44,13 +44,23 @@ def inheritance(obj, attr, root_cls=object):
return res.strip(",") return res.strip(",")
class Entry: async def iter_children(children):
return [child async for child in children]
class Entry(object):
""" """
LDAP Entry. Interface to LDAP. LDAP Entry. Interface to LDAP.
""" """
kind = None kind = None
_name = None _name = None
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
@classmethod @classmethod
def name(cls): def name(cls):
@ -66,16 +76,42 @@ class Entry:
self.client = client self.client = client
self.base_dn = client.base_dn self.base_dn = client.base_dn
def __repr__(self):
return f"<{call_if_callable(self, 'name')} {self.dn}>"
async def get_children(self):
async with self.client.connect(is_async=True) as conn:
for el in await conn.search(self.dn, 2):
yield el
@property
def children(self):
"""
Synchronous property to enumerate the children.
"""
return asyncio.run(iter_children(self.get_children()))
@property @property
def dn(self): def dn(self):
return "{},{}".format(inheritance(self, "qualified_name", Entry), self.base_dn) return "{},{}".format(inheritance(self, "qualified_name", Entry), self.base_dn)
async def describe(self): async def describe(self):
async with self.client.connect(is_async=True) as conn: async with self.client.connect(is_async=True) as conn:
res = await conn.search(self.dn, 2) res = await conn.search(self.dn, 0)
return res return res
async def build_heritage(obj, ChildClass, attribute_id="uid"):
"""
Given the object and the child class, yields the
instances of the children.
"""
async for child in obj.get_children():
if attribute_id in child:
_name = child[attribute_id][0]
yield ChildClass(_name, obj.client)
class Hackers(Entry): class Hackers(Entry):
""" """
This class is where Users belong. This class is where Users belong.
@ -83,6 +119,13 @@ class Hackers(Entry):
kind = "ou" kind = "ou"
def __init__(self, client, *args, **kwargs):
super().__init__(client)
self._hackers = build_heritage(self, User)
def __aiter__(self):
return self._hackers
class Robots(Entry): class Robots(Entry):
""" """
@ -92,19 +135,51 @@ class Robots(Entry):
kind = "ou" kind = "ou"
_name = "Services" _name = "Services"
def __init__(self, client, *args, **kwargs):
super().__init__(client)
self._robots = build_heritage(self, Service)
def __aiter__(self):
return self._robots
class Congregations(Entry):
"""
This class is where Groups belong.
"""
kind = "ou"
_name = "Groups"
def __init__(self, client, *args, **kwargs):
super().__init__(client)
self._groups = build_heritage(self, Group, attribute_id="cn")
def __aiter__(self):
return self._groups
class User(Hackers): class User(Hackers):
""" """
This class models a user. User may have attributes This class models a user. Users may have attributes
and belong to Groups. and belong one or more Group(s).
""" """
kind = "uid" kind = "uid"
_instances = dict()
def __new__(cls, name, *args, **kwargs):
if name not in cls._instances:
cls._instances[name] = object.__new__(cls)
return cls._instances[name]
def __init__(self, name, *args, **kwargs): def __init__(self, name, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.name = name self.name = name
def __repr__(self):
return f"<{get_class(self).__name__}({self.name}) {self.dn}>"
def qualified_name(self): def qualified_name(self):
return "{}={}".format(self.kind, self.name) return "{}={}".format(self.kind, self.name)
@ -119,8 +194,9 @@ class User(Hackers):
class Service(Robots): class Service(Robots):
""" """
This class models a user. User may have attributes This class models a system user (i.e. users that are ancillary to
and belong to Groups. services on a machine). System user may have attributes
and belong to one or more Group(s).
""" """
kind = "uid" kind = "uid"
@ -129,6 +205,36 @@ class Service(Robots):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.name = name self.name = name
def __repr__(self):
return f"<{get_class(self).__name__}({self.name}) {self.dn}>"
def qualified_name(self):
return "{}={}".format(self.kind, self.name)
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
class Group(Congregations):
"""
This class models a group. Groups may have attributes
and may have Users and Services belonging to them.
"""
kind = "uid"
def __init__(self, name, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = name
def __repr__(self):
return f"<{get_class(self).__name__}({self.name}) {self.dn}>"
def qualified_name(self): def qualified_name(self):
return "{}={}".format(self.kind, self.name) return "{}={}".format(self.kind, self.name)