async def destroy( user_id: int, user_repo: UserRepo = Depends(UserRepo()), token: TokenPayload = Depends( ScopedTo("user:create", "super", satisfy="one")), user: User = Depends(current_user), ): if user_id == user.id: abort(403, msg="Cannot deactivate self") remaining_supers = len( user_repo.filter( user_repo.label("is_superuser") == True, user_repo.label("is_active") == True, user_repo.label("id") != user_id, ).all().results()) if remaining_supers < 1: abort( 403, msg="Cannot deactivate user. No other active super users available." ) user_repo.clear() messages = [{"text": "Deactivation Succesful", "type": "success"}] if not user_repo.exists(id=user_id): return BaseResponse(messages=messages) user_repo.deactivate(user_id) return BaseResponse(messages=messages)
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)
def seed_from_env(self): from bountydns.core.user import UserRepo from bountydns.core.zone import ZoneRepo from bountydns.core.dns_server import DnsServerRepo from bountydns.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"seeding user {email}") user = factory("UserFactory", session=session).create( email=email, hashed_password=hashed_password, is_superuser=is_superuser, ) else: logger.info(f"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"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"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"seeding zone without dns server: {ip}, {domain}") factory("GlobalZoneFactory", session=session).create(ip=ip, domain=domain)