Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Improve permission system: frontend

Smaug6739952f31b14b16
diff --git a/backend/controllers/nodes.controller.go b/backend/controllers/nodes.controller.go
index b827b746..3bb1b6d6 100644
--- a/backend/controllers/nodes.controller.go
+++ b/backend/controllers/nodes.controller.go
@@ -105,11 +105,21 @@ func (ctr *Controller) GetNode(c *gin.Context) (int, any) {
if err != nil {
return http.StatusInternalServerError, err
}
- _, err = utils.NodePermission(c, node, ctr.app.Services.Permissions, utils.READ)
+ connectedUserId, err := utils.NodePermission(c, node, ctr.app.Services.Permissions, utils.READ)
if err != nil {
return http.StatusUnauthorized, err
}
- return http.StatusOK, node
+ perms, err := ctr.app.Services.Permissions.GetNodePermission(nodeId)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ filteredPerms := []*models.Permission{}
+ for _, p := range perms {
+ if p.UserId == connectedUserId || node.UserId == connectedUserId {
+ filteredPerms = append(filteredPerms, p)
+ }
+ }
+ return http.StatusOK, gin.H{"node": node, "permissions": filteredPerms}
}
// Create node
diff --git a/frontend/app/pages/dashboard/docs/_modals/NodePermissions.vue b/frontend/app/pages/dashboard/docs/_modals/NodePermissions.vue
index 3c95576f..851df093 100644
--- a/frontend/app/pages/dashboard/docs/_modals/NodePermissions.vue
+++ b/frontend/app/pages/dashboard/docs/_modals/NodePermissions.vue
@@ -30,11 +30,11 @@
<!-- Current permissions -->
<h3>Current permissions</h3>
<ul class="permissions-list">
- <li v-for="perm in permissions" :key="perm.id" class="permission-item">
+ <li v-for="perm in localNode?.permissions" :key="perm.id" class="permission-item">
<div class="user-info-row">
<span class="user-meta">
- <img :src="useAvatar(perm.user)" alt="avatar" class="avatar" />
- <span>{{ perm.user?.username }}</span>
+ <img :src="useAvatar(userById(perm.user_id))" alt="avatar" class="avatar" />
+ <span>{{ userById(perm.user_id)?.username }}</span>
</span>
<div class="user-actions">
@@ -52,7 +52,7 @@
</template>
<script setup lang="ts">
-import type { Permission, PublicUser } from '~/stores';
+import type { Node, Permission, PublicUser } from '~/stores';
const props = defineProps<{
nodeId: string;
@@ -64,7 +64,17 @@ const nodesStore = useNodesStore();
const query = ref('');
const user = ref<PublicUser | null>(null);
const selectedPermission = ref(1);
-const permissions = ref<Permission[]>([]);
+const localNode = ref<Node | null>(null);
+
+onMounted(async () => {
+ localNode.value = await nodesStore.fetch({ id: props.nodeId });
+ for (const perm of localNode.value.permissions) {
+ perm.user = await usersStore.fetchPublicUser(perm.user_id);
+ }
+ console.log(localNode.value);
+});
+
+const userById = computed(() => (id: string) => usersStore.getById(id) || undefined);
watch(query, async newQuery => {
if (newQuery) {
@@ -74,10 +84,6 @@ watch(query, async newQuery => {
}
});
-onMounted(async () => {
- permissions.value = await nodesStore.fetchPermissions(props.nodeId);
-});
-
const addPermission = async () => {
if (!user.value) return;
const perm = await nodesStore.addPermission({
@@ -85,7 +91,7 @@ const addPermission = async () => {
user_id: user.value.id,
permission: selectedPermission.value,
});
- if (perm) permissions.value.push(perm);
+ if (perm && localNode.value) localNode.value.permissions.push(perm);
user.value = null;
query.value = '';
selectedPermission.value = 1;
@@ -96,8 +102,9 @@ const updatePermission = async (perm: Permission) => {
};
const removePermission = async (perm: Permission) => {
- await nodesStore.removePermission(props.nodeId, perm.user_id);
- permissions.value = permissions.value.filter(p => p.id !== perm.id);
+ if (!localNode.value) return;
+ await nodesStore.removePermission(localNode.value.id, perm.user_id);
+ localNode.value.permissions = localNode.value.permissions.filter(p => p.id !== perm.id);
};
</script>
diff --git a/frontend/app/stores/db_strustures.d.ts b/frontend/app/stores/db_strustures.d.ts
index 33defedc..dc000368 100644
--- a/frontend/app/stores/db_strustures.d.ts
+++ b/frontend/app/stores/db_strustures.d.ts
@@ -59,6 +59,7 @@ export interface Permission {
export interface Node extends DB_Node {
partial?: boolean;
shared: boolean;
+ permissions: Permission[];
}
export interface User {
diff --git a/frontend/app/stores/nodes.store.ts b/frontend/app/stores/nodes.store.ts
index 1c8912d2..8455a574 100644
--- a/frontend/app/stores/nodes.store.ts
+++ b/frontend/app/stores/nodes.store.ts
@@ -14,7 +14,6 @@ export const useNodesStore = defineStore('nodes', {
state: () => ({
nodes: [] as Array<Node | Node>,
public_nodes: [] as Node[],
- permissions: [] as Permission[],
allTags: [] as string[],
isFetching: false,
}),
@@ -124,16 +123,17 @@ export const useNodesStore = defineStore('nodes', {
this.isFetching = false;
if (request.status == 'success') {
if (opts?.id) {
+ const result = request.result as { node: DB_Node; permissions: Permission[] };
const index = this.nodes.findIndex(d => d.id == opts?.id);
- const updatedNode: Node = { ...(request.result as DB_Node), partial: false, shared: false };
+ const updatedNode: Node = { ...(result.node as DB_Node), partial: false, shared: false, permissions: result.permissions };
if (index == -1) this.nodes.push(updatedNode);
else this.nodes[index] = updatedNode;
return updatedNode as 'id' extends keyof T ? Node : Node[];
} else {
for (const node of request.result as DB_Node[]) {
const index = this.nodes.findIndex(d => d.id == node.id);
- if (index == -1) this.nodes.push({ ...node, partial: true, shared: false });
- else this.nodes[index] = { ...node, partial: true, shared: false };
+ if (index == -1) this.nodes.push({ ...node, partial: true, shared: false, permissions: [] });
+ else this.nodes[index] = { ...node, partial: true, shared: false, permissions: [] };
}
this.recomputeTags();
return this.nodes as 'id' extends keyof T ? Node : Node[];
@@ -146,7 +146,7 @@ export const useNodesStore = defineStore('nodes', {
if (existingDoc) return existingDoc;
const request = await makeRequest(`nodes/public/${id}`, 'GET', {});
if (request.status === 'success') {
- const fetchedDoc: Node = { ...(request.result as DB_Node), partial: false, shared: false };
+ const fetchedDoc: Node = { ...(request.result as DB_Node), partial: false, shared: false, permissions: [] };
this.public_nodes.push(fetchedDoc);
return fetchedDoc;
} else return undefined;
@@ -158,56 +158,48 @@ export const useNodesStore = defineStore('nodes', {
if (request.status === 'success') {
for (const node of request.result as DB_Node[]) {
const index = this.nodes.findIndex(d => d.id == node.id);
- if (index == -1) this.nodes.push({ ...node, partial: true, shared: true });
- else this.nodes[index] = { ...node, partial: true, shared: true };
+ if (index == -1) this.nodes.push({ ...node, partial: true, shared: true, permissions: [] });
+ else this.nodes[index] = { ...node, partial: true, shared: true, permissions: [] };
}
return this.nodes;
} else throw request;
},
- async fetchPermissions(nodeId: string): Promise<Permission[]> {
- console.log(`[store/nodes/permissions] Fetching permissions for node: ${nodeId}`);
- const node = this.nodes.find(d => d.id === nodeId);
- if (!node) throw 'Node not found in store, cannot fetch permissions';
- const request = await makeRequest(`permissions/${nodeId}`, 'GET', {});
- if (request.status === 'success') {
- for (const perm of request.result as Permission[]) {
- perm.user = await useUserStore().fetchPublicUser(perm.user_id);
- const index = this.permissions.findIndex(p => p.id == perm.id);
- if (index == -1) this.permissions.push(perm);
- else this.permissions[index] = perm;
- }
- return request.result as Permission[];
- } else throw request;
- },
+
async addPermission(perm: Omit<Permission, 'id' | 'created_timestamp'>): Promise<Permission> {
console.log(`[store/nodes/permissions] Adding permission for user ${perm.user_id} on node ${perm.node_id}`);
+ const node = this.nodes.find(n => n.id === perm.node_id);
+ if (!node) throw 'Node not found in store, cannot add permission';
const request = await makeRequest(`permissions`, 'POST', perm);
if (request.status === 'success') {
- this.permissions.push(request.result as Permission);
+ node.permissions.push(request.result as Permission);
return request.result as Permission;
} else throw request.message;
},
async updatePermission(perm: Permission) {
console.log(`[store/nodes/permissions] Updating permission for user ${perm.user_id} on node ${perm.node_id}`);
+ const node = this.nodes.find(n => n.id === perm.node_id);
+ if (!node) throw 'Node not found in store, cannot update permission';
const request = await makeRequest(`permissions/${perm.id}`, 'PATCH', { permission: perm.permission });
if (request.status === 'success') {
- const index = this.permissions.findIndex(p => p.id === perm.id);
- if (index !== -1) this.permissions[index]!.permission = perm.permission;
+ const index = node.permissions.findIndex(p => p.id === perm.id);
+ if (index !== -1) node.permissions[index]!.permission = perm.permission;
} else throw request.message;
},
async removePermission(nodeId: string, userId: string) {
console.log(`[store/nodes/permissions] Removing permission for user ${userId} on node ${nodeId}`);
- const perm = this.permissions.find(p => p.node_id === nodeId && p.user_id === userId);
+ const node = this.nodes.find(n => n.id === nodeId);
+ if (!node) throw 'Node not found in store, cannot remove permission';
+ const perm = node.permissions.find(p => p.user_id === userId);
if (!perm) throw 'Permission not found in store, cannot remove permission';
const request = await makeRequest(`permissions/${perm.id}`, 'DELETE', {});
if (request.status === 'success') {
- this.permissions = this.permissions.filter(p => p.id !== perm.id);
+ node.permissions = node.permissions.filter(p => p.id !== perm.id);
} else throw request.message;
},
async post(node: Partial<Node>): Promise<DB_Node> {
const request = await makeRequest('nodes', 'POST', node);
if (request.status == 'success') {
- this.nodes.push({ ...(request.result as DB_Node), partial: false, shared: false });
+ this.nodes.push({ ...(request.result as DB_Node), partial: false, shared: false, permissions: [] });
return request.result as DB_Node;
} else throw request.message;
},
diff --git a/frontend/app/stores/user.store.ts b/frontend/app/stores/user.store.ts
index 0cf9e022..45861567 100644
--- a/frontend/app/stores/user.store.ts
+++ b/frontend/app/stores/user.store.ts
@@ -7,6 +7,9 @@ export const useUserStore = defineStore('user', {
users: [] as PublicUser[],
last_connection: null as ConnectionLog | null,
}),
+ getters: {
+ getById: state => (id: string) => state.users.find(u => u.id === id) || null,
+ },
actions: {
async login(username: string, password: string) {
try {