From 3668cf9b9e0837cef9fc92176b4450d01add33c7 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Mon, 23 Dec 2024 06:40:51 -0800 Subject: [PATCH] CRISTAL-257: The edit button is presented even if the current user does not have edit rights (#540) --- api/src/api/PageData.ts | 5 +++++ api/src/components/DefaultPageData.ts | 1 + core/backends/backend-xwiki/src/xwikiStorage.ts | 14 ++++++++++++-- skin/src/vue/c-content.vue | 16 +++++++++------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/api/src/api/PageData.ts b/api/src/api/PageData.ts index 49cb232a0..791f80ae9 100644 --- a/api/src/api/PageData.ts +++ b/api/src/api/PageData.ts @@ -43,6 +43,11 @@ export interface PageData { * @since 0.13 */ lastAuthor: UserDetails | undefined; + /** + * Indicate if the current user can edit this page. + * @since 0.13 + */ + canEdit: boolean; // TODO: remove any toObject(): any; // eslint-disable-line diff --git a/api/src/components/DefaultPageData.ts b/api/src/components/DefaultPageData.ts index 3f88319a4..d58233d29 100644 --- a/api/src/components/DefaultPageData.ts +++ b/api/src/components/DefaultPageData.ts @@ -38,6 +38,7 @@ export class DefaultPageData implements PageData { headlineRaw: string = ""; lastModificationDate: Date | undefined; lastAuthor: UserDetails | undefined; + canEdit: boolean = true; public constructor( id: string = "", diff --git a/core/backends/backend-xwiki/src/xwikiStorage.ts b/core/backends/backend-xwiki/src/xwikiStorage.ts index a5e165124..4be0089e4 100644 --- a/core/backends/backend-xwiki/src/xwikiStorage.ts +++ b/core/backends/backend-xwiki/src/xwikiStorage.ts @@ -149,7 +149,7 @@ export class XWikiStorage extends AbstractStorage { try { json = await response.json(); if (!response.ok) { - // TODO: Fix CRISTAL-383 + // TODO: Fix CRISTAL-383 (Error messages in Storages are not translated) this.alertsServiceProvider .get() .error(`Could not load page ${page}. Reason: ${json.error}`); @@ -203,6 +203,7 @@ export class XWikiStorage extends AbstractStorage { Date.parse(json.dateModified), ); pageContentData.lastAuthor = { name: json.editor }; + pageContentData.canEdit = json.canEdit; return pageContentData; } @@ -306,7 +307,7 @@ export class XWikiStorage extends AbstractStorage { async save(page: string, content: string, title: string): Promise { const url = this.buildSavePageURL(page, ["rest", "wikis", "xwiki"]); - await fetch(url, { + const response = await fetch(url, { method: "PUT", headers: { "Content-Type": "application/json", @@ -315,6 +316,15 @@ export class XWikiStorage extends AbstractStorage { // TODO: the syntax provided by the save is ignored and the content is always saved as markdown. body: JSON.stringify({ content, title, syntax: "markdown/1.2" }), }); + if (!response.ok) { + const errorMessage = await response.text(); + // TODO: Fix CRISTAL-383 (Error messages in Storages are not translated) + this.alertsServiceProvider + .get() + .error(`Could not save page ${page}. Reason: ${errorMessage}`); + // We need to throw an error to notify the editor that the save failed. + throw new Error(errorMessage); + } return; } diff --git a/skin/src/vue/c-content.vue b/skin/src/vue/c-content.vue index 2154f42b6..1ca4217da 100644 --- a/skin/src/vue/c-content.vue +++ b/skin/src/vue/c-content.vue @@ -100,13 +100,15 @@ onUpdated(() => {