Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: couldn't log in or resetup after a failed setup #5739

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions api/controllers/console/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from flask import current_app, request
from flask_restful import Resource, reqparse

from extensions.ext_database import db
from libs.helper import email, get_remote_ip, str_len
from libs.password import valid_password
from models.model import DifySetup
from services.account_service import AccountService, RegisterService, TenantService
from services.account_service import RegisterService, TenantService

from . import api
from .error import AlreadySetupError, NotInitValidateError, NotSetupError
Expand Down Expand Up @@ -51,28 +50,17 @@ def post(self):
required=True, location='json')
args = parser.parse_args()

# Register
account = RegisterService.register(
# setup
RegisterService.setup(
email=args['email'],
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


def setup():
dify_setup = DifySetup(
version=current_app.config['CURRENT_VERSION']
)
db.session.add(dify_setup)


def setup_required(view):
@wraps(view)
def decorated(*args, **kwargs):
Expand Down
58 changes: 51 additions & 7 deletions api/services/account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from libs.password import compare_password, hash_password, valid_password
from libs.rsa import generate_key_pair
from models.account import *
from models.model import DifySetup
from services.errors.account import (
AccountAlreadyInTenantError,
AccountLoginError,
Expand Down Expand Up @@ -119,10 +120,11 @@ def update_account_password(account, password, new_password):
return account

@staticmethod
def create_account(email: str, name: str, interface_language: str,
password: str = None,
interface_theme: str = 'light',
timezone: str = 'America/New_York', ) -> Account:
def create_account(email: str,
name: str,
interface_language: str,
password: Optional[str] = None,
interface_theme: str = 'light') -> Account:
"""create account"""
account = Account()
account.email = email
Expand Down Expand Up @@ -200,7 +202,6 @@ def update_last_login(account: Account, *, ip_address: str) -> None:
account.last_login_ip = ip_address
db.session.add(account)
db.session.commit()
logging.info(f'Account {account.id} logged in successfully.')

@staticmethod
def login(account: Account, *, ip_address: Optional[str] = None):
Expand Down Expand Up @@ -444,8 +445,51 @@ def _get_invitation_token_key(cls, token: str) -> str:
return f'member_invite:token:{token}'

@classmethod
def register(cls, email, name, password: str = None, open_id: str = None, provider: str = None,
language: str = None, status: AccountStatus = None) -> Account:
def setup(cls, email: str, name: str, password: str, ip_address: str) -> None:
"""
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()
"""Register account"""
try:
Expand Down