mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
* chore: Rename APIContext to Context and flatten its fields Auth and transaction now live directly on the context instead of in ctx.state and a nested ctx.context, and model helpers read them directly. APIContext remains as a deprecated alias. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove deprecated APIContext alias Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * fix: Read auth from context in OAuth provider callbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Add withUser and withTransaction context helpers Replaces manual createContext rebuilds that copy fields from an existing context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf * chore: Remove arbitrary property access from Koa context types AppContext and Context no longer inherit the any index signature from Koa's DefaultContext, custom fields are declared explicitly, and importing Koa's loose Context type is disallowed by lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf --------- Co-authored-by: Claude <noreply@anthropic.com>
133 lines
3.3 KiB
TypeScript
133 lines
3.3 KiB
TypeScript
import { partition, uniqBy } from "es-toolkit/compat";
|
|
import { UserRole } from "@shared/types";
|
|
import InviteEmail from "@server/emails/templates/InviteEmail";
|
|
import env from "@server/env";
|
|
import Logger from "@server/logging/Logger";
|
|
import { User, Team } from "@server/models";
|
|
import { UserFlag } from "@server/models/User";
|
|
import type { Context } from "@server/types";
|
|
import { DomainNotAllowedError } from "@server/errors";
|
|
import { authorize, can } from "@server/policies";
|
|
|
|
export type Invite = {
|
|
name: string;
|
|
email: string;
|
|
role: UserRole;
|
|
};
|
|
|
|
type Props = {
|
|
invites: Invite[];
|
|
suppressEmail?: boolean;
|
|
};
|
|
|
|
export default async function userInviter(
|
|
ctx: Context,
|
|
{ invites, suppressEmail }: Props
|
|
): Promise<{
|
|
sent: Invite[];
|
|
unsent: Invite[];
|
|
users: User[];
|
|
}> {
|
|
const { user } = ctx.auth;
|
|
const team = await Team.findByPk(user.teamId, { rejectOnEmpty: true });
|
|
|
|
if (invites.some((invite) => invite.role === UserRole.Guest)) {
|
|
authorize(user, "addGuest", team);
|
|
}
|
|
|
|
// filter out empties and obvious non-emails
|
|
const compactedInvites = invites.filter(
|
|
(invite) => !!invite.email.trim() && invite.email.match("@")
|
|
);
|
|
// normalize to lowercase and remove duplicates
|
|
const normalizedInvites = uniqBy(
|
|
compactedInvites.map((invite) => ({
|
|
...invite,
|
|
email: invite.email.toLowerCase(),
|
|
})),
|
|
"email"
|
|
);
|
|
// filter out any existing users in the system
|
|
const emails = normalizedInvites.map((invite) => invite.email);
|
|
|
|
if (!can(user, "update", team)) {
|
|
for (const email of emails) {
|
|
if (!(await team.isDomainAllowed(email))) {
|
|
throw DomainNotAllowedError();
|
|
}
|
|
}
|
|
}
|
|
|
|
const existingUsers = await User.findAll({
|
|
where: {
|
|
teamId: user.teamId,
|
|
email: emails,
|
|
},
|
|
});
|
|
const existingEmails = existingUsers.map(
|
|
(existingUser) => existingUser.email
|
|
);
|
|
const [existingInvites, filteredInvites] = partition(
|
|
normalizedInvites,
|
|
(invite) => existingEmails.includes(invite.email)
|
|
);
|
|
const users = [];
|
|
|
|
// send and record remaining invites
|
|
for (const invite of filteredInvites) {
|
|
const newUser = await User.createWithCtx(
|
|
ctx,
|
|
{
|
|
teamId: user.teamId,
|
|
name: invite.name,
|
|
email: invite.email,
|
|
role:
|
|
invite.role === UserRole.Guest
|
|
? UserRole.Guest
|
|
: user.isAdmin && invite.role === UserRole.Admin
|
|
? UserRole.Admin
|
|
: user.isViewer || invite.role === UserRole.Viewer
|
|
? UserRole.Viewer
|
|
: UserRole.Member,
|
|
invitedById: user.id,
|
|
flags: suppressEmail
|
|
? undefined
|
|
: {
|
|
[UserFlag.InviteSent]: 1,
|
|
},
|
|
},
|
|
{
|
|
name: "invite",
|
|
}
|
|
);
|
|
users.push(newUser);
|
|
|
|
if (!suppressEmail) {
|
|
await new InviteEmail({
|
|
to: invite.email,
|
|
language: newUser.language,
|
|
name: invite.name,
|
|
actorName: user.name,
|
|
actorEmail: user.email,
|
|
teamName: team.name,
|
|
teamUrl: team.url,
|
|
}).schedule();
|
|
}
|
|
|
|
if (env.isDevelopment) {
|
|
Logger.info(
|
|
"email",
|
|
`Sign in immediately: ${
|
|
env.URL
|
|
}/auth/email.callback?token=${newUser.getEmailSigninToken(ctx)}`
|
|
);
|
|
}
|
|
}
|
|
|
|
return {
|
|
sent: filteredInvites,
|
|
unsent: existingInvites,
|
|
users,
|
|
};
|
|
}
|