| 1 |
import { Interaction } from "@web/public/interaction"; |
| 2 |
import { registry } from "@web/core/registry"; |
| 3 |
|
| 4 |
import { InputConfirmationDialog } from "@portal/js/components/input_confirmation_dialog/input_confirmation_dialog"; |
| 5 |
import { handleCheckIdentity } from "@portal/interactions/portal_security"; |
| 6 |
import { browser } from "@web/core/browser/browser"; |
| 7 |
import { user } from "@web/core/user"; |
| 8 |
import { _t } from "@web/core/l10n/translation"; |
| 9 |
|
| 10 |
import { markup } from "@odoo/owl"; |
| 11 |
|
| 12 |
/** |
| 13 |
* Replaces specific <field> elements by normal HTML, strip out the rest entirely |
| 14 |
*/ |
| 15 |
function fromField(f, record) { |
| 16 |
switch (f.getAttribute("name")) { |
| 17 |
case "qrcode": |
| 18 |
const qrcode = document.createElement("img"); |
| 19 |
qrcode.setAttribute("class", "img img-fluid"); |
| 20 |
qrcode.setAttribute("src", "data:image/png;base64," + record["qrcode"]); |
| 21 |
return qrcode; |
| 22 |
case "url": |
| 23 |
const url = document.createElement("a"); |
| 24 |
url.setAttribute("href", record["url"]); |
| 25 |
url.textContent = f.getAttribute("text") || record["url"]; |
| 26 |
return url; |
| 27 |
case "code": |
| 28 |
const code = document.createElement("input"); |
| 29 |
code.setAttribute("name", "code"); |
| 30 |
code.setAttribute("class", "form-control col-10 col-md-6"); |
| 31 |
code.setAttribute("placeholder", "6-digit code"); |
| 32 |
code.required = true; |
| 33 |
code.maxLength = 6; |
| 34 |
code.minLength = 6; |
| 35 |
return code; |
| 36 |
case "secret": |
| 37 |
// As CopyClipboard wizard is backend only, mimic his behaviour to use it in frontend. |
| 38 |
// Field |
| 39 |
const secretSpan = document.createElement("span"); |
| 40 |
secretSpan.setAttribute("name", "secret"); |
| 41 |
secretSpan.setAttribute("class", "o_field_copy_url"); |
| 42 |
secretSpan.textContent = record["secret"]; |
| 43 |
|
| 44 |
// Copy Button |
| 45 |
const copySpanIcon = document.createElement("span"); |
| 46 |
copySpanIcon.setAttribute("class", "fa fa-clipboard"); |
| 47 |
const copySpanText = document.createElement("span"); |
| 48 |
copySpanText.textContent = _t(" Copy"); |
| 49 |
|
| 50 |
const copyButton = document.createElement("button"); |
| 51 |
copyButton.setAttribute("class", "btn btn-sm btn-primary o_clipboard_button o_btn_char_copy py-0 px-2"); |
| 52 |
copyButton.onclick = async function (event) { |
| 53 |
event.preventDefault(); |
| 54 |
$(copyButton).tooltip({ title: _t("Copied!"), trigger: "manual", placement: "bottom" }); |
| 55 |
await browser.navigator.clipboard.writeText($(secretSpan)[0].innerText); |
| 56 |
$(copyButton).tooltip("show"); |
| 57 |
setTimeout(() => $(copyButton).tooltip("hide"), 800); |
| 58 |
}; |
| 59 |
|
| 60 |
copyButton.appendChild(copySpanIcon); |
| 61 |
copyButton.appendChild(copySpanText); |
| 62 |
|
| 63 |
// CopyClipboard Div |
| 64 |
const secretDiv = document.createElement("div"); |
| 65 |
secretDiv.setAttribute("class", "o_field_copy d-flex justify-content-center align-items-center"); |
| 66 |
secretDiv.appendChild(secretSpan); |
| 67 |
secretDiv.appendChild(copyButton); |
| 68 |
|
| 69 |
return secretDiv; |
| 70 |
default: // just display the field's data |
| 71 |
return document.createTextNode(record[f.getAttribute("name")] || ""); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Apparently chrome literally absolutely can't handle parsing XML and using |
| 77 |
* those nodes in an HTML document (even when parsing as application/xhtml+xml), |
| 78 |
* this results in broken rendering and a number of things not working (e.g. |
| 79 |
* classes) without any specific warning in the console or anything, things are |
| 80 |
* just broken with no indication of why. |
| 81 |
* |
| 82 |
* So... rebuild the entire f'ing body using document.createElement to ensure |
| 83 |
* we have HTML elements. |
| 84 |
* |
| 85 |
* This is a recursive implementation so it's not super efficient but the views |
| 86 |
* to fixup *should* be relatively simple. |
| 87 |
*/ |
| 88 |
function fixupViewBody(oldNode, record) { |
| 89 |
let qrcode = null, code = null, node = null; |
| 90 |
|
| 91 |
switch (oldNode.nodeType) { |
| 92 |
case 1: // element |
| 93 |
if (oldNode.tagName === "field") { |
| 94 |
node = fromField(oldNode, record); |
| 95 |
switch (oldNode.getAttribute("name")) { |
| 96 |
case "qrcode": |
| 97 |
qrcode = node; |
| 98 |
break; |
| 99 |
case "code": |
| 100 |
code = node; |
| 101 |
break |
| 102 |
} |
| 103 |
break; // no need to recurse here |
| 104 |
} |
| 105 |
node = document.createElement(oldNode.tagName); |
| 106 |
for (let i = 0; i < oldNode.attributes.length; ++i) { |
| 107 |
const attr = oldNode.attributes[i]; |
| 108 |
node.setAttribute(attr.name, attr.value); |
| 109 |
} |
| 110 |
for (let j = 0; j < oldNode.childNodes.length; ++j) { |
| 111 |
const [ch, qr, co] = fixupViewBody(oldNode.childNodes[j], record); |
| 112 |
if (ch) { node.appendChild(ch); } |
| 113 |
if (qr) { qrcode = qr; } |
| 114 |
if (co) { code = co; } |
| 115 |
} |
| 116 |
break; |
| 117 |
case 3: case 4: // text, cdata |
| 118 |
node = document.createTextNode(oldNode.data); |
| 119 |
break; |
| 120 |
default: |
| 121 |
// don't care about PI & al |
| 122 |
} |
| 123 |
|
| 124 |
return [node, qrcode, code] |
| 125 |
} |
| 126 |
|
| 127 |
export class TOTPEnable extends Interaction { |
| 128 |
static selector = "#auth_totp_portal_enable"; |
| 129 |
dynamicContent = { |
| 130 |
_root: { "t-on-click.prevent": this.onClick }, |
| 131 |
}; |
| 132 |
|
| 133 |
async onClick() { |
| 134 |
const data = await this.waitFor(handleCheckIdentity( |
| 135 |
this.waitFor(this.services.orm.call("res.users", "action_totp_enable_wizard", [user.userId])), |
| 136 |
this.services.orm, |
| 137 |
this.services.dialog, |
| 138 |
)); |
| 139 |
|
| 140 |
if (!data) { |
| 141 |
// TOTP probably already enabled, just reload page |
| 142 |
location.reload() |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
const model = data.res_model; |
| 147 |
const wizard_id = data.res_id; |
| 148 |
const record = (await this.services.orm.read(model, [wizard_id], []))[0]; |
| 149 |
|
| 150 |
const doc = new DOMParser().parseFromString( |
| 151 |
document.getElementById("totp_wizard_view").textContent, |
| 152 |
"application/xhtml+xml" |
| 153 |
); |
| 154 |
|
| 155 |
const xmlBody = doc.querySelector("sheet *"); |
| 156 |
const [body, ,] = fixupViewBody(xmlBody, record); |
| 157 |
|
| 158 |
this.services.dialog.add(InputConfirmationDialog, { |
| 159 |
body: markup(body.outerHTML), |
| 160 |
onInput: ({ inputEl }) => { inputEl.setCustomValidity("") }, |
| 161 |
confirmLabel: _t("Activate"), |
| 162 |
confirm: async ({ inputEl }) => { |
| 163 |
if (!inputEl.reportValidity()) { |
| 164 |
inputEl.classList.add("is-invalid"); |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
try { |
| 169 |
await handleCheckIdentity( |
| 170 |
this.waitFor(this.services.orm.call(model, "enable", |
| 171 |
[ record.id ], |
| 172 |
{ 'context': {'code': inputEl.value} }, |
| 173 |
)), |
| 174 |
this.services.orm, |
| 175 |
this.services.dialog |
| 176 |
); |
| 177 |
} catch (e) { |
| 178 |
const errorMessage = ( |
| 179 |
!e.message ? e.toString() |
| 180 |
: !e.message.data ? e.message.message |
| 181 |
: e.message.data.message || _t("Operation failed for unknown reason.") |
| 182 |
); |
| 183 |
inputEl.classList.add("is-invalid"); |
| 184 |
// show custom validity error message |
| 185 |
inputEl.setCustomValidity(errorMessage); |
| 186 |
inputEl.reportValidity(); |
| 187 |
return false; |
| 188 |
} |
| 189 |
// reloads page, avoid window.location.reload() because it re-posts forms |
| 190 |
location.reload(); |
| 191 |
}, |
| 192 |
cancel: () => { }, |
| 193 |
}); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
registry |
| 198 |
.category("public.interactions") |
| 199 |
.add("auth_totp_portal.totp_enable", TOTPEnable); |