feat: update invite workspace service
This commit is contained in:
parent
955e2871f4
commit
8a014bdda4
@ -10,7 +10,7 @@ from controllers.console.error import AlreadyActivateError
|
||||
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
|
||||
|
||||
|
||||
@ -27,8 +27,18 @@ class ActivateCheckApi(Resource):
|
||||
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):
|
||||
|
@ -107,7 +107,7 @@ class ForgotPasswordResetApi(Resource):
|
||||
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,
|
||||
|
@ -125,7 +125,7 @@ class EmailCodeLoginApi(Resource):
|
||||
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]
|
||||
)
|
||||
|
||||
|
@ -133,6 +133,8 @@ class AccountService:
|
||||
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
|
||||
@ -160,20 +162,14 @@ class AccountService:
|
||||
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
|
||||
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user