fix: couldn't log in or resetup after a failed setup (#5739)
This commit is contained in:
@@ -3,11 +3,10 @@ from functools import wraps
|
|||||||
from flask import current_app, request
|
from flask import current_app, request
|
||||||
from flask_restful import Resource, reqparse
|
from flask_restful import Resource, reqparse
|
||||||
|
|
||||||
from extensions.ext_database import db
|
|
||||||
from libs.helper import email, get_remote_ip, str_len
|
from libs.helper import email, get_remote_ip, str_len
|
||||||
from libs.password import valid_password
|
from libs.password import valid_password
|
||||||
from models.model import DifySetup
|
from models.model import DifySetup
|
||||||
from services.account_service import AccountService, RegisterService, TenantService
|
from services.account_service import RegisterService, TenantService
|
||||||
|
|
||||||
from . import api
|
from . import api
|
||||||
from .error import AlreadySetupError, NotInitValidateError, NotSetupError
|
from .error import AlreadySetupError, NotInitValidateError, NotSetupError
|
||||||
@@ -51,28 +50,17 @@ class SetupApi(Resource):
|
|||||||
required=True, location='json')
|
required=True, location='json')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Register
|
# setup
|
||||||
account = RegisterService.register(
|
RegisterService.setup(
|
||||||
email=args['email'],
|
email=args['email'],
|
||||||
name=args['name'],
|
name=args['name'],
|
||||||
password=args['password']
|
password=args['password'],
|
||||||
|
ip_address=get_remote_ip(request)
|
||||||
)
|
)
|
||||||
|
|
||||||
TenantService.create_owner_tenant_if_not_exist(account)
|
|
||||||
|
|
||||||
setup()
|
|
||||||
AccountService.update_last_login(account, ip_address=get_remote_ip(request))
|
|
||||||
|
|
||||||
return {'result': 'success'}, 201
|
return {'result': 'success'}, 201
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
|
||||||
dify_setup = DifySetup(
|
|
||||||
version=current_app.config['CURRENT_VERSION']
|
|
||||||
)
|
|
||||||
db.session.add(dify_setup)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_required(view):
|
def setup_required(view):
|
||||||
@wraps(view)
|
@wraps(view)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
|
@@ -17,6 +17,7 @@ from libs.passport import PassportService
|
|||||||
from libs.password import compare_password, hash_password, valid_password
|
from libs.password import compare_password, hash_password, valid_password
|
||||||
from libs.rsa import generate_key_pair
|
from libs.rsa import generate_key_pair
|
||||||
from models.account import *
|
from models.account import *
|
||||||
|
from models.model import DifySetup
|
||||||
from services.errors.account import (
|
from services.errors.account import (
|
||||||
AccountAlreadyInTenantError,
|
AccountAlreadyInTenantError,
|
||||||
AccountLoginError,
|
AccountLoginError,
|
||||||
@@ -119,10 +120,11 @@ class AccountService:
|
|||||||
return account
|
return account
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_account(email: str, name: str, interface_language: str,
|
def create_account(email: str,
|
||||||
password: str = None,
|
name: str,
|
||||||
interface_theme: str = 'light',
|
interface_language: str,
|
||||||
timezone: str = 'America/New_York', ) -> Account:
|
password: Optional[str] = None,
|
||||||
|
interface_theme: str = 'light') -> Account:
|
||||||
"""create account"""
|
"""create account"""
|
||||||
account = Account()
|
account = Account()
|
||||||
account.email = email
|
account.email = email
|
||||||
@@ -200,7 +202,6 @@ class AccountService:
|
|||||||
account.last_login_ip = ip_address
|
account.last_login_ip = ip_address
|
||||||
db.session.add(account)
|
db.session.add(account)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
logging.info(f'Account {account.id} logged in successfully.')
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def login(account: Account, *, ip_address: Optional[str] = None):
|
def login(account: Account, *, ip_address: Optional[str] = None):
|
||||||
@@ -444,8 +445,51 @@ class RegisterService:
|
|||||||
return f'member_invite:token:{token}'
|
return f'member_invite:token:{token}'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def register(cls, email, name, password: str = None, open_id: str = None, provider: str = None,
|
def setup(cls, email: str, name: str, password: str, ip_address: str) -> None:
|
||||||
language: str = None, status: AccountStatus = None) -> Account:
|
"""
|
||||||
|
Setup dify
|
||||||
|
|
||||||
|
:param email: email
|
||||||
|
:param name: username
|
||||||
|
:param password: password
|
||||||
|
:param ip_address: ip address
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Register
|
||||||
|
account = AccountService.create_account(
|
||||||
|
email=email,
|
||||||
|
name=name,
|
||||||
|
interface_language=languages[0],
|
||||||
|
password=password,
|
||||||
|
)
|
||||||
|
|
||||||
|
account.last_login_ip = ip_address
|
||||||
|
account.initialized_at = datetime.now(timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
|
TenantService.create_owner_tenant_if_not_exist(account)
|
||||||
|
|
||||||
|
dify_setup = DifySetup(
|
||||||
|
version=current_app.config['CURRENT_VERSION']
|
||||||
|
)
|
||||||
|
db.session.add(dify_setup)
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
db.session.query(DifySetup).delete()
|
||||||
|
db.session.query(TenantAccountJoin).delete()
|
||||||
|
db.session.query(Account).delete()
|
||||||
|
db.session.query(Tenant).delete()
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
logging.exception(f'Setup failed: {e}')
|
||||||
|
raise ValueError(f'Setup failed: {e}')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def register(cls, email, name,
|
||||||
|
password: Optional[str] = None,
|
||||||
|
open_id: Optional[str] = None,
|
||||||
|
provider: Optional[str] = None,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
status: Optional[AccountStatus] = None) -> Account:
|
||||||
db.session.begin_nested()
|
db.session.begin_nested()
|
||||||
"""Register account"""
|
"""Register account"""
|
||||||
try:
|
try:
|
||||||
|
Reference in New Issue
Block a user