Пример #1
0
async def update(
    user_id: int,
    form: UserEditForm,
    user_repo: UserRepo = Depends(UserRepo()),
    token: TokenPayload = Depends(
        ScopedTo("user:update", "super", satisfy="one")),
):

    email = getattr(form, "email", None)
    if email:
        for u in user_repo.new().all().results():
            print("USER:"******"Invalid Email Address",
                            code=422,
                            field="email")

    data = only(form, ["email", "is_active", "is_superuser"])

    if getattr(form, "password", None):
        data["hashed_password"] = hash_password(form.password)

    item = user_repo.new().get_or_fail(user_id).update(data).data()
    return UserResponse(user=item)
Пример #2
0
class UserFactory(BaseFactory):
    class Meta:
        model = User

    email = Faker("email")
    hashed_password = hash_password("password123")
    email_verified = True
    mfa_secret = ""
    is_active = True
    is_superuser = False
Пример #3
0
    async def run(self):
        self.db_register()
        email = self.option("email").lower()
        password = self.get_password()
        hashed_password = hash_password(password)
        is_superuser = bool(self.option("superuser"))
        user = User(email=email,
                    hashed_password=hashed_password,
                    is_superuser=is_superuser)

        self.session().add(user)
        self.session().commit()
Пример #4
0
async def store(
    form: UserCreateForm,
    user_repo: UserRepo = Depends(UserRepo()),
    token: TokenPayload = Depends(
        ScopedTo("user:create", "super", satisfy="one")),
):

    if user_repo.exists(email=form.email):
        abort_for_input("email", "Email has already been taken.")

    user_repo.clear()

    # TODO: data validation against current db & perm checks
    data = {
        "email": form.email,
        "hashed_password": hash_password(form.password),
        "is_active": getattr(form, "is_superuser", True),
        "is_superuser": getattr(form, "is_superuser", False),
    }
    item = user_repo.create(data).data()
    return UserResponse(user=item)
Пример #5
0
    def seed_from_env(self):
        from boucanpy.core.user import UserRepo
        from boucanpy.core.zone import ZoneRepo
        from boucanpy.core.dns_server import DnsServerRepo
        from boucanpy.db.session import _scoped_session

        session = _scoped_session

        for i in range(9):
            i = str(i)
            user_data = {}
            email_key = f"SEED_USER_{i}_EMAIL"
            email = environ.get(email_key, None)
            password_key = f"SEED_USER_{i}_PASSWORD"
            password = environ.get(password_key, None)
            superuser_key = f"SEED_USER_{i}_SUPERUSER"
            is_superuser = int(environ.get(superuser_key, 0))
            if email and password:
                email = email.lower()
                hashed_password = hash_password(password)
                repo = UserRepo(db=session)
                if not repo.exists(email=email):
                    logger.info(
                        f"seed_from_env@api_server.py - seeding user {email}")
                    user = factory("UserFactory", session=session).create(
                        email=email,
                        hashed_password=hashed_password,
                        is_superuser=is_superuser,
                    )
                else:
                    logger.info(
                        f"seed_from_env@api_server.py - Seeded user {email} already exists"
                    )

        for i in range(9):
            i = str(i)
            name_key = f"SEED_DNS_SERVER_{i}_NAME"
            name = environ.get(name_key, None)
            if name:
                repo = DnsServerRepo(db=session)
                if not repo.exists(name=name):
                    logger.info(
                        f"seed_from_env@api_server.py - Seeding domain {name}")
                    domain = factory("DnsServerFactory",
                                     session=session).create(name=name)

        for i in range(9):
            i = str(i)
            ip_key = f"SEED_ZONE_{i}_IP"
            domain_key = f"SEED_ZONE_{i}_DOMAIN"
            dns_server_name_key = f"SEED_ZONE_{i}_DNS_SERVER_NAME"
            ip = environ.get(ip_key, None)
            domain = environ.get(domain_key, None)
            if domain:
                domain = domain.lower()
            dns_server_name = environ.get(dns_server_name_key, None)
            if ip and domain:
                if dns_server_name:
                    dns_server_repo = DnsServerRepo(db=session)
                    if dns_server_repo.exists(name=dns_server_name):
                        dns_server = dns_server_repo.results()
                    else:
                        logger.info(
                            f"seed_from_env@api_server.py - Seeding dns server as zone dependency: {name}"
                        )
                        dns_server = factory(
                            "DnsServerFactory",
                            session=session).create(name=dns_server_name)
                    factory("ZoneFactory",
                            session=session).create(ip=ip,
                                                    domain=domain,
                                                    dns_server=dns_server)
                else:
                    repo = ZoneRepo(db=session)
                    if not repo.exists(ip=ip, domain=domain):
                        logger.info(
                            f"seed_from_env@api_server.py - Seeding zone without dns server: {ip}, {domain}"
                        )
                        factory("GlobalZoneFactory",
                                session=session).create(ip=ip, domain=domain)