Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Implement authorization checks for nodes
Hakan Shehuaa8e83145460
diff --git a/desktop/src/main/crdt.ts b/desktop/src/main/crdt.tsindex 759fc924..471e5971 100644--- a/desktop/src/main/crdt.ts+++ b/desktop/src/main/crdt.ts@@ -9,13 +9,11 @@ export const applyCrdt = (attributes: z.infer<typeof schema>,attributesMap: Y.Map<any>,) => {- const parsedAttributes = schema.parse(attributes);-if (!(schema instanceof z.ZodObject)) {throw new Error('Schema must be a ZodObject');}- applyObjectChanges(schema, parsedAttributes, attributesMap);+ applyObjectChanges(schema, attributes, attributesMap);const parseResult = schema.safeParse(attributesMap.toJSON());if (!parseResult.success) {diff --git a/desktop/src/main/node-manager.ts b/desktop/src/main/node-manager.tsindex f8cdb177..63d88730 100644--- a/desktop/src/main/node-manager.ts+++ b/desktop/src/main/node-manager.ts@@ -6,7 +6,7 @@ import {LocalCreateNodeChangeData,LocalUpdateNodeChangeData,} from '@/types/sync';-import { registryMap } from '@/registry';+import { registry } from '@/registry';import { generateId } from '@/lib/id';import { IdType } from '@/lib/id';import { databaseManager } from '@/main/data/database-manager';@@ -26,12 +26,16 @@ class NodeManager {});const attributesMap = doc.getMap('attributes');- const registry = registryMap[attributes.type];- if (!registry) {+ const model = registry[attributes.type];+ if (!model) {throw new Error('Invalid node type');}- applyCrdt(registry.schema, attributes, attributesMap);+ if (!model.schema.safeParse(attributes).success) {+ throw new Error('Invalid attributes');+ }++ applyCrdt(model.schema, attributes, attributesMap);const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));const createdAt = new Date().toISOString();@@ -107,7 +111,7 @@ class NodeManager {const attributesMap = doc.getMap('attributes');doc.transact(() => {applyCrdt(- registryMap[updatedAttributes.type].schema,+ registry[updatedAttributes.type].schema,updatedAttributes,attributesMap,);diff --git a/desktop/src/registry/channel.ts b/desktop/src/registry/channel.tsindex ce4cdfb8..8228fc4a 100644--- a/desktop/src/registry/channel.ts+++ b/desktop/src/registry/channel.ts@@ -1,5 +1,6 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';+import { isEqual } from 'lodash';export const channelAttributesSchema = z.object({type: z.literal('channel'),@@ -12,7 +13,42 @@ export const channelAttributesSchema = z.object({export type ChannelAttributes = z.infer<typeof channelAttributesSchema>;-export const ChannelRegistry: NodeRegistry = {+export const channelModel: NodeModel = {type: 'channel',schema: channelAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'channel') {+ return false;+ }++ if (context.ancestors.length !== 1) {+ return false;+ }++ const parent = context.ancestors[0];+ if (parent.type !== 'space') {+ return false;+ }++ const collaboratorIds = Object.keys(attributes.collaborators ?? {});+ if (collaboratorIds.length > 0 && !context.hasAdminAccess()) {+ return false;+ }++ return context.hasEditorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'channel' || node.type !== 'channel') {+ return false;+ }++ if (!isEqual(node.attributes.collaborators, attributes.collaborators)) {+ return context.hasAdminAccess();+ }++ return context.hasEditorAccess();+ },+ canDelete: async (context, node) => {+ return context.hasEditorAccess();+ },};diff --git a/desktop/src/registry/chat.ts b/desktop/src/registry/chat.tsindex adf777b2..f7457d5a 100644--- a/desktop/src/registry/chat.ts+++ b/desktop/src/registry/chat.ts@@ -1,5 +1,5 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';export const chatAttributesSchema = z.object({type: z.literal('chat'),@@ -9,7 +9,29 @@ export const chatAttributesSchema = z.object({export type ChatAttributes = z.infer<typeof chatAttributesSchema>;-export const ChatRegistry: NodeRegistry = {+export const chatModel: NodeModel = {type: 'chat',schema: chatAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'chat') {+ return false;+ }++ const collaboratorIds = Object.keys(attributes.collaborators ?? {});+ if (collaboratorIds.length !== 2) {+ return false;+ }++ if (!collaboratorIds.includes(context.userId)) {+ return false;+ }++ return true;+ },+ canUpdate: async () => {+ return false;+ },+ canDelete: async () => {+ return false;+ },};diff --git a/desktop/src/registry/core.ts b/desktop/src/registry/core.tsindex b046deec..66e3b422 100644--- a/desktop/src/registry/core.ts+++ b/desktop/src/registry/core.ts@@ -1,6 +1,31 @@import { ZodSchema } from 'zod';+import { Node, NodeAttributes } from '@/registry';-export interface NodeRegistry {+export type NodeRole = 'admin' | 'editor' | 'collaborator' | 'viewer';++export interface NodeMutationContext {+ accountId: string;+ workspaceId: string;+ userId: string;+ ancestors: Node[];+ role: NodeRole;+ hasAdminAccess: () => boolean;+ hasEditorAccess: () => boolean;+ hasCollaboratorAccess: () => boolean;+ hasViewerAccess: () => boolean;+}++export interface NodeModel {type: string;schema: ZodSchema;+ canCreate: (+ context: NodeMutationContext,+ attributes: NodeAttributes,+ ) => Promise<boolean>;+ canUpdate: (+ context: NodeMutationContext,+ node: Node,+ attributes: NodeAttributes,+ ) => Promise<boolean>;+ canDelete: (context: NodeMutationContext, node: Node) => Promise<boolean>;}diff --git a/desktop/src/registry/database.ts b/desktop/src/registry/database.tsindex e3f6a618..78e26faf 100644--- a/desktop/src/registry/database.ts+++ b/desktop/src/registry/database.ts@@ -1,6 +1,7 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';import { fieldAttributesSchema } from '@/registry/fields';+import { isEqual } from 'lodash';export const viewFieldAttributesSchema = z.object({id: z.string(),@@ -78,7 +79,33 @@ export const databaseAttributesSchema = z.object({export type DatabaseAttributes = z.infer<typeof databaseAttributesSchema>;-export const DatabaseRegistry: NodeRegistry = {+export const databaseModel: NodeModel = {type: 'database',schema: databaseAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'database') {+ return false;+ }++ const collaboratorIds = Object.keys(attributes.collaborators ?? {});+ if (collaboratorIds.length > 0 && !context.hasAdminAccess()) {+ return false;+ }++ return context.hasEditorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'database' || node.type !== 'database') {+ return false;+ }++ if (!isEqual(node.attributes.collaborators, attributes.collaborators)) {+ return context.hasAdminAccess();+ }++ return context.hasEditorAccess();+ },+ canDelete: async (context, node) => {+ return context.hasEditorAccess();+ },};diff --git a/desktop/src/registry/file.ts b/desktop/src/registry/file.tsindex 18cad4b5..c82c1a78 100644--- a/desktop/src/registry/file.ts+++ b/desktop/src/registry/file.ts@@ -1,5 +1,5 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';export const fileAttributesSchema = z.object({type: z.literal('file'),@@ -13,7 +13,16 @@ export const fileAttributesSchema = z.object({export type FileAttributes = z.infer<typeof fileAttributesSchema>;-export const FileRegistry: NodeRegistry = {+export const fileModel: NodeModel = {type: 'file',schema: fileAttributesSchema,+ canCreate: async (context, attributes) => {+ return true;+ },+ canUpdate: async (context, node, attributes) => {+ return true;+ },+ canDelete: async (context, node) => {+ return true;+ },};diff --git a/desktop/src/registry/folder.ts b/desktop/src/registry/folder.tsindex 99ef9ff4..2ff3001d 100644--- a/desktop/src/registry/folder.ts+++ b/desktop/src/registry/folder.ts@@ -1,5 +1,6 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';+import { isEqual } from 'lodash';export const folderAttributesSchema = z.object({type: z.literal('folder'),@@ -11,7 +12,33 @@ export const folderAttributesSchema = z.object({export type FolderAttributes = z.infer<typeof folderAttributesSchema>;-export const FolderRegistry: NodeRegistry = {+export const folderModel: NodeModel = {type: 'folder',schema: folderAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'folder') {+ return false;+ }++ const collaboratorIds = Object.keys(attributes.collaborators ?? {});+ if (collaboratorIds.length > 0 && !context.hasAdminAccess()) {+ return false;+ }++ return context.hasEditorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'folder' || node.type !== 'folder') {+ return false;+ }++ if (!isEqual(node.attributes.collaborators, attributes.collaborators)) {+ return context.hasAdminAccess();+ }++ return context.hasEditorAccess();+ },+ canDelete: async (context, node) => {+ return context.hasEditorAccess();+ },};diff --git a/desktop/src/registry/index.ts b/desktop/src/registry/index.tsindex 1f4b0400..da2e1f69 100644--- a/desktop/src/registry/index.ts+++ b/desktop/src/registry/index.ts@@ -1,14 +1,14 @@-import { ChannelAttributes, ChannelRegistry } from '@/registry/channel';-import { NodeRegistry } from '@/registry/core';-import { PageAttributes, PageRegistry } from '@/registry/page';-import { ChatAttributes, ChatRegistry } from '@/registry/chat';-import { SpaceAttributes, SpaceRegistry } from '@/registry/space';-import { UserAttributes, UserRegistry } from '@/registry/user';-import { MessageAttributes, MessageRegistry } from '@/registry/message';-import { DatabaseAttributes, DatabaseRegistry } from '@/registry/database';-import { FileAttributes, FileRegistry } from '@/registry/file';-import { FolderAttributes, FolderRegistry } from '@/registry/folder';-import { RecordAttributes, RecordRegistry } from '@/registry/record';+import { ChannelAttributes, channelModel } from '@/registry/channel';+import { NodeModel } from '@/registry/core';+import { PageAttributes, pageModel } from '@/registry/page';+import { ChatAttributes, chatModel } from '@/registry/chat';+import { SpaceAttributes, spaceModel } from '@/registry/space';+import { UserAttributes, userModel } from '@/registry/user';+import { MessageAttributes, messageModel } from '@/registry/message';+import { DatabaseAttributes, databaseModel } from '@/registry/database';+import { FileAttributes, fileModel } from '@/registry/file';+import { FolderAttributes, folderModel } from '@/registry/folder';+import { RecordAttributes, recordModel } from '@/registry/record';export * from '@/registry/channel';export * from '@/registry/core';@@ -24,17 +24,17 @@ export * from '@/registry/record';export * from '@/registry/block';export * from '@/registry/fields';-export const registryMap: Record<string, NodeRegistry> = {- channel: ChannelRegistry,- chat: ChatRegistry,- database: DatabaseRegistry,- file: FileRegistry,- folder: FolderRegistry,- message: MessageRegistry,- page: PageRegistry,- record: RecordRegistry,- space: SpaceRegistry,- user: UserRegistry,+export const registry: Record<string, NodeModel> = {+ channel: channelModel,+ chat: chatModel,+ database: databaseModel,+ file: fileModel,+ folder: folderModel,+ message: messageModel,+ page: pageModel,+ record: recordModel,+ space: spaceModel,+ user: userModel,};type NodeBase = {diff --git a/desktop/src/registry/message.ts b/desktop/src/registry/message.tsindex 29c8f967..45ac8f83 100644--- a/desktop/src/registry/message.ts+++ b/desktop/src/registry/message.ts@@ -1,6 +1,7 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';import { blockSchema } from '@/registry/block';+import { isEqual } from 'lodash';export const messageAttributesSchema = z.object({type: z.literal('message'),@@ -11,7 +12,36 @@ export const messageAttributesSchema = z.object({export type MessageAttributes = z.infer<typeof messageAttributesSchema>;-export const MessageRegistry: NodeRegistry = {+export const messageModel: NodeModel = {type: 'message',schema: messageAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'message') {+ return false;+ }++ return context.hasCollaboratorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'message' || node.type !== 'message') {+ return false;+ }++ if (!isEqual(attributes.content, node.attributes.content)) {+ return context.userId === node.createdBy;+ }++ return context.hasCollaboratorAccess();+ },+ canDelete: async (context, node) => {+ if (node.type !== 'message') {+ return false;+ }++ if (context.userId === node.createdBy) {+ return true;+ }++ return context.hasAdminAccess();+ },};diff --git a/desktop/src/registry/page.ts b/desktop/src/registry/page.tsindex c5b74613..309147df 100644--- a/desktop/src/registry/page.ts+++ b/desktop/src/registry/page.ts@@ -1,6 +1,7 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';import { blockSchema } from '@/registry/block';+import { isEqual } from 'lodash';export const pageAttributesSchema = z.object({type: z.literal('page'),@@ -13,7 +14,33 @@ export const pageAttributesSchema = z.object({export type PageAttributes = z.infer<typeof pageAttributesSchema>;-export const PageRegistry: NodeRegistry = {+export const pageModel: NodeModel = {type: 'page',schema: pageAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'page') {+ return false;+ }++ const collaboratorIds = Object.keys(attributes.collaborators ?? {});+ if (collaboratorIds.length > 0 && !context.hasAdminAccess()) {+ return false;+ }++ return context.hasEditorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'page' || node.type !== 'page') {+ return false;+ }++ if (!isEqual(attributes.collaborators, node.attributes.collaborators)) {+ return context.hasAdminAccess();+ }++ return context.hasEditorAccess();+ },+ canDelete: async (context, node) => {+ return context.hasEditorAccess();+ },};diff --git a/desktop/src/registry/record.ts b/desktop/src/registry/record.tsindex 244b8735..3a9bd015 100644--- a/desktop/src/registry/record.ts+++ b/desktop/src/registry/record.ts@@ -1,7 +1,8 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';import { fieldValueSchema } from '@/registry/fields';import { blockSchema } from '@/registry/block';+export const recordAttributesSchema = z.object({type: z.literal('record'),parentId: z.string(),@@ -14,7 +15,24 @@ export const recordAttributesSchema = z.object({export type RecordAttributes = z.infer<typeof recordAttributesSchema>;-export const RecordRegistry: NodeRegistry = {+export const recordModel: NodeModel = {type: 'record',schema: recordAttributesSchema,+ canCreate: async (context, attributes) => {+ if (attributes.type !== 'record') {+ return false;+ }++ return context.hasCollaboratorAccess();+ },+ canUpdate: async (context, node, attributes) => {+ if (attributes.type !== 'record' || node.type !== 'record') {+ return false;+ }++ return context.hasCollaboratorAccess();+ },+ canDelete: async (context, node) => {+ return context.hasCollaboratorAccess();+ },};diff --git a/desktop/src/registry/space.ts b/desktop/src/registry/space.tsindex c7b9957e..ba1d4366 100644--- a/desktop/src/registry/space.ts+++ b/desktop/src/registry/space.ts@@ -1,5 +1,5 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';export const spaceAttributesSchema = z.object({type: z.literal('space'),@@ -12,7 +12,16 @@ export const spaceAttributesSchema = z.object({export type SpaceAttributes = z.infer<typeof spaceAttributesSchema>;-export const SpaceRegistry: NodeRegistry = {+export const spaceModel: NodeModel = {type: 'space',schema: spaceAttributesSchema,+ canCreate: async (context, attributes) => {+ return true;+ },+ canUpdate: async (context, node, attributes) => {+ return true;+ },+ canDelete: async (context, node) => {+ return true;+ },};diff --git a/desktop/src/registry/user.ts b/desktop/src/registry/user.tsindex 4ed9d7d1..dc1ae901 100644--- a/desktop/src/registry/user.ts+++ b/desktop/src/registry/user.ts@@ -1,5 +1,5 @@import { z } from 'zod';-import { NodeRegistry } from '@/registry/core';+import { NodeModel } from '@/registry/core';import { WorkspaceRole } from '@/types/workspaces';export const userAttributesSchema = z.object({@@ -14,7 +14,16 @@ export const userAttributesSchema = z.object({export type UserAttributes = z.infer<typeof userAttributesSchema>;-export const UserRegistry: NodeRegistry = {+export const userModel: NodeModel = {type: 'user',schema: userAttributesSchema,+ canCreate: async (context, attributes) => {+ return true;+ },+ canUpdate: async (context, node, attributes) => {+ return true;+ },+ canDelete: async (context, node) => {+ return true;+ },};