-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable passing custom admin username and password, enable resetting … (…
…#499) Close #484 --------- Co-authored-by: sykp241095 <[email protected]>
- Loading branch information
1 parent
4146511
commit bc20993
Showing
3 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,8 @@ | |
from app.models import User, ChatEngine | ||
|
||
|
||
async def ensure_admin_user(session: AsyncSession, email: str | None = None, password: str | None = None) -> None: | ||
async def ensure_admin_user(session: AsyncSession, email: str | None = None, | ||
password: str | None = None) -> None: | ||
result = await session.exec(select(User).where(User.is_superuser == True)) | ||
user = result.first() | ||
if not user: | ||
|
@@ -34,6 +35,25 @@ async def ensure_admin_user(session: AsyncSession, email: str | None = None, pas | |
else: | ||
print(Fore.YELLOW + "Admin user already exists, skipping...") | ||
|
||
async def reset_admin_password(session: AsyncSession, new_password: str | None = None) -> None: | ||
result = await session.exec(select(User).where(User.is_superuser == True)) | ||
user = result.first() | ||
if not user: | ||
print(Fore.YELLOW + "Admin user does not exist, skipping reset password...") | ||
else: | ||
from app.auth.users import update_user_password | ||
|
||
admin_password = new_password or secrets.token_urlsafe(16) | ||
updated_user = await update_user_password( | ||
session, | ||
user_id=user.id, | ||
new_password=admin_password, | ||
) | ||
print( | ||
Fore.GREEN + "Admin user password reset SUCCESS!\n" | ||
f"email: {updated_user.email} \n" | ||
f"password: {admin_password}" + Style.RESET_ALL | ||
) | ||
|
||
async def ensure_default_chat_engine(session: AsyncSession) -> None: | ||
result = await session.scalar(func.count(ChatEngine.id)) | ||
|
@@ -52,21 +72,23 @@ async def ensure_default_chat_engine(session: AsyncSession) -> None: | |
print(Fore.YELLOW + "Default chat engine already exists, skipping...") | ||
|
||
|
||
async def bootstrap(email: str | None = None, password: str | None = None) -> None: | ||
async def bootstrap(email: str | None = None, password: str | None = None, | ||
reset_password: bool = False) -> None: | ||
async with get_db_async_session_context() as session: | ||
await ensure_admin_user(session, email, password) | ||
await ensure_default_chat_engine(session) | ||
|
||
if reset_password: | ||
await reset_admin_password(session, password) | ||
|
||
@click.command() | ||
@click.option("--email", default=None, help="Admin user email, [email protected]") | ||
@click.option("--password", default=None, help="Admin user password, default=random generated") | ||
def main(email: str | None, password: str | None): | ||
@click.option('--reset-password', '-r', is_flag=True, help='Reset admin user password.') | ||
def main(email: str | None, password: str | None, reset_password: bool): | ||
"""Bootstrap the application with optional admin credentials.""" | ||
print(Fore.GREEN + "Bootstrapping the application..." + Style.RESET_ALL) | ||
asyncio.run(bootstrap(email, password)) | ||
asyncio.run(bootstrap(email, password, reset_password)) | ||
print(Fore.GREEN + "Bootstrapping completed." + Style.RESET_ALL) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters