|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Polkit Agent Example</title> |
| 5 | + <script> |
| 6 | + mikaShell.window.init({}); |
| 7 | + mikaShell.polkitAgent.on("cancel", () => { |
| 8 | + mikaShell.window.close(); |
| 9 | + }); |
| 10 | + let user = null; |
| 11 | + const $ = document.querySelector.bind(document); |
| 12 | + const auth = () => { |
| 13 | + $("#error").textContent = "Authenticating..."; |
| 14 | + $("#approve").disabled = true; |
| 15 | + if (user === null) { |
| 16 | + $("#error").textContent = "User not found."; |
| 17 | + $("#approve").disabled = false; |
| 18 | + return; |
| 19 | + } |
| 20 | + const password = $("#password").value; |
| 21 | + mikaShell.polkitAgent |
| 22 | + .auth(data.cookie, user.name, password) |
| 23 | + .then((result) => { |
| 24 | + if (result.ok) { |
| 25 | + mikaShell.window.close(); |
| 26 | + } else { |
| 27 | + $("#error").textContent = result.err; |
| 28 | + } |
| 29 | + }) |
| 30 | + .finally(() => { |
| 31 | + $("#approve").disabled = false; |
| 32 | + }); |
| 33 | + }; |
| 34 | + const cancel = () => { |
| 35 | + mikaShell.polkitAgent.cancel(data.cookie); |
| 36 | + mikaShell.window.close(); |
| 37 | + }; |
| 38 | + </script> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <h1>Polkit Agent Example</h1> |
| 42 | + <p id="actionId"></p> |
| 43 | + <p id="message"></p> |
| 44 | + <p>Enter your password to approve the action:</p> |
| 45 | + <p>User: <span id="username"></span></p> |
| 46 | + <input id="password" type="password" /> |
| 47 | + <button onclick="cancel()">Cancel</button> |
| 48 | + <button id="approve" onclick="auth()">Approve</button> |
| 49 | + <br /> |
| 50 | + <p id="error"></p> |
| 51 | + <script> |
| 52 | + const data = JSON.parse(new URLSearchParams(window.location.search).get("data")); |
| 53 | + console.log(data); |
| 54 | + $("#actionId").textContent = data.actionId; |
| 55 | + $("#message").textContent = data.message; |
| 56 | + for (const identity of data.identities) { |
| 57 | + if (identity["unixUser"]) { |
| 58 | + mikaShell.os.getUserInfo(identity["unixUser"]).then((u) => { |
| 59 | + user = u; |
| 60 | + $("#username").textContent = u.name; |
| 61 | + }); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + </script> |
| 66 | + </body> |
| 67 | +</html> |
0 commit comments