Skip to content

Commit

Permalink
CRISTAL-257: The edit button is presented even if the current user do…
Browse files Browse the repository at this point in the history
…es not have edit rights (#540)
  • Loading branch information
pjeanjean authored Dec 23, 2024
1 parent a3d0015 commit 3668cf9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions api/src/api/PageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions api/src/components/DefaultPageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class DefaultPageData implements PageData {
headlineRaw: string = "";
lastModificationDate: Date | undefined;
lastAuthor: UserDetails | undefined;
canEdit: boolean = true;

public constructor(
id: string = "",
Expand Down
14 changes: 12 additions & 2 deletions core/backends/backend-xwiki/src/xwikiStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -203,6 +203,7 @@ export class XWikiStorage extends AbstractStorage {
Date.parse(json.dateModified),
);
pageContentData.lastAuthor = { name: json.editor };
pageContentData.canEdit = json.canEdit;
return pageContentData;
}

Expand Down Expand Up @@ -306,7 +307,7 @@ export class XWikiStorage extends AbstractStorage {
async save(page: string, content: string, title: string): Promise<unknown> {
const url = this.buildSavePageURL(page, ["rest", "wikis", "xwiki"]);

await fetch(url, {
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Expand All @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions skin/src/vue/c-content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,23 @@ onUpdated(() => {
<template #doc-page-actions>
<div class="doc-page-actions">
<router-link
:to="
currentPageRevision
? ''
: { name: 'edit', params: { page: currentPageName } }
"
v-if="!currentPageRevision && currentPage?.canEdit"
:to="{ name: 'edit', params: { page: currentPageName } }"
>
<x-btn size="small" :disabled="currentPageRevision !== undefined">
<x-btn
size="small"
:disabled="
currentPageRevision !== undefined || !currentPage?.canEdit
"
>
<c-icon name="pencil" :size="Size.Small"></c-icon>
Edit
</x-btn>
</router-link>
<page-actions
:current-page="currentPage"
:current-page-name="currentPageName ?? ''"
:disabled="currentPageRevision !== undefined"
:disabled="currentPageRevision !== undefined || !currentPage?.canEdit"
></page-actions>
</div>
</template>
Expand Down

0 comments on commit 3668cf9

Please sign in to comment.