| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
/* import { superuser } from "superuser"; |
| 9 |
* |
| 10 |
* The "superuser" object indicates whether or not the current page |
| 11 |
* can open superuser channels. |
| 12 |
* |
| 13 |
* - superuser.allowed |
| 14 |
* |
| 15 |
* This is true when the page can open superuser channels, and false |
| 16 |
* otherwise. This field might be "null" while the page or the Cockpit |
| 17 |
* session itself is still initializing. |
| 18 |
* |
| 19 |
* UI elements that trigger actions that need administrative access |
| 20 |
* should be hidden when the "allowed" field is false or null. (If |
| 21 |
* those elements also show information, such as with checkboxes or |
| 22 |
* toggle buttons, disable them instead of hiding.) |
| 23 |
* |
| 24 |
* UI elements that alert the user that they don't have administrative |
| 25 |
* access should be shown when the "allowed" field is exactly false, |
| 26 |
* but not when it is null. |
| 27 |
* |
| 28 |
* - superuser.configured |
| 29 |
* |
| 30 |
* This is true when the shell has at least one superuser bridge configured, |
| 31 |
* false if there are no superuser bridges, or null during initialization. |
| 32 |
* |
| 33 |
* - superuser.addEventListener("changed", () => ...) |
| 34 |
* |
| 35 |
* The event handler is called whenever superuser.allowed has changed. |
| 36 |
* A page should update its appearance according to superuser.allowed. |
| 37 |
* |
| 38 |
* - superuser.addEventListener("reconnect", () => ...) |
| 39 |
* |
| 40 |
* The event handler is called whenever channels should be re-opened |
| 41 |
* that use the "superuser" option. |
| 42 |
* |
| 43 |
* The difference between "reconnect" and "connect" is that the |
| 44 |
* "reconnect" signal does not trigger when superuser.allowed goes |
| 45 |
* from "null" to its first real value. You don't need to re-open |
| 46 |
* channels in this case, and it happens on every page load, so this |
| 47 |
* is important to avoid. |
| 48 |
* |
| 49 |
* - superuser.reload_page_on_change() |
| 50 |
* |
| 51 |
* Calling this function instructs the "superuser" object to reload |
| 52 |
* the page whenever "superuser.allowed" changes. This is a (bad) |
| 53 |
* alternative to re-initializing the page and intended to be used |
| 54 |
* only to help with the transition. |
| 55 |
* |
| 56 |
* Even if you are using "superuser.reload_page_on_change" to avoid having |
| 57 |
* to re-initialize your page dynamically, you should still use the |
| 58 |
* "changed" event to update the page appearance since |
| 59 |
* "superuser.allowed" might still change a couple of times right |
| 60 |
* after page reload. |
| 61 |
*/ |
| 62 |
|
| 63 |
function Superuser() { |
| 64 |
const proxy = cockpit.dbus(null, { bus: "internal" }).proxy("cockpit.Superuser", "/superuser"); |
| 65 |
let reload_on_change = false; |
| 66 |
|
| 67 |
const compute_allowed = () => { |
| 68 |
if (!proxy.valid || proxy.Current == "init") |
| 69 |
return null; |
| 70 |
return proxy.Current != "none"; |
| 71 |
}; |
| 72 |
|
| 73 |
const compute_configured = () => { |
| 74 |
if (proxy.Current == "init") |
| 75 |
return null; |
| 76 |
return (proxy.Bridges?.length ?? 0) > 0; |
| 77 |
}; |
| 78 |
|
| 79 |
const self = cockpit.event_target({ |
| 80 |
allowed: compute_allowed(), |
| 81 |
configured: null, |
| 82 |
reload_page_on_change |
| 83 |
}); |
| 84 |
|
| 85 |
function changed(allowed) { |
| 86 |
if (self.allowed != allowed) { |
| 87 |
if (self.allowed != null && reload_on_change) { |
| 88 |
window.location.reload(true); |
| 89 |
} else { |
| 90 |
const prev = self.allowed; |
| 91 |
self.allowed = allowed; |
| 92 |
self.configured = compute_configured(); |
| 93 |
self.dispatchEvent("changed"); |
| 94 |
if (prev != null) |
| 95 |
self.dispatchEvent("reconnect"); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
proxy.wait(() => { |
| 101 |
if (!proxy.valid) { |
| 102 |
// Fall back to cockpit.permissions |
| 103 |
const permission = cockpit.permission({ admin: true }); |
| 104 |
const update = () => { |
| 105 |
changed(permission.allowed); |
| 106 |
}; |
| 107 |
permission.addEventListener("changed", update); |
| 108 |
update(); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
proxy.addEventListener("changed", () => { |
| 113 |
changed(compute_allowed()); |
| 114 |
}); |
| 115 |
|
| 116 |
function reload_page_on_change() { |
| 117 |
reload_on_change = true; |
| 118 |
} |
| 119 |
|
| 120 |
return self; |
| 121 |
} |
| 122 |
|
| 123 |
export const superuser = Superuser(); |