From 6d3dfca2148dbdde73cc00c413063474df2e8410 Mon Sep 17 00:00:00 2001 From: phot0n Date: Sun, 5 Jun 2022 22:34:02 +0530 Subject: [PATCH] fix: consider oauth usage as well for asking/updating user email password --- frappe/core/doctype/user/user.py | 4 +-- .../core/doctype/user_email/user_email.json | 15 +++++++++-- .../doctype/email_account/email_account.py | 26 +++++++++++++------ frappe/public/js/frappe/desk.js | 1 - 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/frappe/core/doctype/user/user.py b/frappe/core/doctype/user/user.py index eb3c11a4db..12a48afe7e 100644 --- a/frappe/core/doctype/user/user.py +++ b/frappe/core/doctype/user/user.py @@ -766,7 +766,7 @@ def get_email_awaiting(user): return frappe.get_all( "User Email", fields=["email_account", "email_id"], - filters={"awaiting_password": 1, "parent": user}, + filters={"awaiting_password": 1, "parent": user, "used_oauth": 0}, ) @@ -775,7 +775,7 @@ def ask_pass_update(): from frappe.utils import set_default password_list = frappe.get_all( - "User Email", filters={"awaiting_password": True}, pluck="parent", distinct=True + "User Email", filters={"awaiting_password": 1, "used_oauth": 0}, pluck="parent", distinct=True ) set_default("email_user_password", ",".join(password_list)) diff --git a/frappe/core/doctype/user_email/user_email.json b/frappe/core/doctype/user_email/user_email.json index b106ed4a19..43dc3ace8d 100644 --- a/frappe/core/doctype/user_email/user_email.json +++ b/frappe/core/doctype/user_email/user_email.json @@ -9,6 +9,7 @@ "email_id", "column_break_3", "awaiting_password", + "used_oauth", "enable_outgoing" ], "fields": [ @@ -48,16 +49,26 @@ "fieldtype": "Check", "label": "Enable Outgoing", "read_only": 1 + }, + { + "default": "0", + "fetch_from": "email_account.use_oauth", + "fieldname": "used_oauth", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Used OAuth", + "read_only": 1 } ], "istable": 1, "links": [], - "modified": "2020-04-06 19:19:12.130246", + "modified": "2022-06-03 14:25:46.944733", "modified_by": "Administrator", "module": "Core", "name": "User Email", "owner": "Administrator", "permissions": [], "sort_field": "modified", - "sort_order": "DESC" + "sort_order": "DESC", + "states": [] } \ No newline at end of file diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 4eb230c372..278aeb698b 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -83,13 +83,17 @@ class EmailAccount(Document): if getattr(self, "service", "") != "GMail" and self.use_oauth: self.use_oauth = 0 - if not self.use_oauth and self.refresh_token: + if self.use_oauth: + # no need for awaiting password for oauth + self.awaiting_password = 0 + + elif self.refresh_token: # clear access & refresh token self.refresh_token = self.access_token = None if not frappe.local.flags.in_install and (self.use_oauth or not self.awaiting_password): if ( - (self.use_oauth and self.refresh_token) + self.refresh_token or self.password or self.smtp_server in ("127.0.0.1", "localhost") ): @@ -154,6 +158,7 @@ class EmailAccount(Document): awaiting_password=self.awaiting_password, email_id=self.email_id, enable_outgoing=self.enable_outgoing, + used_oauth=self.use_oauth, ) def there_must_be_only_one_default(self): @@ -839,7 +844,7 @@ def get_max_email_uid(email_account): return max_uid -def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_outgoing): +def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_outgoing, used_oauth): """setup email inbox for user""" from frappe.core.doctype.user.user import ask_pass_update @@ -850,6 +855,7 @@ def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_ou row.email_id = email_id row.email_account = email_account row.awaiting_password = awaiting_password or 0 + row.used_oauth = used_oauth or 0 row.enable_outgoing = enable_outgoing or 0 user.save(ignore_permissions=True) @@ -881,8 +887,12 @@ def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_ou if update_user_email_settings: UserEmail = frappe.qb.DocType("User Email") - frappe.qb.update(UserEmail).set(UserEmail.awaiting_password, (awaiting_password or 0)).set( - UserEmail.enable_outgoing, enable_outgoing + frappe.qb.update(UserEmail).set( + UserEmail.awaiting_password, (awaiting_password or 0) + ).set( + UserEmail.enable_outgoing, (enable_outgoing or 0) + ).set( + UserEmail.used_oauth, (used_oauth or 0) ).where(UserEmail.email_account == email_account).run() else: @@ -908,10 +918,10 @@ def remove_user_email_inbox(email_account): doc.save(ignore_permissions=True) -@frappe.whitelist(allow_guest=False) -def set_email_password(email_account, user, password): +@frappe.whitelist() +def set_email_password(email_account, password): account = frappe.get_doc("Email Account", email_account) - if account.awaiting_password: + if account.awaiting_password and not account.use_oauth: account.awaiting_password = 0 account.password = password try: diff --git a/frappe/public/js/frappe/desk.js b/frappe/public/js/frappe/desk.js index a8cbe020f3..f1a5d00dfd 100644 --- a/frappe/public/js/frappe/desk.js +++ b/frappe/public/js/frappe/desk.js @@ -251,7 +251,6 @@ frappe.Application = class Application { method: 'frappe.email.doctype.email_account.email_account.set_email_password', args: { "email_account": email_account[i]["email_account"], - "user": user, "password": d.get_value("password") }, callback: function(passed) {