feat: set first user to admin by default

This commit is contained in:
Timothy J. Baek 2023-11-19 00:41:29 -08:00
parent 07d2c9871f
commit 83ff1d77ea
8 changed files with 28 additions and 25 deletions

View file

@ -9,7 +9,7 @@ import json
from apps.web.models.users import Users
from constants import ERROR_MESSAGES
from utils.utils import extract_token_from_auth_header
from config import OLLAMA_API_BASE_URL, OLLAMA_WEBUI_AUTH
from config import OLLAMA_API_BASE_URL, WEBUI_AUTH
app = Flask(__name__)
CORS(
@ -32,7 +32,7 @@ def proxy(path):
headers = dict(request.headers)
# Basic RBAC support
if OLLAMA_WEBUI_AUTH:
if WEBUI_AUTH:
if "Authorization" in headers:
token = extract_token_from_auth_header(headers["Authorization"])
user = Users.get_user_by_token(token)

View file

@ -2,7 +2,7 @@ from fastapi import FastAPI, Request, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from apps.web.routers import auths, users
from config import OLLAMA_WEBUI_VERSION, OLLAMA_WEBUI_AUTH
from config import WEBUI_VERSION, WEBUI_AUTH
app = FastAPI()
@ -23,4 +23,4 @@ app.include_router(users.router, prefix="/users", tags=["users"])
@app.get("/")
async def get_status():
return {"status": True, "version": OLLAMA_WEBUI_VERSION, "auth": OLLAMA_WEBUI_AUTH}
return {"status": True, "version": WEBUI_VERSION, "auth": WEBUI_AUTH}

View file

@ -81,6 +81,9 @@ class UsersTable:
)
]
def get_num_users(self) -> Optional[int]:
return self.table.count_documents({})
def update_user_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
user = self.table.find_one_and_update(
{"id": id}, {"$set": updated}, return_document=ReturnDocument.AFTER

View file

@ -86,8 +86,9 @@ async def signin(form_data: SigninForm):
async def signup(form_data: SignupForm):
if not Users.get_user_by_email(form_data.email.lower()):
try:
role = "admin" if Users.get_num_users() == 0 else "pending"
hashed = get_password_hash(form_data.password)
user = Auths.insert_new_auth(form_data.email, hashed, form_data.name)
user = Auths.insert_new_auth(form_data.email, hashed, form_data.name, role)
if user:
token = create_token(data={"email": user.email})

View file

@ -26,40 +26,36 @@ if ENV == "prod":
OLLAMA_API_BASE_URL = "http://host.docker.internal:11434/api"
####################################
# OLLAMA_WEBUI_VERSION
# WEBUI_VERSION
####################################
OLLAMA_WEBUI_VERSION = os.environ.get("OLLAMA_WEBUI_VERSION", "v1.0.0-alpha.9")
WEBUI_VERSION = os.environ.get("WEBUI_VERSION", "v1.0.0-alpha.9")
####################################
# OLLAMA_WEBUI_AUTH
# WEBUI_AUTH
####################################
OLLAMA_WEBUI_AUTH = (
True if os.environ.get("OLLAMA_WEBUI_AUTH", "TRUE") == "TRUE" else False
)
WEBUI_AUTH = True if os.environ.get("WEBUI_AUTH", "TRUE") == "TRUE" else False
if OLLAMA_WEBUI_AUTH:
if WEBUI_AUTH:
####################################
# OLLAMA_WEBUI_DB
# WEBUI_DB
####################################
OLLAMA_WEBUI_DB_URL = os.environ.get(
"OLLAMA_WEBUI_DB_URL", "mongodb://root:root@localhost:27017/"
WEBUI_DB_URL = os.environ.get(
"WEBUI_DB_URL", "mongodb://root:root@localhost:27017/"
)
DB_CLIENT = MongoClient(f"{OLLAMA_WEBUI_DB_URL}?authSource=admin")
DB_CLIENT = MongoClient(f"{WEBUI_DB_URL}?authSource=admin")
DB = DB_CLIENT["ollama-webui"]
####################################
# OLLAMA_WEBUI_JWT_SECRET_KEY
# WEBUI_JWT_SECRET_KEY
####################################
OLLAMA_WEBUI_JWT_SECRET_KEY = os.environ.get(
"OLLAMA_WEBUI_JWT_SECRET_KEY", "t0p-s3cr3t"
)
WEBUI_JWT_SECRET_KEY = os.environ.get("WEBUI_JWT_SECRET_KEY", "t0p-s3cr3t")
if ENV == "prod":
if OLLAMA_WEBUI_JWT_SECRET_KEY == "":
OLLAMA_WEBUI_JWT_SECRET_KEY = str(b64encode(token_bytes(32)).decode())
if WEBUI_JWT_SECRET_KEY == "":
WEBUI_JWT_SECRET_KEY = str(b64encode(token_bytes(32)).decode())

View file

@ -9,7 +9,7 @@ import jwt
import config
JWT_SECRET_KEY = config.OLLAMA_WEBUI_JWT_SECRET_KEY
JWT_SECRET_KEY = config.WEBUI_JWT_SECRET_KEY
ALGORITHM = "HS256"
##############

View file

@ -46,7 +46,8 @@ services:
- 3000:8080
environment:
- "OLLAMA_API_BASE_URL=http://ollama:11434/api"
- "OLLAMA_WEBUI_DB_URL=mongodb://root:example@ollama-webui-db:27017/"
- "WEBUI_DB_URL=mongodb://root:example@ollama-webui-db:27017/"
- "WEBUI_AUTH=TRUE"
extra_hosts:
- host.docker.internal:host-gateway
restart: unless-stopped

View file

@ -3,8 +3,10 @@ import { PUBLIC_API_BASE_URL } from '$env/static/public';
export const OLLAMA_API_BASE_URL =
PUBLIC_API_BASE_URL === ''
? browser
? dev
? `http://${location.hostname}:8080/ollama/api`
: browser
? `http://${location.hostname}:11434/api`
: `http://localhost:11434/api`
: PUBLIC_API_BASE_URL;