| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- module.exports = function(crowi, app) {
- 'use strict';
- var debug = require('debug')('crowi:routes:admin')
- , models = crowi.models
- , Page = models.Page
- , User = models.User
- , Config = models.Config
- , PluginUtils = require('../plugins/plugin-utils')
- , pluginUtils = new PluginUtils()
- , ApiResponse = require('../util/apiResponse')
- , MAX_PAGE_LIST = 5
- , actions = {};
- function createPager(total, limit, page, pagesCount, maxPageList) {
- const pager = {
- page: page,
- pagesCount: pagesCount,
- pages: [],
- total: 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;
- }
- 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) {
- var settingForm;
- settingForm = Config.setupCofigFormData('crowi', req.config);
- return res.render('admin/app', {
- settingForm: settingForm,
- plugins: pluginUtils.listPlugins(crowi.rootDir),
- });
- };
- actions.app.settingUpdate = function(req, res) {
- };
- // app.get('/admin/markdonw' , admin.markdonw.index);
- actions.markdown = {};
- actions.markdown.index = function(req, res) {
- var config = crowi.getConfig();
- var markdownSetting = Config.setupCofigFormData('markdown', config);
- return res.render('admin/markdown', {
- markdownSetting: markdownSetting,
- });
- };
- // app.post('/admin/markdown/lineBreaksSetting' , admin.markdown.lineBreaksSetting);
- actions.markdown.lineBreaksSetting = function(req, res) {
- var markdownSetting = req.form.markdownSetting;
- req.session.markdownSetting = markdownSetting;
- if (req.form.isValid) {
- Config.updateNamespaceByArray('markdown', markdownSetting, function(err, config) {
- Config.updateConfigCache('markdown', config);
- req.session.markdownSetting = null;
- req.flash('successMessage', ['Successfully updated!']);
- return res.redirect('/admin/markdown');
- });
- } else {
- req.flash('errorMessage', req.form.errors);
- return res.redirect('/admin/markdown');
- }
- };
- // app.get('/admin/customize' , admin.customize.index);
- actions.customize = {};
- actions.customize.index = function(req, res) {
- var settingForm;
- settingForm = Config.setupCofigFormData('crowi', req.config);
- return res.render('admin/customize', {
- settingForm: settingForm,
- });
- };
- // app.get('/admin/notification' , admin.notification.index);
- actions.notification = {};
- actions.notification.index = function(req, res) {
- var config = crowi.getConfig();
- var UpdatePost = crowi.model('UpdatePost');
- var slackSetting = Config.setupCofigFormData('notification', config);
- var hasSlackConfig = Config.hasSlackConfig(config);
- var hasSlackToken = Config.hasSlackToken(config);
- var slack = crowi.slack;
- var slackAuthUrl = '';
- if (!Config.hasSlackConfig(req.config)) {
- slackSetting['slack:clientId'] = '';
- slackSetting['slack:clientSecret'] = '';
- } else {
- slackAuthUrl = slack.getAuthorizeURL();
- }
- if (req.session.slackSetting) {
- slackSetting = req.session.slackSetting;
- req.session.slackSetting = null;
- }
- UpdatePost.findAll()
- .then(function(settings) {
- return res.render('admin/notification', {
- settings,
- slackSetting,
- hasSlackConfig,
- hasSlackToken,
- slackAuthUrl
- });
- });
- };
- // app.post('/admin/notification/slackSetting' , admin.notification.slackSetting);
- actions.notification.slackSetting = function(req, res) {
- var slackSetting = req.form.slackSetting;
- req.session.slackSetting = slackSetting;
- if (req.form.isValid) {
- Config.updateNamespaceByArray('notification', slackSetting, function(err, config) {
- Config.updateConfigCache('notification', config);
- req.session.slackSetting = null;
- crowi.setupSlack().then(function() {
- return res.redirect('/admin/notification');
- });
- });
- } 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) {
- var code = req.query.code;
- var config = crowi.getConfig();
- if (!code || !Config.hasSlackConfig(req.config)) {
- return res.redirect('/admin/notification');
- }
- var slack = crowi.slack;
- var bot = slack.createBot();
- bot.api.oauth.access({code}, function(err, data) {
- debug('oauth response', err, data);
- if (!data.ok || !data.access_token) {
- req.flash('errorMessage', ['Failed to fetch access_token. Please do connect again.']);
- return res.redirect('/admin/notification');
- } else {
- Config.updateNamespaceByArray('notification', {'slack:token': data.access_token}, function(err, config) {
- if (err) {
- req.flash('errorMessage', ['Failed to save access_token. Please try again.']);
- } else {
- Config.updateConfigCache('notification', config);
- req.flash('successMessage', ['Successfully Connected!']);
- }
- slack.createBot();
- return res.redirect('/admin/notification');
- });
- }
- });
- };
- actions.search = {};
- actions.search.index = function(req, res) {
- var search = crowi.getSearcher();
- if (!search) {
- return res.redirect('/admin');
- }
- return res.render('admin/search', {
- });
- };
- actions.search.buildIndex = function(req, res) {
- var search = crowi.getSearcher();
- if (!search) {
- return res.redirect('/admin');
- }
- Promise.resolve().then(function() {
- return new Promise(function(resolve, reject) {
- search.deleteIndex()
- .then(function(data) {
- debug('Index deleted.');
- resolve();
- }).catch(function(err) {
- debug('Delete index Error, but if it is initialize, its ok.', err);
- resolve();
- });
- });
- }).then(function() {
- search.buildIndex()
- .then(function(data) {
- if (!data.errors) {
- debug('Index created.');
- }
- return search.addAllPages();
- })
- .then(function(data) {
- if (!data.errors) {
- debug('Data is successfully indexed.');
- } else {
- debug('Data index error.', data.errors);
- }
- })
- .catch(function(err) {
- debug('Error', err);
- });
- req.flash('successMessage', 'Now re-building index ... this takes a while.');
- return res.redirect('/admin/search');
- });
- };
- actions.user = {};
- actions.user.index = function(req, res) {
- var page = parseInt(req.query.page) || 1;
- User.findUsersWithPagination({page: page}, function(err, result) {
- const pager = createPager(result.total, result.limit, result.page, result.pages, MAX_PAGE_LIST);
- return res.render('admin/users', {
- users: result.docs,
- pager: pager
- });
- });
- };
- actions.user.invite = function(req, res) {
- var form = req.form.inviteForm;
- var toSendEmail = form.sendEmail || false;
- if (req.form.isValid) {
- User.createUsersByInvitation(form.emailList.split('\n'), toSendEmail, function(err, userList) {
- if (err) {
- req.flash('errorMessage', req.form.errors.join('\n'));
- } else {
- req.flash('createdUser', userList);
- }
- return res.redirect('/admin/users');
- });
- } else {
- req.flash('errorMessage', req.form.errors.join('\n'));
- return res.redirect('/admin/users');
- }
- };
- actions.user.makeAdmin = function(req, res) {
- var id = req.params.id;
- User.findById(id, function(err, userData) {
- userData.makeAdmin(function(err, userData) {
- if (err === null) {
- req.flash('successMessage', userData.name + 'さんのアカウントを管理者に設定しました。');
- } else {
- req.flash('errorMessage', '更新に失敗しました。');
- debug(err, userData);
- }
- return res.redirect('/admin/users');
- });
- });
- };
- actions.user.removeFromAdmin = function(req, res) {
- var id = req.params.id;
- User.findById(id, function(err, userData) {
- userData.removeFromAdmin(function(err, userData) {
- if (err === null) {
- req.flash('successMessage', userData.name + 'さんのアカウントを管理者から外しました。');
- } else {
- req.flash('errorMessage', '更新に失敗しました。');
- debug(err, userData);
- }
- return res.redirect('/admin/users');
- });
- });
- };
- actions.user.activate = function(req, res) {
- var id = req.params.id;
- User.findById(id, function(err, userData) {
- userData.statusActivate(function(err, userData) {
- if (err === null) {
- req.flash('successMessage', userData.name + 'さんのアカウントを承認しました');
- } else {
- req.flash('errorMessage', '更新に失敗しました。');
- debug(err, userData);
- }
- return res.redirect('/admin/users');
- });
- });
- };
- actions.user.suspend = function(req, res) {
- var id = req.params.id;
- User.findById(id, function(err, userData) {
- userData.statusSuspend(function(err, userData) {
- if (err === null) {
- req.flash('successMessage', userData.name + 'さんのアカウントを利用停止にしました');
- } else {
- req.flash('errorMessage', '更新に失敗しました。');
- debug(err, userData);
- }
- return res.redirect('/admin/users');
- });
- });
- };
- actions.user.remove = function(req, res) {
- // 未実装
- return res.redirect('/admin/users');
- };
- // これやったときの relation の挙動未確認
- actions.user.removeCompletely = function(req, res) {
- // ユーザーの物理削除
- var id = req.params.id;
- User.removeCompletelyById(id, function(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 = function(req, res) {
- const id = req.body.user_id;
- const User = crowi.model('User');
- User.resetPasswordByRandomString(id)
- .then(function(data) {
- data.user = User.filterToPublicFields(data.user);
- return res.json(ApiResponse.success(data));
- }).catch(function(err) {
- debug('Error on reseting password', err);
- return res.json(ApiResponse.error('Error'));
- });
- }
- actions.api = {};
- actions.api.appSetting = function(req, res) {
- var form = req.form.settingForm;
- if (req.form.isValid) {
- debug('form content', form);
- // mail setting ならここで validation
- if (form['mail:from']) {
- validateMailSetting(req, form, function(err, data) {
- debug('Error validate mail setting: ', err, data);
- if (err) {
- req.form.errors.push('SMTPを利用したテストメール送信に失敗しました。設定をみなおしてください。');
- return res.json({status: false, message: req.form.errors.join('\n')});
- }
- return saveSetting(req, res, form);
- });
- } else {
- return saveSetting(req, res, form);
- }
- } else {
- return res.json({status: false, message: req.form.errors.join('\n')});
- }
- };
- actions.api.customizeSetting = function(req, res) {
- var form = req.form.settingForm;
- if (req.form.isValid) {
- debug('form content', form);
- return saveSetting(req, res, form);
- } else {
- return res.json({status: false, message: req.form.errors.join('\n')});
- }
- }
- // app.post('/_api/admin/notifications.add' , admin.api.notificationAdd);
- actions.api.notificationAdd = function(req, res) {
- var UpdatePost = crowi.model('UpdatePost');
- var pathPattern = req.body.pathPattern;
- var channel = req.body.channel;
- debug('notification.add', pathPattern, channel);
- UpdatePost.create(pathPattern, channel, req.user)
- .then(function(doc) {
- debug('Successfully save updatePost', doc);
- // fixme: うーん
- doc.creator = doc.creator._id.toString();
- return res.json(ApiResponse.success({updatePost: doc}));
- }).catch(function(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) {
- var UpdatePost = crowi.model('UpdatePost');
- var id = req.body.id;
- UpdatePost.remove(id)
- .then(function() {
- debug('Successfully remove updatePost');
- return res.json(ApiResponse.success({}));
- }).catch(function(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());
- });
- };
- function saveSetting(req, res, form)
- {
- Config.updateNamespaceByArray('crowi', form, function(err, config) {
- Config.updateConfigCache('crowi', config);
- return res.json({status: true});
- });
- }
- function validateMailSetting(req, form, callback)
- {
- var mailer = crowi.mailer;
- var option = {
- host: form['mail:smtpHost'],
- port: form['mail:smtpPort'],
- };
- if (form['mail:smtpUser'] && form['mail:smtpPassword']) {
- option.auth = {
- user: form['mail:smtpUser'],
- pass: form['mail:smtpPassword'],
- };
- }
- if (option.port === 465) {
- option.secure = true;
- }
- var smtpClient = mailer.createSMTPClient(option);
- debug('mailer setup for validate SMTP setting', smtpClient);
- smtpClient.sendMail({
- to: req.user.email,
- subject: 'Wiki管理設定のアップデートによるメール通知',
- text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。'
- }, callback);
- }
- return actions;
- };
|