mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* fix: Require an enabled authentication provider before disabling email sign-in * test: Cover teams with only disabled providers
134 lines
3.9 KiB
TypeScript
134 lines
3.9 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 {
|
|
AuthenticationProvider,
|
|
Collection,
|
|
Team,
|
|
TeamDomain,
|
|
} from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
import type { Context } from "@server/types";
|
|
|
|
type Props = {
|
|
params: Partial<Omit<Team, "allowedDomains">> & { allowedDomains?: string[] };
|
|
user: User;
|
|
team: Team;
|
|
};
|
|
|
|
const teamUpdater = async (ctx: Context, { params, user, team }: Props) => {
|
|
const { allowedDomains, preferences, subdomain, ...attributes } = params;
|
|
team.fill(attributes);
|
|
|
|
// The caller's lock on the team row serializes this with provider disabling.
|
|
if (team.changed("guestSignin") && !team.guestSignin) {
|
|
const enabledProvider = await AuthenticationProvider.findOne({
|
|
attributes: ["id"],
|
|
where: { teamId: team.id, enabled: true },
|
|
transaction: ctx.transaction,
|
|
});
|
|
|
|
if (!enabledProvider) {
|
|
throw ValidationError("At least one authentication provider is required");
|
|
}
|
|
}
|
|
|
|
if (attributes.defaultCollectionId) {
|
|
const collection = await Collection.findOne({
|
|
attributes: ["id"],
|
|
where: {
|
|
id: attributes.defaultCollectionId,
|
|
teamId: team.id,
|
|
ownerId: null,
|
|
},
|
|
transaction: ctx.transaction,
|
|
});
|
|
|
|
if (!collection) {
|
|
throw ValidationError("Default collection not found");
|
|
}
|
|
}
|
|
|
|
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.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.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) {
|
|
const shareExpiryDays = preferences[TeamPreference.DefaultShareExpiryDays];
|
|
|
|
// Removing the default stays possible after losing the entitlement.
|
|
if (
|
|
shareExpiryDays &&
|
|
shareExpiryDays !==
|
|
team.getPreference(TeamPreference.DefaultShareExpiryDays)
|
|
) {
|
|
authorize(user, "setDefaultShareExpiry", team);
|
|
}
|
|
|
|
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;
|