refac
This commit is contained in:
@@ -19,6 +19,7 @@ from open_webui.models.auths import (
|
|||||||
UpdatePasswordForm,
|
UpdatePasswordForm,
|
||||||
)
|
)
|
||||||
from open_webui.models.users import (
|
from open_webui.models.users import (
|
||||||
|
UserModel,
|
||||||
UserProfileImageResponse,
|
UserProfileImageResponse,
|
||||||
Users,
|
Users,
|
||||||
UpdateProfileForm,
|
UpdateProfileForm,
|
||||||
@@ -603,10 +604,11 @@ async def signin(
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if not Users.get_user_by_email(email.lower(), db=db):
|
if not Users.get_user_by_email(email.lower(), db=db):
|
||||||
await signup(
|
await signup_handler(
|
||||||
request,
|
request,
|
||||||
response,
|
email,
|
||||||
SignupForm(email=email, password=str(uuid.uuid4()), name=name),
|
str(uuid.uuid4()),
|
||||||
|
name,
|
||||||
db=db,
|
db=db,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -634,10 +636,11 @@ async def signin(
|
|||||||
if Users.has_users(db=db):
|
if Users.has_users(db=db):
|
||||||
raise HTTPException(400, detail=ERROR_MESSAGES.EXISTING_USERS)
|
raise HTTPException(400, detail=ERROR_MESSAGES.EXISTING_USERS)
|
||||||
|
|
||||||
await signup(
|
await signup_handler(
|
||||||
request,
|
request,
|
||||||
response,
|
admin_email,
|
||||||
SignupForm(email=admin_email, password=admin_password, name="User"),
|
admin_password,
|
||||||
|
"User",
|
||||||
db=db,
|
db=db,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -679,6 +682,62 @@ async def signin(
|
|||||||
############################
|
############################
|
||||||
|
|
||||||
|
|
||||||
|
async def signup_handler(
|
||||||
|
request: Request,
|
||||||
|
email: str,
|
||||||
|
password: str,
|
||||||
|
name: str,
|
||||||
|
profile_image_url: str = "/user.png",
|
||||||
|
*,
|
||||||
|
db: Session,
|
||||||
|
) -> UserModel:
|
||||||
|
"""
|
||||||
|
Core user-creation logic shared by the signup endpoint and
|
||||||
|
trusted-header / no-auth auto-registration flows.
|
||||||
|
|
||||||
|
Returns the newly created UserModel.
|
||||||
|
Raises HTTPException on failure.
|
||||||
|
"""
|
||||||
|
has_users = Users.has_users(db=db)
|
||||||
|
role = "admin" if not has_users else request.app.state.config.DEFAULT_USER_ROLE
|
||||||
|
hashed = get_password_hash(password)
|
||||||
|
|
||||||
|
user = Auths.insert_new_auth(
|
||||||
|
email=email.lower(),
|
||||||
|
password=hashed,
|
||||||
|
name=name,
|
||||||
|
profile_image_url=profile_image_url,
|
||||||
|
role=role,
|
||||||
|
db=db,
|
||||||
|
)
|
||||||
|
if not user:
|
||||||
|
raise HTTPException(500, detail=ERROR_MESSAGES.CREATE_USER_ERROR)
|
||||||
|
|
||||||
|
if request.app.state.config.WEBHOOK_URL:
|
||||||
|
await post_webhook(
|
||||||
|
request.app.state.WEBUI_NAME,
|
||||||
|
request.app.state.config.WEBHOOK_URL,
|
||||||
|
WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
|
||||||
|
{
|
||||||
|
"action": "signup",
|
||||||
|
"message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
|
||||||
|
"user": user.model_dump_json(exclude_none=True),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not has_users:
|
||||||
|
# Disable signup after the first user is created
|
||||||
|
request.app.state.config.ENABLE_SIGNUP = False
|
||||||
|
|
||||||
|
apply_default_group_assignment(
|
||||||
|
request.app.state.config.DEFAULT_GROUP_ID,
|
||||||
|
user.id,
|
||||||
|
db=db,
|
||||||
|
)
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
@router.post("/signup", response_model=SessionUserResponse)
|
@router.post("/signup", response_model=SessionUserResponse)
|
||||||
async def signup(
|
async def signup(
|
||||||
request: Request,
|
request: Request,
|
||||||
@@ -717,44 +776,15 @@ async def signup(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(400, detail=str(e))
|
raise HTTPException(400, detail=str(e))
|
||||||
|
|
||||||
hashed = get_password_hash(form_data.password)
|
user = await signup_handler(
|
||||||
|
request,
|
||||||
role = "admin" if not has_users else request.app.state.config.DEFAULT_USER_ROLE
|
form_data.email,
|
||||||
user = Auths.insert_new_auth(
|
form_data.password,
|
||||||
form_data.email.lower(),
|
|
||||||
hashed,
|
|
||||||
form_data.name,
|
form_data.name,
|
||||||
form_data.profile_image_url,
|
form_data.profile_image_url,
|
||||||
role,
|
|
||||||
db=db,
|
db=db,
|
||||||
)
|
)
|
||||||
|
return create_session_response(request, user, db, response, set_cookie=True)
|
||||||
if user:
|
|
||||||
if request.app.state.config.WEBHOOK_URL:
|
|
||||||
await post_webhook(
|
|
||||||
request.app.state.WEBUI_NAME,
|
|
||||||
request.app.state.config.WEBHOOK_URL,
|
|
||||||
WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
|
|
||||||
{
|
|
||||||
"action": "signup",
|
|
||||||
"message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
|
|
||||||
"user": user.model_dump_json(exclude_none=True),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
if not has_users:
|
|
||||||
# Disable signup after the first user is created
|
|
||||||
request.app.state.config.ENABLE_SIGNUP = False
|
|
||||||
|
|
||||||
apply_default_group_assignment(
|
|
||||||
request.app.state.config.DEFAULT_GROUP_ID,
|
|
||||||
user.id,
|
|
||||||
db=db,
|
|
||||||
)
|
|
||||||
|
|
||||||
return create_session_response(request, user, db, response, set_cookie=True)
|
|
||||||
else:
|
|
||||||
raise HTTPException(500, detail=ERROR_MESSAGES.CREATE_USER_ERROR)
|
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
|||||||
Reference in New Issue
Block a user