Skip to content

Commit

Permalink
Add delete flag in configuration. Fixes #593.
Browse files Browse the repository at this point in the history
Adds a `delete` flag to collections in `config.yml`. Defaults to
false. This is mostly for use with files to restrict users from
deleting settings files etc that available via the CMS.
  • Loading branch information
rpullinger committed Oct 17, 2017
1 parent 8a819be commit bfa2070
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions example/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ collections: # A list of collections the CMS should be able to edit
folder: "_posts"
slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
create: true # Allow users to create new documents in this collection
delete: true # Allow users to delete documents from this collection
fields: # The fields each document in this collection have
- {label: "Title", name: "title", widget: "string", tagname: "h1"}
- {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD hh:mma"}
Expand All @@ -22,12 +23,14 @@ collections: # A list of collections the CMS should be able to edit
label: "FAQ" # Used in the UI, ie.: "New Post"
folder: "_faqs"
create: true # Allow users to create new documents in this collection
delete: true # Allow users to delete documents from this collection
fields: # The fields each document in this collection have
- {label: "Question", name: "title", widget: "string", tagname: "h1"}
- {label: "Answer", name: "body", widget: "markdown"}

- name: "settings"
label: "Settings"
delete: false
editor:
preview: false
files:
Expand Down
7 changes: 6 additions & 1 deletion src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TestRepoBackend from "./test-repo/implementation";
import GitHubBackend from "./github/implementation";
import GitGatewayBackend from "./git-gateway/implementation";
import { resolveFormat } from "../formats/formats";
import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries, selectFolderEntryExtension } from "../reducers/collections";
import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries, selectAllowDeletion, selectFolderEntryExtension } from "../reducers/collections";
import { createEntry } from "../valueObjects/Entry";
import { sanitizeSlug } from "../lib/urlHelper";

Expand Down Expand Up @@ -246,6 +246,11 @@ class Backend {

deleteEntry(config, collection, slug) {
const path = selectEntryPath(collection, slug);

if (!selectAllowDeletion(collection)) {
throw (new Error("Not allowed to delete entries in this collection"));
}

const commitMessage = `Delete ${ collection.get('label') }${ slug }”`;
return this.implementation.deleteFile(path, commitMessage);
}
Expand Down
8 changes: 6 additions & 2 deletions src/containers/editorialWorkflow/EntryPageHOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { EDITORIAL_WORKFLOW } from '../../constants/publishModes';
import { selectUnpublishedEntry, selectEntry } from '../../reducers';
import { selectAllowDeletion } from "../../reducers/collections";
import { loadUnpublishedEntry, persistUnpublishedEntry } from '../../actions/editorialWorkflow';


Expand All @@ -15,11 +16,14 @@ export default function EntryPageHOC(EntryPage) {
function mapStateToProps(state, ownProps) {
const { collections } = state;
const isEditorialWorkflow = (state.config.get('publish_mode') === EDITORIAL_WORKFLOW);
const returnObj = { isEditorialWorkflow, showDelete: !ownProps.newEntry };
const collection = collections.get(ownProps.match.params.name);
const returnObj = {
isEditorialWorkflow,
showDelete: !ownProps.newEntry && selectAllowDeletion(collection),
};
if (isEditorialWorkflow) {
returnObj.showDelete = false;
const slug = ownProps.match.params.slug;
const collection = collections.get(ownProps.match.params.name);
const unpublishedEntry = selectUnpublishedEntry(state, collection.get('name'), slug);
if (unpublishedEntry) {
returnObj.unpublishedEntry = true;
Expand Down
7 changes: 7 additions & 0 deletions src/reducers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const selectors = {
allowNewEntries(collection) {
return collection.get('create');
},
allowDeletion(collection) {
return collection.get('delete');
},
templateName(collection) {
return collection.get('name');
},
Expand Down Expand Up @@ -80,6 +83,9 @@ const selectors = {
allowNewEntries() {
return false;
},
allowDeletion(collection) {
return collection.get('delete');
},
templateName(collection, slug) {
return slug;
},
Expand All @@ -92,6 +98,7 @@ export const selectEntryPath = (collection, slug) => selectors[collection.get('t
export const selectEntrySlug = (collection, path) => selectors[collection.get('type')].entrySlug(collection, path);
export const selectListMethod = collection => selectors[collection.get('type')].listMethod();
export const selectAllowNewEntries = collection => selectors[collection.get('type')].allowNewEntries(collection);
export const selectAllowDeletion = collection => selectors[collection.get('type')].allowDeletion(collection);
export const selectTemplateName = (collection, slug) => selectors[collection.get('type')].templateName(collection, slug);
export const selectInferedField = (collection, fieldName) => {
const inferableField = INFERABLE_FIELDS[fieldName];
Expand Down

0 comments on commit bfa2070

Please sign in to comment.