Files
factorio-clusterio/packages/web_ui/src/model/account.jsx
T
Hornwitser 18e9fc8e7c 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.
2021-09-20 00:52:59 +02:00

77 lines
1.8 KiB
React

import { useContext, useEffect, useState } from "react";
import { notifyErrorHandler } from "../util/notify";
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 onAccountUpdate(account) {
setName(account.name);
setRoles(account.roles);
}
control.onAccountUpdate(onAccountUpdate);
return () => {
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"));
},
};
}