mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
* shares.info, collections.info, documents.info * shares.list, shares.create, shares.update * shares.sitemap * parity with existing document shared screen * collection share popover * parent share and table * collection scene * collection link in sidebar * sidebar and breadcrumb collection link click * collection link click in editor * meta * more meta + 404 page * map internal link, remove showLastUpdated option * fix shares.list pagination * show last updated * shareLoader tests * lint * sidebar context for collection link * badge in shares table * fix existing tests * tsc * update failing test snapshot * env * signed url for collection attachments * include collection content in SSR for screen readers * search * drafts can be shared * review * tsc, remove old shared-doc scene * tweaks * DRY * refactor loader * Remove share/collection urls * fix: Collection overview should not be editable when viewing shared link and logged in * Tweak public breadcrumb * fix: Deleted documents should never be exposed through share * empty sharedTree array where includeChildDocuments is false * revert includeChildDocs guard for logical correctness + SSR bug fix * fix: check document is part of share --------- Co-authored-by: Tom Moor <tom@getoutline.com>
229 lines
6.2 KiB
TypeScript
229 lines
6.2 KiB
TypeScript
import { Op, WhereOptions } from "sequelize";
|
|
import isUUID from "validator/lib/isUUID";
|
|
import { UrlHelper } from "@shared/utils/UrlHelper";
|
|
import {
|
|
NotFoundError,
|
|
InvalidRequestError,
|
|
AuthorizationError,
|
|
AuthenticationError,
|
|
PaymentRequiredError,
|
|
} from "@server/errors";
|
|
import { Collection, Document, Share, User, Team } from "@server/models";
|
|
import { authorize, can } from "@server/policies";
|
|
|
|
type Props = {
|
|
id?: string;
|
|
shareId?: string;
|
|
teamId?: string;
|
|
user?: User;
|
|
includeState?: boolean;
|
|
};
|
|
|
|
type Result = {
|
|
document: Document;
|
|
share: Share | null;
|
|
collection: Collection | null;
|
|
};
|
|
|
|
export default async function loadDocument({
|
|
id,
|
|
shareId,
|
|
teamId,
|
|
user,
|
|
includeState,
|
|
}: Props): Promise<Result> {
|
|
let document: Document | null = null;
|
|
let collection: Collection | null = null;
|
|
let share: Share | null = null;
|
|
|
|
if (!shareId && !(id && user)) {
|
|
throw AuthenticationError(`Authentication or shareId required`);
|
|
}
|
|
|
|
const shareUrlId =
|
|
shareId && !isUUID(shareId) && UrlHelper.SHARE_URL_SLUG_REGEX.test(shareId)
|
|
? shareId
|
|
: undefined;
|
|
|
|
if (shareUrlId && !teamId) {
|
|
throw InvalidRequestError(
|
|
"teamId required for fetching share using shareUrlId"
|
|
);
|
|
}
|
|
|
|
if (shareId) {
|
|
let whereClause: WhereOptions<Share> = {
|
|
revokedAt: {
|
|
[Op.is]: null,
|
|
},
|
|
id: shareId,
|
|
};
|
|
if (shareUrlId) {
|
|
whereClause = {
|
|
revokedAt: {
|
|
[Op.is]: null,
|
|
},
|
|
teamId,
|
|
urlId: shareUrlId,
|
|
};
|
|
}
|
|
share = await Share.findOne({
|
|
where: whereClause,
|
|
include: [
|
|
{
|
|
model: Document.scope("withDrafts"),
|
|
as: "document",
|
|
},
|
|
{
|
|
model: Collection.scope("withDocumentStructure"),
|
|
as: "collection",
|
|
},
|
|
],
|
|
});
|
|
|
|
if (!share || share.collection?.archivedAt || share.document?.archivedAt) {
|
|
throw InvalidRequestError("Document could not be found for shareId");
|
|
}
|
|
|
|
// It is possible to pass both an id and a shareId to the documents.info
|
|
// endpoint. In this case we'll load the document based on the `id` and check
|
|
// if the provided share token allows access. This is used by the frontend
|
|
// to navigate nested documents from a single share link.
|
|
if (id) {
|
|
document = await Document.findByPk(id, {
|
|
userId: user ? user.id : undefined,
|
|
paranoid: false,
|
|
}); // otherwise, if the user has an authenticated session make sure to load
|
|
// with their details so that we can return the correct policies, they may
|
|
// be able to edit the shared document
|
|
} else if (user && share.documentId) {
|
|
document = await Document.findByPk(share.documentId, {
|
|
userId: user.id,
|
|
paranoid: false,
|
|
});
|
|
} else {
|
|
document = share.document;
|
|
}
|
|
|
|
if (!document) {
|
|
throw NotFoundError("Document could not be found for shareId");
|
|
}
|
|
|
|
if (document.isTrialImport) {
|
|
throw PaymentRequiredError();
|
|
}
|
|
|
|
// If the user has access to read the document, we can just update
|
|
// the last access date and return the document without additional checks.
|
|
const canReadDocument = user && can(user, "read", document);
|
|
|
|
if (canReadDocument) {
|
|
if (document.collectionId) {
|
|
collection = await Collection.findByPk(document.collectionId, {
|
|
includeDocumentStructure: true,
|
|
rejectOnEmpty: true,
|
|
});
|
|
}
|
|
|
|
return {
|
|
document,
|
|
share,
|
|
collection,
|
|
};
|
|
}
|
|
|
|
// "published" === on the public internet.
|
|
// We already know that there's either no logged in user or the user doesn't
|
|
// have permission to read the document, so we can throw an error.
|
|
if (!share.published) {
|
|
throw AuthorizationError();
|
|
}
|
|
|
|
// It is possible to disable sharing at the collection so we must check
|
|
if (document.collectionId) {
|
|
collection = await Collection.findByPk(document.collectionId, {
|
|
includeDocumentStructure: true,
|
|
rejectOnEmpty: true,
|
|
});
|
|
|
|
if (!collection.sharing) {
|
|
throw AuthorizationError();
|
|
}
|
|
}
|
|
|
|
if (share.collectionId) {
|
|
// If this is a collection share, we need to ensure that
|
|
// the document is within the collection.
|
|
const childDocumentIds = share.collection?.getAllDocumentIds() ?? [];
|
|
if (!childDocumentIds.includes(document.id)) {
|
|
throw AuthorizationError();
|
|
}
|
|
} else if (share.documentId !== document.id) {
|
|
// If we're attempting to load a document that isn't the document originally
|
|
// shared then includeChildDocuments must be enabled and the document must
|
|
// still be active and nested within the shared document
|
|
if (!share.includeChildDocuments) {
|
|
throw AuthorizationError();
|
|
}
|
|
|
|
// If the document is not a direct child of the shared document then we
|
|
// need to check if it is nested within the shared document somewhere.
|
|
if (document.parentDocumentId !== share.documentId) {
|
|
const childDocumentIds =
|
|
(await share.document?.findAllChildDocumentIds({
|
|
archivedAt: {
|
|
[Op.is]: null,
|
|
},
|
|
})) ?? [];
|
|
|
|
if (!childDocumentIds.includes(document.id)) {
|
|
throw AuthorizationError();
|
|
}
|
|
}
|
|
}
|
|
|
|
// It is possible to disable sharing at the team level so we must check
|
|
const team = await Team.findByPk(document.teamId, { rejectOnEmpty: true });
|
|
|
|
if (team.suspendedAt) {
|
|
throw NotFoundError();
|
|
}
|
|
if (!team.sharing) {
|
|
throw AuthorizationError();
|
|
}
|
|
} else {
|
|
document = await Document.findByPk(id as string, {
|
|
userId: user ? user.id : undefined,
|
|
paranoid: false,
|
|
includeState,
|
|
});
|
|
|
|
if (!document) {
|
|
throw NotFoundError();
|
|
}
|
|
|
|
if (document.deletedAt) {
|
|
// don't send data if user cannot restore deleted doc
|
|
if (user) {
|
|
authorize(user, "restore", document);
|
|
}
|
|
} else {
|
|
if (user) {
|
|
authorize(user, "read", document);
|
|
}
|
|
}
|
|
|
|
if (document.isTrialImport) {
|
|
throw PaymentRequiredError();
|
|
}
|
|
|
|
collection = document.collection;
|
|
}
|
|
|
|
return {
|
|
document,
|
|
share,
|
|
collection,
|
|
};
|
|
}
|