|
|
@@ -1,15 +1,16 @@
|
|
|
module.exports = function(crowi, app) {
|
|
|
'use strict';
|
|
|
|
|
|
- var debug = require('debug')('growi:routes:page')
|
|
|
+ const debug = require('debug')('growi:routes:page')
|
|
|
+ , logger = require('@alias/logger')('growi:routes:page')
|
|
|
, Page = crowi.model('Page')
|
|
|
, User = crowi.model('User')
|
|
|
, Config = crowi.model('Config')
|
|
|
, config = crowi.getConfig()
|
|
|
, Revision = crowi.model('Revision')
|
|
|
, Bookmark = crowi.model('Bookmark')
|
|
|
- , UserGroupRelation = crowi.model('UserGroupRelation')
|
|
|
, PageGroupRelation = crowi.model('PageGroupRelation')
|
|
|
+ , UpdatePost = crowi.model('UpdatePost')
|
|
|
, ApiResponse = require('../util/apiResponse')
|
|
|
, interceptorManager = crowi.getInterceptorManager()
|
|
|
, pagePathUtil = require('../util/pagePathUtil')
|
|
|
@@ -221,7 +222,7 @@ module.exports = function(crowi, app) {
|
|
|
};
|
|
|
|
|
|
actions.pageListShowForCrowiPlus = function(req, res) {
|
|
|
- var path = getPathFromRequest(req);
|
|
|
+ let path = getPathFromRequest(req);
|
|
|
// omit the slash of the last
|
|
|
path = path.replace((/\/$/), '');
|
|
|
// redirect
|
|
|
@@ -257,9 +258,10 @@ module.exports = function(crowi, app) {
|
|
|
pageRelatedGroup: null,
|
|
|
template: null,
|
|
|
revisionHackmdSynced: null,
|
|
|
+ slack: '',
|
|
|
};
|
|
|
|
|
|
- let pageTeamplate = 'customlayout-selector/page';
|
|
|
+ let view = 'customlayout-selector/page';
|
|
|
|
|
|
let isRedirect = false;
|
|
|
Page.findPage(path, req.user, req.query.revision)
|
|
|
@@ -294,13 +296,19 @@ module.exports = function(crowi, app) {
|
|
|
renderVars.pageRelatedGroup = pageGroupRelation.relatedGroup;
|
|
|
}
|
|
|
})
|
|
|
+ .then(() => {
|
|
|
+ return getSlackChannels(page);
|
|
|
+ })
|
|
|
+ .then((channels) => {
|
|
|
+ renderVars.slack = channels;
|
|
|
+ })
|
|
|
.then(function() {
|
|
|
const userPage = isUserPage(page.path);
|
|
|
let userData = null;
|
|
|
|
|
|
if (userPage) {
|
|
|
// change template
|
|
|
- pageTeamplate = 'customlayout-selector/user_page';
|
|
|
+ view = 'customlayout-selector/user_page';
|
|
|
|
|
|
return User.findUserByUsername(User.getUsernameByPath(page.path))
|
|
|
.then(function(data) {
|
|
|
@@ -326,18 +334,30 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
}
|
|
|
})
|
|
|
- // look for templates if page not exists
|
|
|
+ // page is not found or user is forbidden
|
|
|
.catch(function(err) {
|
|
|
- pageTeamplate = 'customlayout-selector/not_found';
|
|
|
+ let isForbidden = false;
|
|
|
+ if (err.name === 'UserHasNoGrantException') {
|
|
|
+ isForbidden = true;
|
|
|
+ }
|
|
|
|
|
|
- return Page.findTemplate(path)
|
|
|
- .then(template => {
|
|
|
- if (template) {
|
|
|
- template = replacePlaceholders(template, req);
|
|
|
- }
|
|
|
+ if (isForbidden) {
|
|
|
+ view = 'customlayout-selector/forbidden';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ view = 'customlayout-selector/not_found';
|
|
|
|
|
|
- renderVars.template = template;
|
|
|
- });
|
|
|
+ // look for templates
|
|
|
+ return Page.findTemplate(path)
|
|
|
+ .then(template => {
|
|
|
+ if (template) {
|
|
|
+ template = replacePlaceholders(template, req);
|
|
|
+ }
|
|
|
+
|
|
|
+ renderVars.template = template;
|
|
|
+ });
|
|
|
+ }
|
|
|
})
|
|
|
// get list pages
|
|
|
.then(function() {
|
|
|
@@ -362,16 +382,26 @@ module.exports = function(crowi, app) {
|
|
|
return interceptorManager.process('beforeRenderPage', req, res, renderVars);
|
|
|
})
|
|
|
.then(function() {
|
|
|
- res.render(req.query.presentation ? 'page_presentation' : pageTeamplate, renderVars);
|
|
|
+ res.render(req.query.presentation ? 'page_presentation' : view, renderVars);
|
|
|
})
|
|
|
.catch(function(err) {
|
|
|
- console.log(err);
|
|
|
- debug('Error on rendering pageListShowForCrowiPlus', err);
|
|
|
+ logger.error('Error on rendering pageListShowForCrowiPlus', err);
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ const getSlackChannels = async page => {
|
|
|
+ if (page.extended.slack) {
|
|
|
+ return page.extended.slack;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const data = await UpdatePost.findSettingsByPath(page.path);
|
|
|
+ const channels = data.map(e => e.channel).join(', ');
|
|
|
+ return channels;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
const replacePlaceholders = (template, req) => {
|
|
|
const definitions = {
|
|
|
pagepath: getPathFromRequest(req),
|
|
|
@@ -452,22 +482,28 @@ module.exports = function(crowi, app) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- function renderPage(pageData, req, res) {
|
|
|
- // create page
|
|
|
+ async function renderPage(pageData, req, res, isForbidden) {
|
|
|
if (!pageData) {
|
|
|
- const path = getPathFromRequest(req);
|
|
|
- return Page.findTemplate(path)
|
|
|
- .then(template => {
|
|
|
- if (template) {
|
|
|
- template = replacePlaceholders(template, req);
|
|
|
- }
|
|
|
+ let view = 'customlayout-selector/not_found';
|
|
|
+ let template = undefined;
|
|
|
|
|
|
- return res.render('customlayout-selector/not_found', {
|
|
|
- author: {},
|
|
|
- page: false,
|
|
|
- template,
|
|
|
- });
|
|
|
- });
|
|
|
+ // forbidden
|
|
|
+ if (isForbidden) {
|
|
|
+ view = 'customlayout-selector/forbidden';
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const path = getPathFromRequest(req);
|
|
|
+ template = await Page.findTemplate(path);
|
|
|
+ if (template != null) {
|
|
|
+ template = replacePlaceholders(template, req);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res.render(view, {
|
|
|
+ author: {},
|
|
|
+ page: false,
|
|
|
+ template,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -475,14 +511,15 @@ module.exports = function(crowi, app) {
|
|
|
return res.redirect(encodeURI(pageData.redirectTo + '?redirectFrom=' + pagePathUtil.encodePagePath(pageData.path)));
|
|
|
}
|
|
|
|
|
|
- var renderVars = {
|
|
|
+ const renderVars = {
|
|
|
path: pageData.path,
|
|
|
page: pageData,
|
|
|
revision: pageData.revision || {},
|
|
|
author: pageData.revision.author || false,
|
|
|
+ slack: '',
|
|
|
};
|
|
|
- var userPage = isUserPage(pageData.path);
|
|
|
- var userData = null;
|
|
|
+ const userPage = isUserPage(pageData.path);
|
|
|
+ let userData = null;
|
|
|
|
|
|
Revision.findRevisionList(pageData.path, {})
|
|
|
.then(function(tree) {
|
|
|
@@ -496,6 +533,12 @@ module.exports = function(crowi, app) {
|
|
|
renderVars.pageRelatedGroup = pageGroupRelation.relatedGroup;
|
|
|
}
|
|
|
})
|
|
|
+ .then(() => {
|
|
|
+ return getSlackChannels(pageData);
|
|
|
+ })
|
|
|
+ .then(channels => {
|
|
|
+ renderVars.slack = channels;
|
|
|
+ })
|
|
|
.then(function() {
|
|
|
if (userPage) {
|
|
|
return User.findUserByUsername(User.getUsernameByPath(pageData.path))
|
|
|
@@ -525,11 +568,11 @@ module.exports = function(crowi, app) {
|
|
|
}).then(function() {
|
|
|
return interceptorManager.process('beforeRenderPage', req, res, renderVars);
|
|
|
}).then(function() {
|
|
|
- var defaultPageTeamplate = 'customlayout-selector/page';
|
|
|
+ let view = 'customlayout-selector/page';
|
|
|
if (userData) {
|
|
|
- defaultPageTeamplate = 'customlayout-selector/user_page';
|
|
|
+ view = 'customlayout-selector/user_page';
|
|
|
}
|
|
|
- res.render(req.query.presentation ? 'page_presentation' : defaultPageTeamplate, renderVars);
|
|
|
+ res.render(req.query.presentation ? 'page_presentation' : view, renderVars);
|
|
|
}).catch(function(err) {
|
|
|
debug('Error: renderPage()', err);
|
|
|
if (err) {
|
|
|
@@ -556,7 +599,14 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
|
|
|
return renderPage(page, req, res);
|
|
|
- }).catch(function(err) {
|
|
|
+ })
|
|
|
+ // page is not found or the user is forbidden
|
|
|
+ .catch(function(err) {
|
|
|
+
|
|
|
+ let isForbidden = false;
|
|
|
+ if (err.name === 'UserHasNoGrantException') {
|
|
|
+ isForbidden = true;
|
|
|
+ }
|
|
|
|
|
|
const normalizedPath = Page.normalizePath(path);
|
|
|
if (normalizedPath !== path) {
|
|
|
@@ -567,7 +617,7 @@ module.exports = function(crowi, app) {
|
|
|
// これ以前に定義されているはずなので、こうしてしまって問題ない。
|
|
|
if (!Page.isCreatableName(path)) {
|
|
|
// 削除済みページの場合 /trash 以下に移動しているので creatableName になっていないので、表示を許可
|
|
|
- debug('Page is not creatable name.', path);
|
|
|
+ logger.warn('Page is not creatable name.', path);
|
|
|
res.redirect('/');
|
|
|
return ;
|
|
|
}
|
|
|
@@ -585,9 +635,9 @@ module.exports = function(crowi, app) {
|
|
|
return res.redirect(pagePathUtil.encodePagePath(path) + '/');
|
|
|
}
|
|
|
else {
|
|
|
- var fixed = Page.fixToCreatableName(path);
|
|
|
+ const fixed = Page.fixToCreatableName(path);
|
|
|
if (fixed !== path) {
|
|
|
- debug('fixed page name', fixed);
|
|
|
+ logger.warn('fixed page name', fixed);
|
|
|
res.redirect(pagePathUtil.encodePagePath(fixed));
|
|
|
return ;
|
|
|
}
|
|
|
@@ -599,7 +649,7 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
// render editor
|
|
|
debug('Catch pageShow', err);
|
|
|
- return renderPage(null, req, res);
|
|
|
+ return renderPage(null, req, res, isForbidden);
|
|
|
}
|
|
|
}).catch(function(err) {
|
|
|
debug('Error on rendering pageShow (redirect to portal)', err);
|
|
|
@@ -669,11 +719,19 @@ module.exports = function(crowi, app) {
|
|
|
// TODO: move to events
|
|
|
if (notify.slack) {
|
|
|
if (notify.slack.on && notify.slack.channel) {
|
|
|
- data.updateSlackChannel(notify.slack.channel).then(function() {}).catch(function() {});
|
|
|
+ data.updateSlackChannel(notify.slack.channel)
|
|
|
+ .catch(err => {
|
|
|
+ logger.error('Error occured in updating slack channels: ', err);
|
|
|
+ });
|
|
|
|
|
|
if (crowi.slack) {
|
|
|
- notify.slack.channel.split(',').map(function(chan) {
|
|
|
- crowi.slack.post(pageData, req.user, chan, updateOrCreate, previousRevision);
|
|
|
+ const promises = notify.slack.channel.split(',').map(function(chan) {
|
|
|
+ return crowi.slack.postPage(pageData, req.user, chan, updateOrCreate, previousRevision);
|
|
|
+ });
|
|
|
+
|
|
|
+ Promise.all(promises)
|
|
|
+ .catch(err => {
|
|
|
+ logger.error('Error occured in sending slack notification: ', err);
|
|
|
});
|
|
|
}
|
|
|
}
|