mirror of
https://github.com/outline/outline.git
synced 2026-12-03 18:10:36 +03:00
* fix: Make count-based limit checks reliable under concurrency Limits enforced by counting rows before an insert could be exceeded by requests that ran at the same time, as each one read the same count before any of them had written. Add a LockHelper that takes a Postgres advisory lock for the remainder of the transaction, and use it to serialize the check for webhook subscriptions, team domains, share subscriptions and pins. The counts now also run on the transaction so they include rows written earlier in the same request. * Remove excessive tests * Also guard simultaneous row removal
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import { has, isEqual } from "es-toolkit/compat";
|
|
import { TeamPreference } from "@shared/types";
|
|
import env from "@server/env";
|
|
import { ValidationError } from "@server/errors";
|
|
import type { User } from "@server/models";
|
|
import { Team, TeamDomain } from "@server/models";
|
|
import type { APIContext } from "@server/types";
|
|
|
|
type Props = {
|
|
params: Partial<Omit<Team, "allowedDomains">> & { allowedDomains?: string[] };
|
|
user: User;
|
|
team: Team;
|
|
};
|
|
|
|
const teamUpdater = async (ctx: APIContext, { params, user, team }: Props) => {
|
|
const { allowedDomains, preferences, subdomain, ...attributes } = params;
|
|
team.setAttributes(attributes);
|
|
|
|
if (subdomain !== undefined && env.isCloudHosted) {
|
|
const newSubdomain = subdomain === "" ? null : subdomain;
|
|
|
|
if (newSubdomain && newSubdomain !== team.subdomain) {
|
|
const existing = await Team.findOne({
|
|
where: { subdomain: newSubdomain },
|
|
paranoid: false,
|
|
transaction: ctx.state.transaction,
|
|
});
|
|
|
|
if (existing) {
|
|
throw ValidationError("Subdomain is already in use");
|
|
}
|
|
}
|
|
|
|
team.subdomain = newSubdomain;
|
|
}
|
|
|
|
if (allowedDomains !== undefined) {
|
|
const existingAllowedDomains = await TeamDomain.findAll({
|
|
where: { teamId: team.id },
|
|
transaction: ctx.state.transaction,
|
|
});
|
|
|
|
// Only keep existing domains if they are still in the list of allowed domains
|
|
const newAllowedDomains = team.allowedDomains.filter((existingTeamDomain) =>
|
|
allowedDomains.includes(existingTeamDomain.name)
|
|
);
|
|
|
|
// Add new domains
|
|
const existingDomains = team.allowedDomains.map((x) => x.name);
|
|
const newDomains = allowedDomains.filter(
|
|
(newDomain) => newDomain !== "" && !existingDomains.includes(newDomain)
|
|
);
|
|
// Created one at a time so that the limit check on each create counts the
|
|
// domains added earlier in the same request.
|
|
for (const newDomain of newDomains) {
|
|
newAllowedDomains.push(
|
|
await TeamDomain.createWithCtx(ctx, {
|
|
name: newDomain,
|
|
teamId: team.id,
|
|
createdById: user.id,
|
|
})
|
|
);
|
|
}
|
|
|
|
// Destroy the existing TeamDomains that were removed
|
|
const deletedDomains = existingAllowedDomains.filter(
|
|
(x) => !allowedDomains.includes(x.name)
|
|
);
|
|
await Promise.all(deletedDomains.map((x) => x.destroyWithCtx(ctx)));
|
|
team.allowedDomains = newAllowedDomains;
|
|
}
|
|
|
|
if (preferences) {
|
|
for (const key of Object.values(TeamPreference)) {
|
|
if (
|
|
has(preferences, key) &&
|
|
!isEqual(preferences[key], team.getPreference(key))
|
|
) {
|
|
team.setPreference(key, preferences[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return team.saveWithCtx(ctx);
|
|
};
|
|
|
|
export default teamUpdater;
|