Added create user function.
This commit is contained in:
@@ -98,14 +98,21 @@ exec_in_container() {
|
||||
# Returns 0 if the command was successful
|
||||
local CONTAINER_NAME=$CONTAINER_NAME
|
||||
local COMMAND="$1"
|
||||
|
||||
# Check if the container is running
|
||||
if ! docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
|
||||
print_header "Error: Container '$CONTAINER_NAME' is not running."
|
||||
print_header "Error: Container '$CONTAINER_NAME' is not running.
|
||||
|
||||
We can't execute commands inside it, you can run it with:
|
||||
|
||||
>>> $0 start"
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Execute the command in the container
|
||||
OUTPUT=$(docker exec "$CONTAINER_NAME" bash -c "$COMMAND" 2>&1)
|
||||
|
||||
local EXIT_CODE=$?
|
||||
|
||||
# Check if the command succeeded
|
||||
@@ -159,6 +166,19 @@ set_up_scripts() {
|
||||
return 0
|
||||
}
|
||||
|
||||
create_user(){
|
||||
read -p "Enter username: " USERNAME
|
||||
read -p "Enter role (admin/user): " ROLE_INPUT
|
||||
local COMMAND="python3 -c 'from utils.scripts import create_user_script_sync;create_user_script_sync(username=\"$USERNAME\", role_input=\"$ROLE_INPUT\")'"
|
||||
exec_in_container "$COMMAND"
|
||||
local RETURN_CODE=$?
|
||||
|
||||
if [[ $RETURN_CODE -eq 0 ]]; then
|
||||
print_header $EXECUTION_MESSAGE
|
||||
else
|
||||
print_header "Couldn't create the user due to the above error."
|
||||
fi
|
||||
}
|
||||
|
||||
update_config() {
|
||||
print_header "Update configs."
|
||||
@@ -659,23 +679,26 @@ help() {
|
||||
print_header "help:
|
||||
|
||||
Source Code Management:
|
||||
install
|
||||
update_code
|
||||
upgrade
|
||||
rebuild
|
||||
> install
|
||||
> update_code
|
||||
> upgrade
|
||||
> rebuild
|
||||
|
||||
App Running:
|
||||
start
|
||||
restart
|
||||
status
|
||||
stop
|
||||
> status
|
||||
> start
|
||||
> restart
|
||||
> stop
|
||||
|
||||
Users Management:
|
||||
> create_user
|
||||
|
||||
Configurations:
|
||||
show_config
|
||||
update_config
|
||||
> show_config
|
||||
> update_config
|
||||
|
||||
Help:
|
||||
help
|
||||
> help
|
||||
"
|
||||
exit 1
|
||||
}
|
||||
@@ -695,6 +718,7 @@ main() {
|
||||
case "$1" in
|
||||
install) install ;;
|
||||
update_code) update_code ;;
|
||||
create_user) create_user ;;
|
||||
upgrade) upgrade ;;
|
||||
rebuild) set_up_middleware;;
|
||||
start) start ;;
|
||||
@@ -702,8 +726,8 @@ main() {
|
||||
status) status ;;
|
||||
stop) stop ;;
|
||||
show_config) show_config ;;
|
||||
update_config) update_config;;
|
||||
help) help;;
|
||||
update_config) update_config ;;
|
||||
help) help ;;
|
||||
*) echo "Invalid argument: $1"; help ;;
|
||||
esac
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# add_user.py
|
||||
|
||||
import asyncio, logging, pymysql
|
||||
import asyncio, logging, pymysql, re
|
||||
import secrets
|
||||
from sqlalchemy.future import select
|
||||
from data.db import engine, SessionLocal
|
||||
@@ -72,17 +72,23 @@ async def db_startup():
|
||||
def create_secret():
|
||||
return secrets.token_hex(32)
|
||||
|
||||
async def create_user_script_async():
|
||||
async def create_user_script_async(username:str|None=None, role_input:str|None=None):
|
||||
|
||||
username = username.strip() if username else input("Enter username: ").strip()
|
||||
if not username.isalnum():
|
||||
print("> Invalid username. Please use alphanumerical characters only.")
|
||||
return
|
||||
if not re.match("[a-zA-Z]{1}[a-zA-Z0-0-9]{,15}", username):
|
||||
print("> Invalid username. Please use english characters and numbers only.")
|
||||
return
|
||||
role_input = role_input.strip().lower() if role_input else input("Enter role (admin/user): ").strip().lower()
|
||||
print('\n')
|
||||
if role_input not in UserRole._value2member_map_:
|
||||
print("> Invalid role. Please enter 'admin' or 'user'.")
|
||||
return
|
||||
|
||||
role = UserRole(role_input)
|
||||
async with SessionLocal() as session:
|
||||
username = input("Enter username: ").strip()
|
||||
role_input = input("Enter role (admin/user): ").strip().lower()
|
||||
print('\n')
|
||||
if role_input not in UserRole._value2member_map_:
|
||||
print("> Invalid role. Please enter 'admin' or 'user'.")
|
||||
return
|
||||
|
||||
role = UserRole(role_input)
|
||||
|
||||
# Check if username already exists
|
||||
result = await session.execute(select(User).filter_by(username=username))
|
||||
@@ -102,5 +108,5 @@ async def create_user_script_async():
|
||||
print(f"> User '{username}' with role '{role.value}' created successfully.")
|
||||
print(f"> API Key: {api_key}")
|
||||
|
||||
def create_user_script_sync():
|
||||
asyncio.run(create_user_script_async())
|
||||
def create_user_script_sync(username:str|None=None, role_input:str|None=None):
|
||||
asyncio.run(create_user_script_async(username=username, role_input=role_input))
|
||||
|
||||
Reference in New Issue
Block a user