| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- 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
- , MAX_PAGE_LIST = 5
- , actions = {};
- function createPager(currentPage, pageCount, itemCount, maxPageList) {
- var pager = {};
- pager.currentPage = currentPage;
- pager.pageCount = pageCount;
- pager.itemCount = itemCount;
- pager.previous = null;
- if (currentPage > 1) {
- pager.previous = currentPage - 1;
- }
- pager.next = null;
- if (currentPage < pageCount) {
- pager.next = currentPage + 1;
- }
- pager.pages = [];
- var pagerMin = Math.max(1, Math.ceil(currentPage - maxPageList/2));
- var pagerMax = Math.min(pageCount, Math.floor(currentPage + maxPageList/2));
- if (pagerMin == 1) {
- if (MAX_PAGE_LIST < pageCount) {
- pagerMax = MAX_PAGE_LIST;
- } else {
- pagerMax = pageCount;
- }
- }
- if (pagerMax == pageCount) {
- 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 < pageCount) {
- pager.nextDots = true;
- }
- for (var i = pagerMin;
- i <= pagerMax;
- i++) {
- pager.pages.push(i);
- }
- return pager;
- }
- actions.index = function(req, res) {
- return res.render('admin/index');
- };
- actions.app = {};
- actions.app.index = function(req, res) {
- var settingForm;
- settingForm = Config.setupCofigFormData('crowi', req.config);
- return res.render('admin/app', {
- settingForm: settingForm,
- });
- };
- actions.app.settingUpdate = function(req, res) {
- };
- // app.get('/admin/notification' , admin.notification.index);
- actions.notification = {};
- actions.notification.index = function(req, res) {
- var config = crowi.getConfig();
- 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;
- }
- return res.render('admin/notification', {
- slackSetting,
- hasSlackConfig,
- hasSlackToken,
- slackAuthUrl
- });
- };
- // app.post('/admin/notification/slackSetting' , admin.notification.slackauth);
- 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.user = {};
- actions.user.index = function(req, res) {
- var page = parseInt(req.query.page) || 1;
- User.findUsersWithPagination({page: page}, function(err, users, pageCount, itemCount) {
- var pager = createPager(page, pageCount, itemCount, MAX_PAGE_LIST);
- return res.render('admin/users', {
- users: users,
- 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');
- };
- 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');
- });
- };
- actions.slackauth = function(req, res) {
- //debug(req.query.code);
- //var Botkit = require('botkit');
- //var controller = Botkit.slackbot({debug: true});
- //var config = crowi.getConfig();
- //controller.configureSlackApp({
- // clientId: '2462768682.25922619424',
- // clientSecret: '2bbbc1c5d355e128ffd009a5d57629b9',
- // redirectUri: config.crowi['app:url'] + '/slackauth',
- // scopes: ['chat:write:bot']
- //});
- //var bot = controller.spawn();
- //bot.api.oauth.access({code: req.query.code}, function (err, response) {
- // debug(err, response);
- //});
- //var accessToken = 'xoxp-2462768682-2462768686-25940861495-3e4cc9168a';
- //var accessToken = 'xoxp-2462768682-2462768686-26103706022-46943fb5ec';
- //
- // access_token: '',
- // scope: 'identify,chat:write:bot',
- // team_name: '',
- // team_id: '' }
- //var bot = controller.spawn({token: accessToken});
- //bot.api.chat.postMessage({
- // channel: '#xtest2',
- // username: 'Crowi',
- // text: '/hoge/fuga/piyo is updated.',
- // attachments: [
- // {
- // color: '#263a3c',
- // author_name: '@sotarok',
- // author_link: 'http://localhost:3000/user/sotarok',
- // author_icon: 'https://crowi-strk-dev.s3.amazonaws.com/user/56c9dbf860ab5bc62647d84a.png',
- // title: "/hoge/fuga/piyo",
- // title_link: "http://localhost:3000/hoge/fuga/piyo",
- // text: '*# sotarok*\n\nshellに続き、初心者が頑張って理解していくノート的記事です。\nGitHubが常識と化してきた今日この頃、チャレンジしてみたものの、そもそもGitってなんなのかわからないと使いこなせない、っていうか挫折すると思います。私はしました。\nなので、起き上がってまずGitについて本当に一から理解を試みました。\n\n***\n \n*## Gitって何?*\n\nGitとは、**[バージョン管理システム](https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%E7%AE%A1%E7%90%86%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0)**のひとつです。\n バージョン管理‥? :open_mouth: \nバージョンすなわち変更履歴の管理です。\n\n例えばなにかファイルの書き換えをするとき、元データを失わないためにどのような工夫をするでしょうか。ほとんどの場合、保存用にコピーを作成しておくというのが定番の方法だと思います。\nですが、誰しもこんな経験があるのでは。\n\n - 元データの保管が面倒\n - \'meeting_3月.txt\'、\'meeting_最新.txt\'といった名前のデータが複数できてしまう\n - チームで触っていて、最後に書き換えたのが誰なのかわからない\n - 先輩も同時に編集していたのに、知らずにタッチの差で上書きしてしまった\n - 書き換えたよ~と言われても(あるいは自分でも)、どこを換えたかわからない\n - 何回か前の更新状態の方が良かった、戻したい…\n \n ある~~~!!\n こんな事態を解消してくれるのが**Gitというバージョン管理システム**です。',
- // mrkdwn_in: ["text"],
- // },
- // ],
- //});
- //var code = req.query.);
- res.render('admin/slackauth', {});
- };
- 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')});
- }
- };
- // app.post('/_api/admin/notifications.add' , admin.api.notificationAdd);
- actions.api.notificationAdd = function(req, res) {
- };
- // app.post('/_api/admin/notifications.remove' , admin.api.notificationRemove);
- actions.api.notificationRemove = function(req, res) {
- };
- 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;
- };
|