| 
									
										
										
										
											2024-04-12 16:22:24 +08:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import pytz | 
					
						
							| 
									
										
										
										
											2024-07-21 02:11:40 +09:00
										 |  |  | from flask import request | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, fields, marshal_with, reqparse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-21 02:11:40 +09:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from constants.languages import supported_language | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console import api | 
					
						
							|  |  |  | from controllers.console.setup import setup_required | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.console.workspace.error import ( | 
					
						
							|  |  |  |     AccountAlreadyInitedError, | 
					
						
							|  |  |  |     CurrentPasswordIncorrectError, | 
					
						
							|  |  |  |     InvalidInvitationCodeError, | 
					
						
							|  |  |  |     RepeatPasswordNotMatchError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console.wraps import account_initialization_required | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from fields.member_fields import account_fields | 
					
						
							| 
									
										
										
										
											2024-01-23 21:14:53 +08:00
										 |  |  | from libs.helper import TimestampField, timezone | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from libs.login import login_required | 
					
						
							|  |  |  | from models.account import AccountIntegrate, InvitationCode | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from services.account_service import AccountService | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from services.errors.account import CurrentPasswordIncorrectError as ServiceCurrentPasswordIncorrectError | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountInitApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if account.status == "active": | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise AccountAlreadyInitedError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if dify_config.EDITION == "CLOUD": | 
					
						
							|  |  |  |             parser.add_argument("invitation_code", type=str, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("interface_language", type=supported_language, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("timezone", type=timezone, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if dify_config.EDITION == "CLOUD": | 
					
						
							|  |  |  |             if not args["invitation_code"]: | 
					
						
							|  |  |  |                 raise ValueError("invitation_code is required") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # check invitation code | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             invitation_code = ( | 
					
						
							|  |  |  |                 db.session.query(InvitationCode) | 
					
						
							|  |  |  |                 .filter( | 
					
						
							|  |  |  |                     InvitationCode.code == args["invitation_code"], | 
					
						
							|  |  |  |                     InvitationCode.status == "unused", | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 .first() | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not invitation_code: | 
					
						
							|  |  |  |                 raise InvalidInvitationCodeError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             invitation_code.status = "used" | 
					
						
							| 
									
										
										
										
											2024-04-12 16:22:24 +08:00
										 |  |  |             invitation_code.used_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             invitation_code.used_by_tenant_id = account.current_tenant_id | 
					
						
							|  |  |  |             invitation_code.used_by_account_id = account.id | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         account.interface_language = args["interface_language"] | 
					
						
							|  |  |  |         account.timezone = args["timezone"] | 
					
						
							|  |  |  |         account.interface_theme = "light" | 
					
						
							|  |  |  |         account.status = "active" | 
					
						
							| 
									
										
										
										
											2024-04-12 16:22:24 +08:00
										 |  |  |         account.initialized_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"result": "success"} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountProfileApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def get(self): | 
					
						
							|  |  |  |         return current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountNameApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("name", type=str, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Validate account name length | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if len(args["name"]) < 3 or len(args["name"]) > 30: | 
					
						
							|  |  |  |             raise ValueError("Account name must be between 3 and 30 characters.") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         updated_account = AccountService.update_account(current_user, name=args["name"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return updated_account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountAvatarApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("avatar", type=str, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         updated_account = AccountService.update_account(current_user, avatar=args["avatar"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return updated_account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountInterfaceLanguageApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("interface_language", type=supported_language, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         updated_account = AccountService.update_account(current_user, interface_language=args["interface_language"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return updated_account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountInterfaceThemeApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("interface_theme", type=str, choices=["light", "dark"], required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         updated_account = AccountService.update_account(current_user, interface_theme=args["interface_theme"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return updated_account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountTimezoneApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("timezone", type=str, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Validate timezone string, e.g. America/New_York, Asia/Shanghai | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["timezone"] not in pytz.all_timezones: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise ValueError("Invalid timezone string.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         updated_account = AccountService.update_account(current_user, timezone=args["timezone"]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return updated_account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountPasswordApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(account_fields) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         parser.add_argument("password", type=str, required=False, location="json") | 
					
						
							|  |  |  |         parser.add_argument("new_password", type=str, required=True, location="json") | 
					
						
							|  |  |  |         parser.add_argument("repeat_new_password", type=str, required=True, location="json") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["new_password"] != args["repeat_new_password"]: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise RepeatPasswordNotMatchError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             AccountService.update_account_password(current_user, args["password"], args["new_password"]) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |         except ServiceCurrentPasswordIncorrectError: | 
					
						
							|  |  |  |             raise CurrentPasswordIncorrectError() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return {"result": "success"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AccountIntegrateApi(Resource): | 
					
						
							|  |  |  |     integrate_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "provider": fields.String, | 
					
						
							|  |  |  |         "created_at": TimestampField, | 
					
						
							|  |  |  |         "is_bound": fields.Boolean, | 
					
						
							|  |  |  |         "link": fields.String, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     integrate_list_fields = { | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         "data": fields.List(fields.Nested(integrate_fields)), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(integrate_list_fields) | 
					
						
							|  |  |  |     def get(self): | 
					
						
							|  |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         account_integrates = db.session.query(AccountIntegrate).filter(AccountIntegrate.account_id == account.id).all() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         base_url = request.url_root.rstrip("/") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         oauth_base_path = "/console/api/oauth/login" | 
					
						
							|  |  |  |         providers = ["github", "google"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         integrate_data = [] | 
					
						
							|  |  |  |         for provider in providers: | 
					
						
							|  |  |  |             existing_integrate = next((ai for ai in account_integrates if ai.provider == provider), None) | 
					
						
							|  |  |  |             if existing_integrate: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 integrate_data.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "id": existing_integrate.id, | 
					
						
							|  |  |  |                         "provider": provider, | 
					
						
							|  |  |  |                         "created_at": existing_integrate.created_at, | 
					
						
							|  |  |  |                         "is_bound": True, | 
					
						
							|  |  |  |                         "link": None, | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 integrate_data.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "id": None, | 
					
						
							|  |  |  |                         "provider": provider, | 
					
						
							|  |  |  |                         "created_at": None, | 
					
						
							|  |  |  |                         "is_bound": False, | 
					
						
							|  |  |  |                         "link": f"{base_url}{oauth_base_path}/{provider}", | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"data": integrate_data} | 
					
						
							| 
									
										
										
										
											2024-07-05 13:38:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | # Register API resources | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(AccountInitApi, "/account/init") | 
					
						
							|  |  |  | api.add_resource(AccountProfileApi, "/account/profile") | 
					
						
							|  |  |  | api.add_resource(AccountNameApi, "/account/name") | 
					
						
							|  |  |  | api.add_resource(AccountAvatarApi, "/account/avatar") | 
					
						
							|  |  |  | api.add_resource(AccountInterfaceLanguageApi, "/account/interface-language") | 
					
						
							|  |  |  | api.add_resource(AccountInterfaceThemeApi, "/account/interface-theme") | 
					
						
							|  |  |  | api.add_resource(AccountTimezoneApi, "/account/timezone") | 
					
						
							|  |  |  | api.add_resource(AccountPasswordApi, "/account/password") | 
					
						
							|  |  |  | api.add_resource(AccountIntegrateApi, "/account/integrates") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | # api.add_resource(AccountEmailApi, '/account/email') | 
					
						
							|  |  |  | # api.add_resource(AccountEmailVerifyApi, '/account/email-verify') |