mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Merge pull request #1056 from bbassie/fix/subscription-permission-revoke
Drop subscriptions when a user loses permission for them
This commit is contained in:
6 files changed
+188
-17
No files matched your search
@@ -875,6 +875,16 @@ export default class ControlConnection extends BaseConnection {
|
||||
this.updateLogSubscriptions();
|
||||
}
|
||||
|
||||
/** Stop following logs if the user no longer has permission to */
|
||||
checkLogPermission() {
|
||||
try {
|
||||
this.user.checkPermission("core.log.follow");
|
||||
} catch {
|
||||
this.logSubscriptions = { all: false, controller: false, hostIds: [], instanceIds: [] };
|
||||
this.updateLogSubscriptions();
|
||||
}
|
||||
}
|
||||
|
||||
updateLogSubscriptions() {
|
||||
let { all, controller, hostIds, instanceIds } = this.logSubscriptions;
|
||||
if (all || controller || hostIds.length || instanceIds.length) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type winston from "winston";
|
||||
import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net";
|
||||
import type { ControllerArgs } from "../controller.js";
|
||||
import type ControlConnection from "./ControlConnection.js";
|
||||
import express, { type Request, type Response, type NextFunction, type Application } from "express";
|
||||
import type { Static } from "@sinclair/typebox";
|
||||
import finalhandler from "finalhandler";
|
||||
@@ -1087,17 +1088,24 @@ export default class Controller {
|
||||
userPermissionsUpdated(user: Readonly<User>) {
|
||||
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||
if (controlConnection.user === user) {
|
||||
controlConnection.send(
|
||||
new lib.AccountUpdateEvent([...user.roles].map(r => ({
|
||||
name: r.name,
|
||||
id: r.id,
|
||||
permissions: [...r.permissions],
|
||||
})))
|
||||
);
|
||||
this._controlPermissionsUpdated(controlConnection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_controlPermissionsUpdated(controlConnection: ControlConnection) {
|
||||
const user = controlConnection.user;
|
||||
this.subscriptions.checkLinkPermissions(controlConnection, user);
|
||||
controlConnection.checkLogPermission();
|
||||
controlConnection.send(
|
||||
new lib.AccountUpdateEvent([...user.roles].map(r => ({
|
||||
name: r.name,
|
||||
id: r.id,
|
||||
permissions: [...r.permissions],
|
||||
})))
|
||||
);
|
||||
}
|
||||
|
||||
rolesUpdated(roles: lib.Role[]) {
|
||||
this.subscriptions.broadcast(new lib.RoleUpdatesEvent(roles));
|
||||
this.hooks.rolesUpdated.invoke(roles);
|
||||
@@ -1105,15 +1113,7 @@ export default class Controller {
|
||||
for (const role of roles) {
|
||||
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||
if (controlConnection.user.roles.has(role)) {
|
||||
controlConnection.send(
|
||||
new lib.AccountUpdateEvent(
|
||||
[...controlConnection.user.roles].map(r => ({
|
||||
name: r.name,
|
||||
id: r.id,
|
||||
permissions: [...r.permissions],
|
||||
}))
|
||||
)
|
||||
);
|
||||
this._controlPermissionsUpdated(controlConnection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,6 +355,32 @@ export class SubscriptionController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the subscriptions of a link against the permissions of a user,
|
||||
* removing those the user no longer has permission for.
|
||||
* Used when the permissions of a user changes.
|
||||
* @param link - Link to check the subscriptions of.
|
||||
* @param user - User the link is authenticated as.
|
||||
*/
|
||||
checkLinkPermissions(link: Link, user: IUser) {
|
||||
for (const [eventName, eventData] of this._events) {
|
||||
for (const [addressIndex, subscriber] of eventData.subscriptions) {
|
||||
if (subscriber.link !== link) {
|
||||
continue;
|
||||
}
|
||||
const request = new SubscriptionRequest(eventName, "subscribe", 0, subscriber.filters);
|
||||
const message = new MessageRequest(
|
||||
0, subscriber.address, new Address(Address.controller, 0), "SubscriptionRequest", request.toJSON(),
|
||||
);
|
||||
try {
|
||||
SubscriptionRequest.permission(user, message);
|
||||
} catch {
|
||||
eventData.subscriptions.delete(addressIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming subscription requests on a link
|
||||
* @param link - Link message was received on
|
||||
|
||||
Reference in new issue
Block a user