|
|
@@ -1,3 +1,4 @@
|
|
|
+const { isCreatablePage } = require('@commons/util/path-utils');
|
|
|
const { serializePageSecurely } = require('../models/serializers/page-serializer');
|
|
|
const { serializeRevisionSecurely } = require('../models/serializers/revision-serializer');
|
|
|
const { serializeUserSecurely } = require('../models/serializers/user-serializer');
|
|
|
@@ -478,12 +479,10 @@ module.exports = function(crowi, app) {
|
|
|
actions.notFound = async function(req, res) {
|
|
|
const path = getPathFromRequest(req);
|
|
|
|
|
|
- const isCreatable = Page.isCreatableName(path);
|
|
|
-
|
|
|
let view;
|
|
|
const renderVars = { path };
|
|
|
|
|
|
- if (!isCreatable) {
|
|
|
+ if (!isCreatablePage(path)) {
|
|
|
view = 'layout-growi/not_creatable';
|
|
|
}
|
|
|
else if (req.isForbidden) {
|
|
|
@@ -1231,124 +1230,6 @@ module.exports = function(crowi, app) {
|
|
|
return res.json(ApiResponse.success(result));
|
|
|
};
|
|
|
|
|
|
- /**
|
|
|
- * @swagger
|
|
|
- *
|
|
|
- * /pages.rename:
|
|
|
- * post:
|
|
|
- * tags: [Pages, CrowiCompatibles]
|
|
|
- * operationId: renamePage
|
|
|
- * summary: /pages.rename
|
|
|
- * description: Rename page
|
|
|
- * requestBody:
|
|
|
- * content:
|
|
|
- * application/json:
|
|
|
- * schema:
|
|
|
- * properties:
|
|
|
- * page_id:
|
|
|
- * $ref: '#/components/schemas/Page/properties/_id'
|
|
|
- * path:
|
|
|
- * $ref: '#/components/schemas/Page/properties/path'
|
|
|
- * revision_id:
|
|
|
- * $ref: '#/components/schemas/Revision/properties/_id'
|
|
|
- * new_path:
|
|
|
- * type: string
|
|
|
- * description: new path
|
|
|
- * example: /user/alice/new_test
|
|
|
- * create_redirect:
|
|
|
- * type: boolean
|
|
|
- * description: whether redirect page
|
|
|
- * required:
|
|
|
- * - page_id
|
|
|
- * responses:
|
|
|
- * 200:
|
|
|
- * description: Succeeded to rename page.
|
|
|
- * content:
|
|
|
- * application/json:
|
|
|
- * schema:
|
|
|
- * properties:
|
|
|
- * ok:
|
|
|
- * $ref: '#/components/schemas/V1Response/properties/ok'
|
|
|
- * page:
|
|
|
- * $ref: '#/components/schemas/Page'
|
|
|
- * 403:
|
|
|
- * $ref: '#/components/responses/403'
|
|
|
- * 500:
|
|
|
- * $ref: '#/components/responses/500'
|
|
|
- */
|
|
|
- /**
|
|
|
- * @api {post} /pages.rename Rename page
|
|
|
- * @apiName RenamePage
|
|
|
- * @apiGroup Page
|
|
|
- *
|
|
|
- * @apiParam {String} page_id Page Id.
|
|
|
- * @apiParam {String} path
|
|
|
- * @apiParam {String} revision_id
|
|
|
- * @apiParam {String} new_path New path name.
|
|
|
- * @apiParam {Bool} create_redirect
|
|
|
- */
|
|
|
- api.rename = async function(req, res) {
|
|
|
- const pageId = req.body.page_id;
|
|
|
- const previousRevision = req.body.revision_id || null;
|
|
|
- let newPagePath = pathUtils.normalizePath(req.body.new_path);
|
|
|
- const options = {
|
|
|
- createRedirectPage: (req.body.create_redirect != null),
|
|
|
- updateMetadata: (req.body.remain_metadata == null),
|
|
|
- socketClientId: +req.body.socketClientId || undefined,
|
|
|
- };
|
|
|
- const isRecursively = (req.body.recursively != null);
|
|
|
-
|
|
|
- if (!Page.isCreatableName(newPagePath)) {
|
|
|
- return res.json(ApiResponse.error(`Could not use the path '${newPagePath})'`, 'invalid_path'));
|
|
|
- }
|
|
|
-
|
|
|
- // check whether path starts slash
|
|
|
- newPagePath = pathUtils.addHeadingSlash(newPagePath);
|
|
|
-
|
|
|
- const isExist = await Page.count({ path: newPagePath }) > 0;
|
|
|
- if (isExist) {
|
|
|
- // if page found, cannot cannot rename to that path
|
|
|
- return res.json(ApiResponse.error(`'new_path=${newPagePath}' already exists`, 'already_exists'));
|
|
|
- }
|
|
|
-
|
|
|
- let page;
|
|
|
-
|
|
|
- try {
|
|
|
- page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
-
|
|
|
- if (page == null) {
|
|
|
- return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
|
|
|
- }
|
|
|
-
|
|
|
- if (!page.isUpdatable(previousRevision)) {
|
|
|
- return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
|
|
|
- }
|
|
|
-
|
|
|
- page = await crowi.pageService.renamePage(page, newPagePath, req.user, options, isRecursively);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.error(err);
|
|
|
- return res.json(ApiResponse.error('Failed to update page.', 'unknown'));
|
|
|
- }
|
|
|
-
|
|
|
- const result = {};
|
|
|
- result.page = page; // TODO consider to use serializePageSecurely method -- 2018.08.06 Yuki Takei
|
|
|
-
|
|
|
- res.json(ApiResponse.success(result));
|
|
|
-
|
|
|
- try {
|
|
|
- // global notification
|
|
|
- await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page, req.user, {
|
|
|
- oldPath: req.body.path,
|
|
|
- });
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.error('Move notification failed', err);
|
|
|
- }
|
|
|
-
|
|
|
- return page;
|
|
|
- };
|
|
|
-
|
|
|
/**
|
|
|
* @swagger
|
|
|
*
|