|
| 1 | +from bonsai import LDAPEntry, LDAPSearchScope, errors |
| 2 | +from fastapi import HTTPException |
| 3 | +from pydantic import BaseModel, Field |
| 4 | + |
| 5 | +from freenit.config import getConfig |
| 6 | +from freenit.models.ldap.base import LDAPBaseModel, get_client, save_data, class2filter |
| 7 | + |
| 8 | +config = getConfig() |
| 9 | + |
| 10 | + |
| 11 | +class Domain(LDAPBaseModel): |
| 12 | + ou: str = Field("", description=("Domain name")) |
| 13 | + |
| 14 | + @classmethod |
| 15 | + def from_entry(cls, entry): |
| 16 | + domain = cls(dn=str(entry["dn"]), ou=entry["ou"][0]) |
| 17 | + return domain |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def create(cls, fqdn): |
| 21 | + dn = f"{config.ldap.domainDN},{config.ldap.roleBase}" |
| 22 | + rdomain = Domain(dn=dn.format(fqdn), ou=fqdn) |
| 23 | + dn = f"{config.ldap.domainDN},{config.ldap.userBase}" |
| 24 | + udomain = Domain(dn=dn.format(fqdn), ou=fqdn) |
| 25 | + return (rdomain, udomain) |
| 26 | + |
| 27 | + @classmethod |
| 28 | + async def get(cls, fqdn): |
| 29 | + classes = class2filter(config.ldap.domainClasses) |
| 30 | + client = get_client() |
| 31 | + try: |
| 32 | + async with client.connect(is_async=True) as conn: |
| 33 | + dn = f"{config.ldap.domainDN},{config.ldap.userBase}" |
| 34 | + res = await conn.search(dn.format(fqdn), LDAPSearchScope.SUB, f"(|{classes})") |
| 35 | + except errors.AuthenticationError: |
| 36 | + raise HTTPException(status_code=403, detail="Failed to login") |
| 37 | + if len(res) < 1: |
| 38 | + raise HTTPException(status_code=404, detail="No such domain") |
| 39 | + if len(res) > 1: |
| 40 | + raise HTTPException(status_code=409, detail="Multiple domains found") |
| 41 | + data = res[0] |
| 42 | + domain = cls.from_entry(data) |
| 43 | + return domain |
| 44 | + |
| 45 | + @classmethod |
| 46 | + async def get_rdomain(cls, fqdn): |
| 47 | + classes = class2filter(config.ldap.domainClasses) |
| 48 | + client = get_client() |
| 49 | + try: |
| 50 | + async with client.connect(is_async=True) as conn: |
| 51 | + dn = f"{config.ldap.domainDN},{config.ldap.roleBase}" |
| 52 | + res = await conn.search(dn.format(fqdn), LDAPSearchScope.SUB, f"(|{classes})") |
| 53 | + except errors.AuthenticationError: |
| 54 | + raise HTTPException(status_code=403, detail="Failed to login") |
| 55 | + if len(res) < 1: |
| 56 | + raise HTTPException(status_code=404, detail="No such domain") |
| 57 | + if len(res) > 1: |
| 58 | + raise HTTPException(status_code=409, detail="Multiple domains found") |
| 59 | + data = res[0] |
| 60 | + domain = cls.from_entry(data) |
| 61 | + return domain |
| 62 | + |
| 63 | + @classmethod |
| 64 | + async def get_all(cls): |
| 65 | + classes = class2filter(config.ldap.domainClasses) |
| 66 | + client = get_client() |
| 67 | + try: |
| 68 | + async with client.connect(is_async=True) as conn: |
| 69 | + dn = config.ldap.userBase |
| 70 | + res = await conn.search(dn, LDAPSearchScope.SUB, f"(|{classes})") |
| 71 | + data = [] |
| 72 | + for gdata in res: |
| 73 | + data.append(cls.from_entry(gdata)) |
| 74 | + except errors.AuthenticationError: |
| 75 | + raise HTTPException(status_code=403, detail="Failed to login") |
| 76 | + return data |
| 77 | + |
| 78 | + async def save(self): |
| 79 | + data = LDAPEntry(self.dn) |
| 80 | + data["objectClass"] = config.ldap.domainClasses |
| 81 | + data["ou"] = self.ou |
| 82 | + await save_data(data) |
| 83 | + |
| 84 | + async def destroy(self): |
| 85 | + client = get_client() |
| 86 | + try: |
| 87 | + async with client.connect(is_async=True) as conn: |
| 88 | + await conn.delete(self.dn) |
| 89 | + except errors.AuthenticationError: |
| 90 | + raise HTTPException(status_code=403, detail="Failed to login") |
| 91 | + |
| 92 | + |
| 93 | +class DomainCreate(BaseModel): |
| 94 | + name: str = Field(description=("Common name")) |
0 commit comments