| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031 |
- /* eslint-disable no-use-before-define */
- module.exports = function(crowi, app) {
- const debug = require('debug')('growi:routes:admin');
- const logger = require('@alias/logger')('growi:routes:admin');
- const models = crowi.models;
- const User = models.User;
- const ExternalAccount = models.ExternalAccount;
- const UserGroup = models.UserGroup;
- const UserGroupRelation = models.UserGroupRelation;
- const GlobalNotificationSetting = models.GlobalNotificationSetting;
- const GlobalNotificationMailSetting = models.GlobalNotificationMailSetting;
- const GlobalNotificationSlackSetting = models.GlobalNotificationSlackSetting; // eslint-disable-line no-unused-vars
- const {
- configManager,
- aclService,
- slackNotificationService,
- exportService,
- } = crowi;
- const recommendedWhitelist = require('@commons/service/xss/recommended-whitelist');
- const ApiResponse = require('../util/apiResponse');
- const importer = require('../util/importer')(crowi);
- const searchEvent = crowi.event('search');
- const MAX_PAGE_LIST = 50;
- const actions = {};
- const { check } = require('express-validator/check');
- const api = {};
- function createPager(total, limit, page, pagesCount, maxPageList) {
- const pager = {
- page,
- pagesCount,
- pages: [],
- total,
- previous: null,
- previousDots: false,
- next: null,
- nextDots: false,
- };
- if (page > 1) {
- pager.previous = page - 1;
- }
- if (page < pagesCount) {
- pager.next = page + 1;
- }
- let pagerMin = Math.max(1, Math.ceil(page - maxPageList / 2));
- let pagerMax = Math.min(pagesCount, Math.floor(page + maxPageList / 2));
- if (pagerMin === 1) {
- if (MAX_PAGE_LIST < pagesCount) {
- pagerMax = MAX_PAGE_LIST;
- }
- else {
- pagerMax = pagesCount;
- }
- }
- if (pagerMax === pagesCount) {
- if ((pagerMax - MAX_PAGE_LIST) < 1) {
- pagerMin = 1;
- }
- else {
- pagerMin = pagerMax - MAX_PAGE_LIST;
- }
- }
- pager.previousDots = null;
- if (pagerMin > 1) {
- pager.previousDots = true;
- }
- pager.nextDots = null;
- if (pagerMax < pagesCount) {
- pager.nextDots = true;
- }
- for (let i = pagerMin; i <= pagerMax; i++) {
- pager.pages.push(i);
- }
- return pager;
- }
- // setup websocket event for rebuild index
- searchEvent.on('addPageProgress', (total, current, skip) => {
- crowi.getIo().sockets.emit('admin:addPageProgress', { total, current, skip });
- });
- searchEvent.on('finishAddPage', (total, current, skip) => {
- crowi.getIo().sockets.emit('admin:finishAddPage', { total, current, skip });
- });
- actions.index = function(req, res) {
- return res.render('admin/index');
- };
- // app.get('/admin/app' , admin.app.index);
- actions.app = {};
- actions.app.index = function(req, res) {
- return res.render('admin/app');
- };
- actions.app.settingUpdate = function(req, res) {
- };
- // app.get('/admin/security' , admin.security.index);
- actions.security = {};
- actions.security.index = function(req, res) {
- const isWikiModeForced = aclService.isWikiModeForced();
- const guestModeValue = aclService.getGuestModeValue();
- return res.render('admin/security', {
- isWikiModeForced,
- guestModeValue,
- });
- };
- // app.get('/admin/markdown' , admin.markdown.index);
- actions.markdown = {};
- actions.markdown.index = function(req, res) {
- const markdownSetting = configManager.getConfigByPrefix('markdown', 'markdown:');
- return res.render('admin/markdown', {
- markdownSetting,
- recommendedWhitelist,
- });
- };
- // app.get('/admin/customize' , admin.customize.index);
- actions.customize = {};
- actions.customize.index = function(req, res) {
- const settingForm = configManager.getConfigByPrefix('crowi', 'customize:');
- // TODO delete after apiV3
- /* eslint-disable quote-props, no-multi-spaces */
- const highlightJsCssSelectorOptions = {
- 'github': { name: '[Light] GitHub', border: false },
- 'github-gist': { name: '[Light] GitHub Gist', border: true },
- 'atom-one-light': { name: '[Light] Atom One Light', border: true },
- 'xcode': { name: '[Light] Xcode', border: true },
- 'vs': { name: '[Light] Vs', border: true },
- 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
- 'hybrid': { name: '[Dark] Hybrid', border: false },
- 'monokai': { name: '[Dark] Monokai', border: false },
- 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
- 'vs2015': { name: '[Dark] Vs 2015', border: false },
- };
- /* eslint-enable quote-props, no-multi-spaces */
- return res.render('admin/customize', {
- settingForm,
- highlightJsCssSelectorOptions,
- });
- };
- // app.get('/admin/notification' , admin.notification.index);
- actions.notification = {};
- actions.notification.index = async(req, res) => {
- const UpdatePost = crowi.model('UpdatePost');
- let slackSetting = configManager.getConfigByPrefix('notification', 'slack:');
- const hasSlackIwhUrl = !!configManager.getConfig('notification', 'slack:incomingWebhookUrl');
- const hasSlackToken = !!configManager.getConfig('notification', 'slack:token');
- if (!hasSlackIwhUrl) {
- slackSetting['slack:incomingWebhookUrl'] = '';
- }
- if (req.session.slackSetting) {
- slackSetting = req.session.slackSetting;
- req.session.slackSetting = null;
- }
- const globalNotifications = await GlobalNotificationSetting.findAll();
- const userNotifications = await UpdatePost.findAll();
- return res.render('admin/notification', {
- userNotifications,
- slackSetting,
- hasSlackIwhUrl,
- hasSlackToken,
- globalNotifications,
- });
- };
- // app.post('/admin/notification/slackSetting' , admin.notification.slackauth);
- actions.notification.slackSetting = async function(req, res) {
- const slackSetting = req.form.slackSetting;
- if (req.form.isValid) {
- await configManager.updateConfigsInTheSameNamespace('notification', slackSetting);
- req.flash('successMessage', ['Successfully Updated!']);
- // Re-setup
- crowi.setupSlack().then(() => {
- });
- }
- else {
- req.flash('errorMessage', req.form.errors);
- }
- return res.redirect('/admin/notification');
- };
- // app.get('/admin/notification/slackAuth' , admin.notification.slackauth);
- actions.notification.slackAuth = function(req, res) {
- const code = req.query.code;
- if (!code || !slackNotificationService.hasSlackConfig()) {
- return res.redirect('/admin/notification');
- }
- const slack = crowi.slack;
- slack.getOauthAccessToken(code)
- .then(async(data) => {
- debug('oauth response', data);
- try {
- await configManager.updateConfigsInTheSameNamespace('notification', { 'slack:token': data.access_token });
- req.flash('successMessage', ['Successfully Connected!']);
- }
- catch (err) {
- req.flash('errorMessage', ['Failed to save access_token. Please try again.']);
- }
- return res.redirect('/admin/notification');
- })
- .catch((err) => {
- debug('oauth response ERROR', err);
- req.flash('errorMessage', ['Failed to fetch access_token. Please do connect again.']);
- return res.redirect('/admin/notification');
- });
- };
- // app.post('/admin/notification/slackIwhSetting' , admin.notification.slackIwhSetting);
- actions.notification.slackIwhSetting = async function(req, res) {
- const slackIwhSetting = req.form.slackIwhSetting;
- if (req.form.isValid) {
- await configManager.updateConfigsInTheSameNamespace('notification', slackIwhSetting);
- req.flash('successMessage', ['Successfully Updated!']);
- // Re-setup
- crowi.setupSlack().then(() => {
- return res.redirect('/admin/notification#slack-incoming-webhooks');
- });
- }
- else {
- req.flash('errorMessage', req.form.errors);
- return res.redirect('/admin/notification#slack-incoming-webhooks');
- }
- };
- // app.post('/admin/notification/slackSetting/disconnect' , admin.notification.disconnectFromSlack);
- actions.notification.disconnectFromSlack = async function(req, res) {
- await configManager.updateConfigsInTheSameNamespace('notification', { 'slack:token': '' });
- req.flash('successMessage', ['Successfully Disconnected!']);
- return res.redirect('/admin/notification');
- };
- actions.globalNotification = {};
- actions.globalNotification.detail = async(req, res) => {
- const notificationSettingId = req.params.id;
- const renderVars = {};
- if (notificationSettingId) {
- try {
- renderVars.setting = await GlobalNotificationSetting.findOne({ _id: notificationSettingId });
- }
- catch (err) {
- logger.error(`Error in finding a global notification setting with {_id: ${notificationSettingId}}`);
- }
- }
- return res.render('admin/global-notification-detail', renderVars);
- };
- actions.globalNotification.create = (req, res) => {
- const form = req.form.notificationGlobal;
- let setting;
- switch (form.notifyToType) {
- case GlobalNotificationSetting.TYPE.MAIL:
- setting = new GlobalNotificationMailSetting(crowi);
- setting.toEmail = form.toEmail;
- break;
- case GlobalNotificationSetting.TYPE.SLACK:
- setting = new GlobalNotificationSlackSetting(crowi);
- setting.slackChannels = form.slackChannels;
- break;
- default:
- logger.error('GlobalNotificationSetting Type Error: undefined type');
- req.flash('errorMessage', 'Error occurred in creating a new global notification setting: undefined notification type');
- return res.redirect('/admin/notification#global-notification');
- }
- setting.triggerPath = form.triggerPath;
- setting.triggerEvents = getNotificationEvents(form);
- setting.save();
- return res.redirect('/admin/notification#global-notification');
- };
- actions.globalNotification.update = async(req, res) => {
- const form = req.form.notificationGlobal;
- const models = {
- [GlobalNotificationSetting.TYPE.MAIL]: GlobalNotificationMailSetting,
- [GlobalNotificationSetting.TYPE.SLACK]: GlobalNotificationSlackSetting,
- };
- let setting = await GlobalNotificationSetting.findOne({ _id: form.id });
- setting = setting.toObject();
- // when switching from one type to another,
- // remove toEmail from slack setting and slackChannels from mail setting
- if (setting.__t !== form.notifyToType) {
- setting = models[setting.__t].hydrate(setting);
- setting.toEmail = undefined;
- setting.slackChannels = undefined;
- await setting.save();
- setting = setting.toObject();
- }
- switch (form.notifyToType) {
- case GlobalNotificationSetting.TYPE.MAIL:
- setting = GlobalNotificationMailSetting.hydrate(setting);
- setting.toEmail = form.toEmail;
- break;
- case GlobalNotificationSetting.TYPE.SLACK:
- setting = GlobalNotificationSlackSetting.hydrate(setting);
- setting.slackChannels = form.slackChannels;
- break;
- default:
- logger.error('GlobalNotificationSetting Type Error: undefined type');
- req.flash('errorMessage', 'Error occurred in updating the global notification setting: undefined notification type');
- return res.redirect('/admin/notification#global-notification');
- }
- setting.__t = form.notifyToType;
- setting.triggerPath = form.triggerPath;
- setting.triggerEvents = getNotificationEvents(form);
- await setting.save();
- return res.redirect('/admin/notification#global-notification');
- };
- actions.globalNotification.remove = async(req, res) => {
- const id = req.params.id;
- try {
- await GlobalNotificationSetting.findOneAndRemove({ _id: id });
- return res.redirect('/admin/notification#global-notification');
- }
- catch (err) {
- req.flash('errorMessage', 'Error in deleting global notification setting');
- return res.redirect('/admin/notification#global-notification');
- }
- };
- const getNotificationEvents = (form) => {
- const triggerEvents = [];
- const triggerEventKeys = Object.keys(form).filter((key) => { return key.match(/^triggerEvent/) });
- triggerEventKeys.forEach((key) => {
- if (form[key]) {
- triggerEvents.push(form[key]);
- }
- });
- return triggerEvents;
- };
- actions.search = {};
- actions.search.index = function(req, res) {
- const search = crowi.getSearcher();
- if (!search) {
- return res.redirect('/admin');
- }
- return res.render('admin/search', {});
- };
- actions.user = {};
- actions.user.index = async function(req, res) {
- return res.render('admin/users');
- };
- // これやったときの relation の挙動未確認
- actions.user.removeCompletely = function(req, res) {
- // ユーザーの物理削除
- const id = req.params.id;
- User.removeCompletelyById(id, (err, removed) => {
- if (err) {
- debug('Error while removing user.', err, id);
- req.flash('errorMessage', '完全な削除に失敗しました。');
- }
- else {
- req.flash('successMessage', '削除しました');
- }
- return res.redirect('/admin/users');
- });
- };
- // app.post('/_api/admin/users.resetPassword' , admin.api.usersResetPassword);
- actions.user.resetPassword = async function(req, res) {
- const id = req.body.user_id;
- const User = crowi.model('User');
- try {
- const newPassword = await User.resetPasswordByRandomString(id);
- const user = await User.findById(id);
- const result = { user: user.toObject(), newPassword };
- return res.json(ApiResponse.success(result));
- }
- catch (err) {
- debug('Error on reseting password', err);
- return res.json(ApiResponse.error(err));
- }
- };
- actions.externalAccount = {};
- actions.externalAccount.index = function(req, res) {
- return res.render('admin/external-accounts');
- };
- actions.externalAccount.remove = async function(req, res) {
- const id = req.params.id;
- let account = null;
- try {
- account = await ExternalAccount.findByIdAndRemove(id);
- if (account == null) {
- throw new Error('削除に失敗しました。');
- }
- }
- catch (err) {
- req.flash('errorMessage', err.message);
- return res.redirect('/admin/users/external-accounts');
- }
- req.flash('successMessage', `外部アカウント '${account.providerType}/${account.accountId}' を削除しました`);
- return res.redirect('/admin/users/external-accounts');
- };
- actions.userGroup = {};
- actions.userGroup.index = function(req, res) {
- const page = parseInt(req.query.page) || 1;
- const isAclEnabled = aclService.isAclEnabled();
- const renderVar = {
- userGroups: [],
- userGroupRelations: new Map(),
- pager: null,
- isAclEnabled,
- };
- UserGroup.findUserGroupsWithPagination({ page })
- .then((result) => {
- const pager = createPager(result.total, result.limit, result.page, result.pages, MAX_PAGE_LIST);
- const userGroups = result.docs;
- renderVar.userGroups = userGroups;
- renderVar.pager = pager;
- return userGroups.map((userGroup) => {
- return new Promise((resolve, reject) => {
- UserGroupRelation.findAllRelationForUserGroup(userGroup)
- .then((relations) => {
- return resolve({
- id: userGroup._id,
- relatedUsers: relations.map((relation) => {
- return relation.relatedUser;
- }),
- });
- });
- });
- });
- })
- .then((allRelationsPromise) => {
- return Promise.all(allRelationsPromise);
- })
- .then((relations) => {
- for (const relation of relations) {
- renderVar.userGroupRelations[relation.id] = relation.relatedUsers;
- }
- debug('in findUserGroupsWithPagination findAllRelationForUserGroupResult', renderVar.userGroupRelations);
- return res.render('admin/user-groups', renderVar);
- })
- .catch((err) => {
- debug('Error on find all relations', err);
- return res.json(ApiResponse.error('Error'));
- });
- };
- // グループ詳細
- actions.userGroup.detail = async function(req, res) {
- const userGroupId = req.params.id;
- const userGroup = await UserGroup.findOne({ _id: userGroupId });
- if (userGroup == null) {
- logger.error('no userGroup is exists. ', userGroupId);
- return res.redirect('/admin/user-groups');
- }
- return res.render('admin/user-group-detail', { userGroup });
- };
- // Importer management
- actions.importer = {};
- actions.importer.api = api;
- api.validators = {};
- api.validators.importer = {};
- actions.importer.index = function(req, res) {
- const settingForm = configManager.getConfigByPrefix('crowi', 'importer:');
- return res.render('admin/importer', {
- settingForm,
- });
- };
- api.validators.importer.esa = function() {
- const validator = [
- check('importer:esa:team_name').not().isEmpty().withMessage('Error. Empty esa:team_name'),
- check('importer:esa:access_token').not().isEmpty().withMessage('Error. Empty esa:access_token'),
- ];
- return validator;
- };
- api.validators.importer.qiita = function() {
- const validator = [
- check('importer:qiita:team_name').not().isEmpty().withMessage('Error. Empty qiita:team_name'),
- check('importer:qiita:access_token').not().isEmpty().withMessage('Error. Empty qiita:access_token'),
- ];
- return validator;
- };
- // Export management
- actions.export = {};
- actions.export.index = (req, res) => {
- return res.render('admin/export');
- };
- actions.export.download = (req, res) => {
- // TODO: add express validator
- const { fileName } = req.params;
- try {
- const zipFile = exportService.getFile(fileName);
- return res.download(zipFile);
- }
- catch (err) {
- // TODO: use ApiV3Error
- logger.error(err);
- return res.json(ApiResponse.error());
- }
- };
- actions.api = {};
- actions.api.securitySetting = async function(req, res) {
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- const form = req.form.settingForm;
- if (aclService.isWikiModeForced()) {
- logger.debug('security:restrictGuestMode will not be changed because wiki mode is forced to set');
- delete form['security:restrictGuestMode'];
- }
- try {
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- return res.json({ status: true });
- }
- catch (err) {
- logger.error(err);
- return res.json({ status: false });
- }
- };
- actions.api.securityPassportLocalSetting = async function(req, res) {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- try {
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- crowi.passportService.resetLocalStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-local:isEnabled')) {
- crowi.passportService.setupLocalStrategy(true);
- }
- }
- catch (err) {
- logger.error(err);
- return res.json({ status: false, message: err.message });
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportLdapSetting = async function(req, res) {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- try {
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- crowi.passportService.resetLdapStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')) {
- crowi.passportService.setupLdapStrategy(true);
- }
- }
- catch (err) {
- logger.error(err);
- return res.json({ status: false, message: err.message });
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportSamlSetting = async(req, res) => {
- const form = req.form.settingForm;
- validateSamlSettingForm(req.form, req.t);
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetSamlStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-saml:isEnabled')) {
- try {
- await crowi.passportService.setupSamlStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetSamlStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportBasicSetting = async(req, res) => {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetBasicStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-basic:isEnabled')) {
- try {
- await crowi.passportService.setupBasicStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetBasicStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportGoogleSetting = async(req, res) => {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetGoogleStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-google:isEnabled')) {
- try {
- await crowi.passportService.setupGoogleStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetGoogleStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportGitHubSetting = async(req, res) => {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetGitHubStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-github:isEnabled')) {
- try {
- await crowi.passportService.setupGitHubStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetGitHubStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportTwitterSetting = async(req, res) => {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetTwitterStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-twitter:isEnabled')) {
- try {
- await crowi.passportService.setupTwitterStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetTwitterStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- actions.api.securityPassportOidcSetting = async(req, res) => {
- const form = req.form.settingForm;
- if (!req.form.isValid) {
- return res.json({ status: false, message: req.form.errors.join('\n') });
- }
- debug('form content', form);
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- // reset strategy
- await crowi.passportService.resetOidcStrategy();
- // setup strategy
- if (configManager.getConfig('crowi', 'security:passport-oidc:isEnabled')) {
- try {
- await crowi.passportService.setupOidcStrategy(true);
- }
- catch (err) {
- // reset
- await crowi.passportService.resetOidcStrategy();
- return res.json({ status: false, message: err.message });
- }
- }
- return res.json({ status: true });
- };
- // app.post('/_api/admin/notifications.add' , admin.api.notificationAdd);
- actions.api.notificationAdd = function(req, res) {
- const UpdatePost = crowi.model('UpdatePost');
- const pathPattern = req.body.pathPattern;
- const channel = req.body.channel;
- debug('notification.add', pathPattern, channel);
- UpdatePost.create(pathPattern, channel, req.user)
- .then((doc) => {
- debug('Successfully save updatePost', doc);
- // fixme: うーん
- doc.creator = doc.creator._id.toString();
- return res.json(ApiResponse.success({ updatePost: doc }));
- })
- .catch((err) => {
- debug('Failed to save updatePost', err);
- return res.json(ApiResponse.error());
- });
- };
- // app.post('/_api/admin/notifications.remove' , admin.api.notificationRemove);
- actions.api.notificationRemove = function(req, res) {
- const UpdatePost = crowi.model('UpdatePost');
- const id = req.body.id;
- UpdatePost.remove(id)
- .then(() => {
- debug('Successfully remove updatePost');
- return res.json(ApiResponse.success({}));
- })
- .catch((err) => {
- debug('Failed to remove updatePost', err);
- return res.json(ApiResponse.error());
- });
- };
- // app.get('/_api/admin/users.search' , admin.api.userSearch);
- actions.api.usersSearch = function(req, res) {
- const User = crowi.model('User');
- const email = req.query.email;
- User.findUsersByPartOfEmail(email, {})
- .then((users) => {
- const result = {
- data: users,
- };
- return res.json(ApiResponse.success(result));
- })
- .catch((err) => {
- return res.json(ApiResponse.error());
- });
- };
- actions.api.toggleIsEnabledForGlobalNotification = async(req, res) => {
- const id = req.query.id;
- const isEnabled = (req.query.isEnabled === 'true');
- try {
- if (isEnabled) {
- await GlobalNotificationSetting.enable(id);
- }
- else {
- await GlobalNotificationSetting.disable(id);
- }
- return res.json(ApiResponse.success());
- }
- catch (err) {
- return res.json(ApiResponse.error());
- }
- };
- /**
- * save esa settings, update config cache, and response json
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.importerSettingEsa = async(req, res) => {
- const form = req.body;
- const { validationResult } = require('express-validator');
- const errors = validationResult(req);
- if (!errors.isEmpty()) {
- return res.json(ApiResponse.error('esa.io form is blank'));
- }
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- importer.initializeEsaClient(); // let it run in the back aftert res
- return res.json(ApiResponse.success());
- };
- /**
- * save qiita settings, update config cache, and response json
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.importerSettingQiita = async(req, res) => {
- const form = req.body;
- const { validationResult } = require('express-validator');
- const errors = validationResult(req);
- if (!errors.isEmpty()) {
- return res.json(ApiResponse.error('Qiita form is blank'));
- }
- await configManager.updateConfigsInTheSameNamespace('crowi', form);
- importer.initializeQiitaClient(); // let it run in the back aftert res
- return res.json(ApiResponse.success());
- };
- /**
- * Import all posts from esa
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.importDataFromEsa = async(req, res) => {
- const user = req.user;
- let errors;
- try {
- errors = await importer.importDataFromEsa(user);
- }
- catch (err) {
- errors = [err];
- }
- if (errors.length > 0) {
- return res.json(ApiResponse.error(`<br> - ${errors.join('<br> - ')}`));
- }
- return res.json(ApiResponse.success());
- };
- /**
- * Import all posts from qiita
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.importDataFromQiita = async(req, res) => {
- const user = req.user;
- let errors;
- try {
- errors = await importer.importDataFromQiita(user);
- }
- catch (err) {
- errors = [err];
- }
- if (errors.length > 0) {
- return res.json(ApiResponse.error(`<br> - ${errors.join('<br> - ')}`));
- }
- return res.json(ApiResponse.success());
- };
- /**
- * Test connection to esa and response result with json
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.testEsaAPI = async(req, res) => {
- try {
- await importer.testConnectionToEsa();
- return res.json(ApiResponse.success());
- }
- catch (err) {
- return res.json(ApiResponse.error(err));
- }
- };
- /**
- * Test connection to qiita and response result with json
- *
- * @param {*} req
- * @param {*} res
- */
- actions.api.testQiitaAPI = async(req, res) => {
- try {
- await importer.testConnectionToQiita();
- return res.json(ApiResponse.success());
- }
- catch (err) {
- return res.json(ApiResponse.error(err));
- }
- };
- actions.api.searchBuildIndex = async function(req, res) {
- const search = crowi.getSearcher();
- if (!search) {
- return res.json(ApiResponse.error('ElasticSearch Integration is not set up.'));
- }
- try {
- search.buildIndex();
- }
- catch (err) {
- return res.json(ApiResponse.error(err));
- }
- return res.json(ApiResponse.success());
- };
- /**
- * validate setting form values for SAML
- *
- * This validation checks, for the value of each mandatory items,
- * whether it from the environment variables is empty and form value to update it is empty.
- */
- function validateSamlSettingForm(form, t) {
- for (const key of crowi.passportService.mandatoryConfigKeysForSaml) {
- const formValue = form.settingForm[key];
- if (configManager.getConfigFromEnvVars('crowi', key) === null && formValue === '') {
- const formItemName = t(`security_setting.form_item_name.${key}`);
- form.errors.push(t('form_validation.required', formItemName));
- }
- }
- }
- return actions;
- };
|