minor: better oauth flow
This commit is contained in:
parent
221423c718
commit
ab9a577474
4 changed files with 43 additions and 25 deletions
|
|
@ -67,6 +67,21 @@ frappe.email_defaults_pop = {
|
|||
|
||||
};
|
||||
|
||||
function oauth_access(frm) {
|
||||
return frappe.call({
|
||||
method: "frappe.email.oauth.oauth_access",
|
||||
args: {
|
||||
"email_account": frm.doc.name,
|
||||
"service": frm.doc.service || ""
|
||||
},
|
||||
callback: function(r) {
|
||||
if (!r.exc) {
|
||||
window.open(r.message.url);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
frappe.ui.form.on("Email Account", {
|
||||
service: function(frm) {
|
||||
$.each(frappe.email_defaults[frm.doc.service], function(key, value) {
|
||||
|
|
@ -126,6 +141,7 @@ frappe.ui.form.on("Email Account", {
|
|||
frm.events.enable_incoming(frm);
|
||||
frm.events.notify_if_unreplied(frm);
|
||||
frm.events.show_gmail_message_for_less_secure_apps(frm);
|
||||
frm.events.show_oauth_authorization_message(frm);
|
||||
|
||||
if (frappe.route_flags.delete_user_from_locals && frappe.route_flags.linked_user) {
|
||||
delete frappe.route_flags.delete_user_from_locals;
|
||||
|
|
@ -133,6 +149,12 @@ frappe.ui.form.on("Email Account", {
|
|||
}
|
||||
},
|
||||
|
||||
after_save(frm) {
|
||||
if (frm.doc.use_oauth && !frm.doc.refresh_token) {
|
||||
oauth_access(frm);
|
||||
}
|
||||
},
|
||||
|
||||
show_gmail_message_for_less_secure_apps: function(frm) {
|
||||
frm.dashboard.clear_headline();
|
||||
let msg = __("GMail will only work if you enable 2-step authentication and use app-specific password OR use OAuth.");
|
||||
|
|
@ -143,19 +165,16 @@ frappe.ui.form.on("Email Account", {
|
|||
}
|
||||
},
|
||||
|
||||
show_oauth_authorization_message(frm) {
|
||||
let msg = __("Oauth Enabled but not Authorized. Please use `Authorize API Access` Button to do the same.");
|
||||
if (frm.doc.use_oauth && !frm.doc.refresh_token) {
|
||||
frm.dashboard.clear_headline();
|
||||
frm.dashboard.set_headline_alert(msg, "yellow");
|
||||
}
|
||||
},
|
||||
|
||||
authorize_api_access: function(frm) {
|
||||
frappe.call({
|
||||
method: "frappe.email.oauth.oauth_access",
|
||||
args: {
|
||||
"email_account": frm.doc.name,
|
||||
"service": frm.doc.service || ""
|
||||
},
|
||||
callback: function(r) {
|
||||
if (!r.exc) {
|
||||
window.open(r.message.url);
|
||||
}
|
||||
}
|
||||
});
|
||||
oauth_access(frm);
|
||||
},
|
||||
|
||||
email_id:function(frm) {
|
||||
|
|
|
|||
|
|
@ -613,7 +613,7 @@
|
|||
"icon": "fa fa-inbox",
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2022-05-30 15:45:05.282867",
|
||||
"modified": "2022-06-01 17:51:37.878446",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Email",
|
||||
"name": "Email Account",
|
||||
|
|
|
|||
|
|
@ -83,8 +83,16 @@ 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:
|
||||
# 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.refresh_token or self.password or self.smtp_server in ("127.0.0.1", "localhost"):
|
||||
if (
|
||||
(self.use_oauth and self.refresh_token)
|
||||
or self.password
|
||||
or self.smtp_server in ("127.0.0.1", "localhost")
|
||||
):
|
||||
if self.enable_incoming:
|
||||
self.get_incoming_server()
|
||||
self.no_failed = 0
|
||||
|
|
@ -93,10 +101,7 @@ class EmailAccount(Document):
|
|||
self.validate_smtp_conn()
|
||||
else:
|
||||
if self.enable_incoming or (self.enable_outgoing and not self.no_smtp_authentication):
|
||||
if self.use_oauth:
|
||||
if not self.is_new():
|
||||
frappe.throw(_("Please Enable OAuth by using `Authorize API access` button"))
|
||||
else:
|
||||
if not self.use_oauth:
|
||||
frappe.throw(_("Password is required or select Awaiting Password"))
|
||||
|
||||
if self.notify_if_unreplied:
|
||||
|
|
@ -151,12 +156,6 @@ class EmailAccount(Document):
|
|||
enable_outgoing=self.enable_outgoing,
|
||||
)
|
||||
|
||||
def after_insert(self):
|
||||
if self.use_oauth and not self.refresh_token:
|
||||
frappe.msgprint(
|
||||
_("Please Enable OAuth by using `Authorize API access` button"), indicator="orange"
|
||||
)
|
||||
|
||||
def there_must_be_only_one_default(self):
|
||||
"""If current Email Account is default, un-default all other accounts."""
|
||||
for field in ("default_incoming", "default_outgoing"):
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ def handle_response(
|
|||
)
|
||||
|
||||
if raise_err:
|
||||
frappe.throw(frappe._(error_title), frappe._(error_message))
|
||||
frappe.throw(frappe._(error_title), GoogleAuthenticationError, frappe._(error_message))
|
||||
|
||||
return {}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue