Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat/new-login' into deploy/dev
Browse files Browse the repository at this point in the history
* refs/heads/feat/new-login:
  feat: update invite workspace service
  • Loading branch information
ZhouhaoJiang committed Sep 2, 2024
2 parents c4efadc + 8a014bd commit 185b661
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
16 changes: 13 additions & 3 deletions api/controllers/console/auth/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from extensions.ext_database import db
from libs.helper import email, str_len, timezone
from libs.password import hash_password, valid_password
from models.account import AccountStatus
from models.account import AccountStatus, Tenant
from services.account_service import RegisterService


Expand All @@ -27,8 +27,18 @@ def get(self):
token = args["token"]

invitation = RegisterService.get_invitation_if_token_valid(workspaceId, reg_email, token)

return {"is_valid": invitation is not None, "workspace_name": invitation["tenant"].name if invitation else None}
if invitation:
data = invitation.get("data", {})
tenant: Tenant = invitation.get("tenant")
workspace_name = tenant.name if tenant else "Unknown Workspace"
workspace_id = tenant.id if tenant else "Unknown Workspace ID"
invitee_email = data.get("email", "Unknown Email")
return {
"is_valid": invitation is not None,
"data": {"workspace_name": workspace_name, "workspace_id": workspace_id, "email": invitee_email}
}
else:
return {"is_valid": False}


class ActivateApi(Resource):
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/console/auth/forgot_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def post(self):
account.password_salt = base64_salt
db.session.commit()
else:
account = AccountService.create_user_through_env(
account = AccountService.create_account_and_tenant(
email=reset_data.get("email"),
name=reset_data.get("email"),
password=password_confirm,
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/console/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def post(self):
AccountService.revoke_email_code_login_token(args["token"])
account = AccountService.get_user_through_email(user_email)
if account is None:
account = AccountService.create_user_through_env(
account = AccountService.create_account_and_tenant(
email=user_email, name=user_email, interface_language=languages[0]
)

Expand Down
20 changes: 9 additions & 11 deletions api/services/account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def create_account(
email: str, name: str, interface_language: str, password: Optional[str] = None, interface_theme: str = "light"
) -> Account:
"""create account"""
if not dify_config.ALLOW_REGISTER:
raise Unauthorized("Register is not allowed.")
account = Account()
account.email = email
account.name = name
Expand Down Expand Up @@ -160,20 +162,14 @@ def create_account(
return account

@staticmethod
def create_user_through_env(
def create_account_and_tenant(
email: str, name: str, interface_language: str, password: Optional[str] = None
) -> Account:
"""create account"""
if dify_config.ALLOW_REGISTER:
account = AccountService.create_account(
email=email, name=name, interface_language=interface_language, password=password
)
else:
raise Unauthorized("Register is not allowed.")
if dify_config.ALLOW_CREATE_WORKSPACE:
TenantService.create_owner_tenant_if_not_exist(account=account)
else:
raise Unauthorized("Create workspace is not allowed.")
account = AccountService.create_account(
email=email, name=name, interface_language=interface_language, password=password
)
TenantService.create_owner_tenant_if_not_exist(account=account)

return account

Expand Down Expand Up @@ -319,6 +315,8 @@ class TenantService:
@staticmethod
def create_tenant(name: str) -> Tenant:
"""Create tenant"""
if not dify_config.ALLOW_CREATE_WORKSPACE:
raise Unauthorized("Create workspace is not allowed.")
tenant = Tenant(name=name)

db.session.add(tenant)
Expand Down

0 comments on commit 185b661

Please sign in to comment.