2023-10-21 10:00:24 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2023-07-16 18:42:42 +02:00
|
|
|
import { ModelType } from '../../BaseModel';
|
|
|
|
import { ErrorCode } from '../../errors';
|
|
|
|
import JoplinError from '../../JoplinError';
|
|
|
|
import { State as ShareState } from '../../services/share/reducer';
|
|
|
|
import ItemChange from '../ItemChange';
|
|
|
|
import Setting from '../Setting';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { checkObjectHasProperties } from '@joplin/utils/object';
|
2023-07-16 18:42:42 +02:00
|
|
|
|
2023-10-21 10:00:24 +02:00
|
|
|
const logger = Logger.create('models/utils/readOnly');
|
|
|
|
|
2023-07-16 18:42:42 +02:00
|
|
|
export interface ItemSlice {
|
|
|
|
id?: string;
|
|
|
|
share_id: string;
|
2024-03-02 16:25:27 +02:00
|
|
|
deleted_time: number;
|
2023-07-16 18:42:42 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
// This function can be called to wrap code that related to share permission read-only checks. It
|
|
|
|
// should be fast and allows an early exit for cases that don't apply, for example if not
|
2023-07-16 18:42:42 +02:00
|
|
|
// synchronising with Joplin Cloud or if not sharing any notebook.
|
2024-03-02 16:25:27 +02:00
|
|
|
export const needsShareReadOnlyChecks = (itemType: ModelType, changeSource: number, shareState: ShareState, disableReadOnlyCheck = false) => {
|
2023-07-23 16:57:55 +02:00
|
|
|
if (disableReadOnlyCheck) return false;
|
2023-07-16 18:42:42 +02:00
|
|
|
if (Setting.value('sync.target') !== 10) return false;
|
|
|
|
if (changeSource === ItemChange.SOURCE_SYNC) return false;
|
|
|
|
if (!Setting.value('sync.userId')) return false;
|
|
|
|
if (![ModelType.Note, ModelType.Folder, ModelType.Resource].includes(itemType)) return false;
|
|
|
|
|
|
|
|
if (!shareState) throw new Error('Share state must be provided');
|
|
|
|
if (!shareState.shareInvitations.length) return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkIfItemsCanBeChanged = (itemType: ModelType, changeSource: number, items: ItemSlice[], shareState: ShareState) => {
|
|
|
|
for (const item of items) {
|
|
|
|
checkIfItemCanBeChanged(itemType, changeSource, item, shareState);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkIfItemCanBeChanged = (itemType: ModelType, changeSource: number, item: ItemSlice, shareState: ShareState) => {
|
2024-03-02 16:25:27 +02:00
|
|
|
if (!needsShareReadOnlyChecks(itemType, changeSource, shareState)) return;
|
2023-07-16 18:42:42 +02:00
|
|
|
if (!item) return;
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
if (itemIsReadOnlySync(itemType, changeSource, item, Setting.value('sync.userId'), shareState, true)) {
|
2023-07-16 18:42:42 +02:00
|
|
|
throw new JoplinError(`Cannot change or delete a read-only item: ${item.id}`, ErrorCode.IsReadOnly);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkIfItemCanBeAddedToFolder = async (itemType: ModelType, Folder: any, changeSource: number, shareState: ShareState, parentId: string) => {
|
2024-03-02 16:25:27 +02:00
|
|
|
if (needsShareReadOnlyChecks(itemType, changeSource, shareState) && parentId) {
|
2023-07-16 18:42:42 +02:00
|
|
|
const parentFolder = await Folder.load(parentId, { fields: ['id', 'share_id'] });
|
2023-10-21 10:00:24 +02:00
|
|
|
|
|
|
|
if (!parentFolder) {
|
|
|
|
// Historically it's always been possible to set the parent_id of a
|
|
|
|
// note to a folder that does not exist - this is to support
|
|
|
|
// synchronisation, where items are downloaded in random order. It
|
|
|
|
// is not ideal to skip the check here, but if for some reason the
|
|
|
|
// folder turns out to be read-only the issue will be resolved
|
|
|
|
// during sync.
|
|
|
|
logger.warn('checkIfItemCanBeAddedToFolder: Trying to add an item to a folder that does not exist - skipping check');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
if (itemIsReadOnlySync(itemType, changeSource, parentFolder, Setting.value('sync.userId'), shareState, true)) {
|
2023-07-16 18:42:42 +02:00
|
|
|
throw new JoplinError('Cannot add an item as a child of a read-only item', ErrorCode.IsReadOnly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
// Originally all these functions were there to handle share permissions - a note, folder or
|
|
|
|
// resource that is not editable would be read-only. However this particular function now is also
|
|
|
|
// used to tell if a note is read-only because it is in the trash.
|
|
|
|
//
|
|
|
|
// But this requires access to more properties, `deleted_time` in particular, which are not needed
|
|
|
|
// for share-related checks (and does not exist on Resource objects). So this is why there's this
|
|
|
|
// extra `sharePermissionCheckOnly` boolean to do the check for one case or the other. A bit of a
|
|
|
|
// hack but good enough for now.
|
|
|
|
export const itemIsReadOnlySync = (itemType: ModelType, changeSource: number, item: ItemSlice, userId: string, shareState: ShareState, sharePermissionCheckOnly = false): boolean => {
|
|
|
|
checkObjectHasProperties(item, sharePermissionCheckOnly ? ['share_id'] : ['share_id', 'deleted_time']);
|
2023-07-16 18:42:42 +02:00
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
// Item is in trash
|
|
|
|
if (!sharePermissionCheckOnly && item.deleted_time) return true;
|
|
|
|
|
|
|
|
if (!needsShareReadOnlyChecks(itemType, changeSource, shareState)) return false;
|
2023-07-16 18:42:42 +02:00
|
|
|
|
|
|
|
// Item is not shared
|
|
|
|
if (!item.share_id) return false;
|
|
|
|
|
|
|
|
// Item belongs to the user
|
|
|
|
if (shareState.shares.find(s => s.user.id === userId)) return false;
|
|
|
|
|
|
|
|
const shareUser = shareState.shareInvitations.find(si => si.share.id === item.share_id);
|
|
|
|
|
|
|
|
// Shouldn't happen
|
|
|
|
if (!shareUser) return false;
|
|
|
|
|
|
|
|
return !shareUser.can_write;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const itemIsReadOnly = async (BaseItem: any, itemType: ModelType, changeSource: number, itemId: string, userId: string, shareState: ShareState): Promise<boolean> => {
|
2024-03-02 16:25:27 +02:00
|
|
|
// if (!needsShareReadOnlyChecks(itemType, changeSource, shareState)) return false;
|
|
|
|
const item: ItemSlice = await BaseItem.loadItem(itemType, itemId, { fields: ['id', 'share_id', 'deleted_time'] });
|
2023-07-16 18:42:42 +02:00
|
|
|
if (!item) throw new JoplinError(`No such item: ${itemType}: ${itemId}`, ErrorCode.NotFound);
|
|
|
|
return itemIsReadOnlySync(itemType, changeSource, item, userId, shareState);
|
|
|
|
};
|