mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Provide roles and permission for the control user
Add the roles and their permissions for the user the control connection is connected as along with updates when these change. This enables the web interface to know which permissions it has available to it.
This commit is contained in:
1 parent
1035389c9e
commit
18e9fc8e7c
9 files changed
+178
-10
No files matched your search
@@ -6,30 +6,68 @@ import ControlContext from "../components/ControlContext";
|
||||
/**
|
||||
* @typedef {Object} Account
|
||||
* @property {?string} name - Name of the currently logged in account.
|
||||
* @property {?Array<object>} roles - Roles of the corrently logged in account.
|
||||
* @property {function(string)} hasPermission -
|
||||
* Check if the currently logged in account has the given permission.
|
||||
* @property {function()} logout - Logs out of the web interface.
|
||||
*/
|
||||
|
||||
export function useAccount() {
|
||||
let control = useContext(ControlContext);
|
||||
let [name, setName] = useState(control.accountName);
|
||||
let [roles, setRoles] = useState(control.accountRoles);
|
||||
|
||||
useEffect(() => {
|
||||
function onConnect(data) {
|
||||
setName(data.account.name);
|
||||
function onAccountUpdate(account) {
|
||||
setName(account.name);
|
||||
setRoles(account.roles);
|
||||
}
|
||||
function onClose() {
|
||||
setName(null);
|
||||
}
|
||||
control.connector.on("connect", onConnect);
|
||||
control.connector.on("close", onClose);
|
||||
control.onAccountUpdate(onAccountUpdate);
|
||||
return () => {
|
||||
control.connector.off("connect", onConnect);
|
||||
control.connector.off("close", onClose);
|
||||
control.offAccountUpdate(onAccountUpdate);
|
||||
};
|
||||
}, [control]);
|
||||
|
||||
function hasPermission(permission) {
|
||||
for (let role of roles) {
|
||||
if (role.permissions.includes("core.admin") || role.permissions.includes(permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
roles,
|
||||
hasPermission(permission) {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
return hasPermission(permission);
|
||||
},
|
||||
hasAnyPermission(...permissions) {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
for (let permission of permissions) {
|
||||
if (hasPermission(permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
hasAllPermission(...permissions) {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
for (let permission of permissions) {
|
||||
if (!hasPermission(permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
logOut() {
|
||||
control.loggingOut = true;
|
||||
control.shutdown(notifyErrorHandler("Error logging out"));
|
||||
|
||||
@@ -60,6 +60,13 @@ export class Control extends libLink.Link {
|
||||
*/
|
||||
this.accountName = null;
|
||||
|
||||
/**
|
||||
* Roles of the account this control link is connected as.
|
||||
* @type {?Array<object>}
|
||||
*/
|
||||
this.accountRoles = null;
|
||||
|
||||
this.accountUpdateHandlers = [];
|
||||
this.slaveUpdateHandlers = new Map();
|
||||
this.instanceUpdateHandlers = new Map();
|
||||
this.saveListUpdateHandlers = new Map();
|
||||
@@ -67,6 +74,8 @@ export class Control extends libLink.Link {
|
||||
|
||||
this.connector.on("connect", data => {
|
||||
this.accountName = data.account.name;
|
||||
this.accountRoles = data.account.roles;
|
||||
this.emitAccountUpdate();
|
||||
this.updateSlaveSubscriptions().catch(err => logger.error(
|
||||
`Unexpected error updating slave subscriptions:\n${err.stack}`
|
||||
));
|
||||
@@ -83,6 +92,8 @@ export class Control extends libLink.Link {
|
||||
|
||||
this.connector.on("close", () => {
|
||||
this.accountName = null;
|
||||
this.accountRoles = null;
|
||||
this.emitAccountUpdate();
|
||||
});
|
||||
|
||||
for (let event of ["connect", "drop", "resume", "close"]) {
|
||||
@@ -94,6 +105,32 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
}
|
||||
|
||||
async accountUpdateEventHandler(message) {
|
||||
this.accountRoles = message.data.roles;
|
||||
this.emitAccountUpdate();
|
||||
}
|
||||
|
||||
emitAccountUpdate() {
|
||||
for (let handler of this.accountUpdateHandlers) {
|
||||
handler({
|
||||
name: this.accountName,
|
||||
roles: this.accountRoles,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onAccountUpdate(handler) {
|
||||
this.accountUpdateHandlers.push(handler);
|
||||
}
|
||||
|
||||
offAccountUpdate(handler) {
|
||||
let index = this.accountUpdateHandlers.lastIndexOf(handler);
|
||||
if (index === -1) {
|
||||
throw new Error("Given handler is not registered for account update");
|
||||
}
|
||||
this.accountUpdateHandlers.splice(index, 1);
|
||||
}
|
||||
|
||||
async slaveUpdateEventHandler(message) {
|
||||
let handlers = [].concat(
|
||||
this.slaveUpdateHandlers.get(null) || [],
|
||||
|
||||
Reference in new issue
Block a user