Files
NODEDC_LAUNCHER/server/device-core-session-access.mjs
T

103 lines
3.8 KiB
JavaScript

const administrativeMembershipRoles = new Set(["client_owner", "client_admin"]);
const administrativeAppRoles = new Set(["owner", "admin"]);
const appRoleWeights = Object.freeze({ viewer: 10, member: 20, admin: 30, owner: 40 });
export function resolveDeviceCoreSessionAccess(data, user, serviceSlug = "device-core") {
const empty = Object.freeze({ allowed: false, hubRole: "viewer", ownerScopes: Object.freeze([]) });
if (!data || !user || user.globalStatus !== "active") return empty;
const service = data.services?.find((candidate) => candidate.slug === serviceSlug);
if (!service || service.status !== "active") return empty;
if (hasUserException(data, user.id, service.id, "deny")) return empty;
const ownerScopes = [];
const roles = [];
const isRoot = user.id === "user_root";
const directGrant = activeGrant(data, service.id, "user", user.id);
const explicitlyAllowed = hasUserException(data, user.id, service.id, "allow");
if (isRoot) {
roles.push("owner");
} else if (explicitlyAllowed) {
roles.push("member");
} else if (directGrant) {
roles.push(directGrant.appRole);
}
for (const membership of data.memberships ?? []) {
if (membership.userId !== user.id || membership.status !== "active") continue;
const client = data.clients?.find((candidate) => candidate.id === membership.clientId);
if (!client || ["suspended", "expired"].includes(client.status)) continue;
const clientGroupIds = new Set(
(data.groups ?? [])
.filter((group) => group.clientId === client.id && group.memberIds?.includes(user.id))
.map((group) => group.id),
);
const groupGrant = (data.grants ?? []).find((grant) =>
grant.serviceId === service.id
&& grant.targetType === "group"
&& clientGroupIds.has(grant.targetId)
&& grant.status === "active");
const clientGrant = activeGrant(data, service.id, "client", client.id);
const scopedGrant = groupGrant || clientGrant;
if (!explicitlyAllowed && scopedGrant) roles.push(scopedGrant.appRole);
if (
client.type === "company"
&& administrativeMembershipRoles.has(membership.role)
&& (isRoot || (!explicitlyAllowed && administrativeAppRoles.has(scopedGrant?.appRole)))
) {
ownerScopes.push({
scopeKind: "company",
ownerRef: `client:${client.id}`,
displayName: String(client.name || client.legalName || client.id).trim().slice(0, 240),
});
}
}
const hubRole = strongestRole(roles);
if (!hubRole) return empty;
if (administrativeAppRoles.has(hubRole)) {
ownerScopes.push({
scopeKind: "personal",
ownerRef: `user:${user.id}`,
displayName: String(user.name || user.email || user.id).trim().slice(0, 240),
});
}
return Object.freeze({
allowed: true,
hubRole,
ownerScopes: Object.freeze(uniqueOwnerScopes(ownerScopes)),
});
}
function activeGrant(data, serviceId, targetType, targetId) {
return (data.grants ?? []).find((grant) =>
grant.serviceId === serviceId
&& grant.targetType === targetType
&& grant.targetId === targetId
&& grant.status === "active") ?? null;
}
function hasUserException(data, userId, serviceId, type) {
return (data.exceptions ?? []).some((exception) =>
exception.serviceId === serviceId
&& exception.userId === userId
&& exception.type === type);
}
function strongestRole(roles) {
return roles
.filter((role) => Object.hasOwn(appRoleWeights, role))
.sort((left, right) => appRoleWeights[right] - appRoleWeights[left])[0] ?? null;
}
function uniqueOwnerScopes(scopes) {
const byIdentity = new Map();
for (const scope of scopes) byIdentity.set(`${scope.scopeKind}\0${scope.ownerRef}`, scope);
return [...byIdentity.values()].sort((left, right) =>
`${left.scopeKind}:${left.ownerRef}`.localeCompare(`${right.scopeKind}:${right.ownerRef}`));
}