|
| 1 | +#!/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2023-2024 MTS PJSC |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import argparse |
| 8 | +import asyncio |
| 9 | +import logging |
| 10 | + |
| 11 | +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine |
| 12 | +from sqlalchemy.future import select |
| 13 | + |
| 14 | +from syncmaster.backend.middlewares import setup_logging |
| 15 | +from syncmaster.backend.settings import BackendSettings as Settings |
| 16 | +from syncmaster.db.models.user import User |
| 17 | + |
| 18 | + |
| 19 | +async def add_superusers(session: AsyncSession, usernames: list[str]) -> None: |
| 20 | + logging.info("Adding superusers:") |
| 21 | + result = await session.execute(select(User).where(User.username.in_(usernames)).order_by(User.username)) |
| 22 | + users = result.scalars().all() |
| 23 | + |
| 24 | + not_found = set(usernames) |
| 25 | + for user in users: |
| 26 | + user.is_superuser = True |
| 27 | + logging.info(" %r", user.username) |
| 28 | + not_found.discard(user.username) |
| 29 | + |
| 30 | + if not_found: |
| 31 | + for username in not_found: |
| 32 | + session.add(User(username=username, email=f"{username}@mts.ru", is_active=True, is_superuser=True)) |
| 33 | + logging.info(" %r (new user)", username) |
| 34 | + |
| 35 | + await session.commit() |
| 36 | + logging.info("Done.") |
| 37 | + |
| 38 | + |
| 39 | +async def remove_superusers(session: AsyncSession, usernames: list[str]) -> None: |
| 40 | + logging.info("Removing superusers:") |
| 41 | + result = await session.execute(select(User).where(User.username.in_(usernames)).order_by(User.username)) |
| 42 | + users = result.scalars().all() |
| 43 | + |
| 44 | + not_found = set(usernames) |
| 45 | + for user in users: |
| 46 | + logging.info(" %r", user.username) |
| 47 | + user.is_superuser = False |
| 48 | + not_found.discard(user.username) |
| 49 | + |
| 50 | + if not_found: |
| 51 | + logging.info("Not found:") |
| 52 | + for username in not_found: |
| 53 | + logging.info(" %r", username) |
| 54 | + |
| 55 | + await session.commit() |
| 56 | + logging.info("Done.") |
| 57 | + |
| 58 | + |
| 59 | +async def list_superusers(session: AsyncSession) -> None: |
| 60 | + result = await session.execute(select(User).filter_by(is_superuser=True).order_by(User.username)) |
| 61 | + superusers = result.scalars().all() |
| 62 | + logging.info("Listing users with SUPERUSER role:") |
| 63 | + for superuser in superusers: |
| 64 | + logging.info(" %r", superuser.username) |
| 65 | + logging.info("Done.") |
| 66 | + |
| 67 | + |
| 68 | +def create_parser() -> argparse.ArgumentParser: |
| 69 | + parser = argparse.ArgumentParser(description="Manage superusers.") |
| 70 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 71 | + |
| 72 | + parser_add = subparsers.add_parser("add", help="Add superuser privileges to users") |
| 73 | + parser_add.add_argument("usernames", nargs="+", help="Usernames to add as superusers") |
| 74 | + parser_add.set_defaults(func=add_superusers) |
| 75 | + |
| 76 | + parser_remove = subparsers.add_parser("remove", help="Remove superuser privileges from users") |
| 77 | + parser_remove.add_argument("usernames", nargs="+", help="Usernames to remove from superusers") |
| 78 | + parser_remove.set_defaults(func=remove_superusers) |
| 79 | + |
| 80 | + parser_list = subparsers.add_parser("list", help="List all superusers") |
| 81 | + parser_list.set_defaults(func=list_superusers) |
| 82 | + |
| 83 | + return parser |
| 84 | + |
| 85 | + |
| 86 | +async def main(args: argparse.Namespace, session: AsyncSession) -> None: |
| 87 | + async with session: |
| 88 | + if args.command == "list": |
| 89 | + # 'list' command does not take additional arguments |
| 90 | + await args.func(session) |
| 91 | + else: |
| 92 | + await args.func(session, args.usernames) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + settings = Settings() |
| 97 | + if settings.logging.setup: |
| 98 | + setup_logging(settings.logging.get_log_config_path()) |
| 99 | + |
| 100 | + engine = create_async_engine(settings.database.url) |
| 101 | + SessionLocal = async_sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession) |
| 102 | + parser = create_parser() |
| 103 | + args = parser.parse_args() |
| 104 | + session = SessionLocal() |
| 105 | + asyncio.run(main(args, session)) |
0 commit comments