feat: branch off / create from template
This commit is contained in:
@@ -5,6 +5,8 @@ const _ = require('lodash')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
const tmplCreateRegex = /^[0-9]+(,[0-9]+)?$/
|
||||
|
||||
/**
|
||||
* Robots.txt
|
||||
*/
|
||||
@@ -89,13 +91,16 @@ router.get(['/e', '/e/*'], async (req, res, next) => {
|
||||
return res.redirect(`/e/${pageArgs.locale}/${pageArgs.path}`)
|
||||
}
|
||||
|
||||
// -> Set Editor Lang
|
||||
_.set(res, 'locals.siteConfig.lang', pageArgs.locale)
|
||||
_.set(res, 'locals.siteConfig.rtl', req.i18n.dir() === 'rtl')
|
||||
|
||||
// -> Check for reserved path
|
||||
if (pageHelper.isReservedPath(pageArgs.path)) {
|
||||
return next(new Error('Cannot create this page because it starts with a system reserved path.'))
|
||||
}
|
||||
|
||||
// -> Get page data from DB
|
||||
let page = await WIKI.models.pages.getPageFromDb({
|
||||
path: pageArgs.path,
|
||||
locale: pageArgs.locale,
|
||||
@@ -112,11 +117,13 @@ router.get(['/e', '/e/*'], async (req, res, next) => {
|
||||
}
|
||||
|
||||
if (page) {
|
||||
// -> EDIT MODE
|
||||
if (!WIKI.auth.checkAccess(req.user, ['write:pages', 'manage:pages'], pageArgs)) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
||||
return res.render('unauthorized', { action: 'edit' })
|
||||
}
|
||||
|
||||
// -> Get page tags
|
||||
await page.$relatedQuery('tags')
|
||||
page.tags = _.map(page.tags, 'tag')
|
||||
|
||||
@@ -126,6 +133,7 @@ router.get(['/e', '/e/*'], async (req, res, next) => {
|
||||
page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
|
||||
page.content = Buffer.from(page.content).toString('base64')
|
||||
} else {
|
||||
// -> CREATE MODE
|
||||
if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
||||
return res.render('unauthorized', { action: 'create' })
|
||||
@@ -137,7 +145,54 @@ router.get(['/e', '/e/*'], async (req, res, next) => {
|
||||
localeCode: pageArgs.locale,
|
||||
editorKey: null,
|
||||
mode: 'create',
|
||||
content: null
|
||||
content: null,
|
||||
title: null,
|
||||
description: null
|
||||
}
|
||||
|
||||
// -> From Template
|
||||
if (req.query.from && tmplCreateRegex.test(req.query.from)) {
|
||||
let tmplPageId = 0
|
||||
let tmplVersionId = 0
|
||||
if (req.query.from.indexOf(',')) {
|
||||
const q = req.query.from.split(',')
|
||||
tmplPageId = _.toSafeInteger(q[0])
|
||||
tmplVersionId = _.toSafeInteger(q[1])
|
||||
} else {
|
||||
tmplPageId = _.toSafeInteger(req.query.from)
|
||||
}
|
||||
|
||||
if (tmplVersionId > 0) {
|
||||
// -> From Page Version
|
||||
const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: tmplPageId, versionId: tmplVersionId })
|
||||
if (!pageVersion) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
||||
return res.status(404).render('notfound', { action: 'template' })
|
||||
}
|
||||
if (!WIKI.auth.checkAccess(req.user, ['read:history'], { path: pageVersion.path, locale: pageVersion.locale })) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
||||
return res.render('unauthorized', { action: 'sourceVersion' })
|
||||
}
|
||||
page.content = Buffer.from(pageVersion.content).toString('base64')
|
||||
page.editorKey = pageVersion.editor
|
||||
page.title = pageVersion.title
|
||||
page.description = pageVersion.description
|
||||
} else {
|
||||
// -> From Page Live
|
||||
const pageOriginal = await WIKI.models.pages.query().findById(tmplPageId)
|
||||
if (!pageOriginal) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
||||
return res.status(404).render('notfound', { action: 'template' })
|
||||
}
|
||||
if (!WIKI.auth.checkAccess(req.user, ['read:source'], { path: pageOriginal.path, locale: pageOriginal.locale })) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
|
||||
return res.render('unauthorized', { action: 'source' })
|
||||
}
|
||||
page.content = Buffer.from(pageOriginal.content).toString('base64')
|
||||
page.editorKey = pageOriginal.editorKey
|
||||
page.title = pageOriginal.title
|
||||
page.description = pageOriginal.description
|
||||
}
|
||||
}
|
||||
}
|
||||
res.render('editor', { page, injectCode })
|
||||
@@ -163,6 +218,11 @@ router.get(['/h', '/h/*'], async (req, res, next) => {
|
||||
isPrivate: false
|
||||
})
|
||||
|
||||
if (!page) {
|
||||
_.set(res.locals, 'pageMeta.title', 'Page Not Found')
|
||||
return res.status(404).render('notfound', { action: 'history' })
|
||||
}
|
||||
|
||||
pageArgs.tags = _.get(page, 'tags', [])
|
||||
|
||||
if (!WIKI.auth.checkAccess(req.user, ['read:history'], pageArgs)) {
|
||||
|
Reference in New Issue
Block a user