Skip to content

How to check if the user switched to Administrative Access mode? #21153

Answered by martinpitt
RamonGiovane asked this question in Q&A
Discussion options

You must be logged in to vote

I.e. you want your page to show an alert or similar when not in privileged mode? This is quite common indeed. We have a little superuser.js library (which you can pull in from cockpit like starter-kit does, or just copy). This has a superuser.allowed boolean(ish) property, and also a callback when the superuser state changes, so that you page can react.

cockpit.permission is older code, and TBH I'm not sure right now how superuser.js is "better" -- but this should also work:

const permission = cockpit.permission({ admin: true });
const amISuperuser = permission.allowed;
permission.addEventListener("changed", () => console.log("it changed!"));

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@RamonGiovane
Comment options

@martinpitt
Comment options

Answer selected by RamonGiovane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants