middlewares.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. var debug = require('debug')('crowi:lib:middlewares');
  2. exports.loginChecker = function(crowi, app) {
  3. return function(req, res, next) {
  4. var User = crowi.model('User');
  5. var csrfKey = (req.session && req.session.id) || 'anon';
  6. if (req.csrfToken === null) {
  7. req.csrfToken = crowi.getTokens().create(csrfKey);
  8. }
  9. // session に user object が入ってる
  10. if (req.session.user && '_id' in req.session.user) {
  11. User.findById(req.session.user._id, function(err, userData) {
  12. if (err) {
  13. next();
  14. } else {
  15. req.user = req.session.user = userData;
  16. res.locals.user = req.user;
  17. next();
  18. }
  19. });
  20. } else {
  21. req.user = req.session.user = false;
  22. res.locals.user = req.user;
  23. next();
  24. }
  25. };
  26. };
  27. exports.csrfVerify = function(crowi, app) {
  28. return function(req, res, next) {
  29. var token = req.body._csrf || req.query._csrf || null;
  30. var csrfKey = (req.session && req.session.id) || 'anon';
  31. if (req.skipCsrfVerify) {
  32. return next();
  33. }
  34. if (crowi.getTokens().verify(csrfKey, token)) {
  35. return next();
  36. }
  37. return res.sendStatus(403);
  38. };
  39. };
  40. exports.swigFunctions = function(crowi, app) {
  41. return function(req, res, next) {
  42. require('../util/swigFunctions')(crowi, app, req, res.locals);
  43. next();
  44. };
  45. };
  46. exports.swigFilters = function(app, swig) {
  47. return function(req, res, next) {
  48. swig.setFilter('path2name', function(string) {
  49. var name = string.replace(/(\/)$/, '');
  50. if (name.match(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/)) { // /.../hoge/YYYY/MM/DD 形式のページ
  51. return name.replace(/.+\/([^/]+\/\d{4}\/\d{2}\/\d{2})$/, '$1');
  52. }
  53. if (name.match(/.+\/([^/]+\/\d{4}\/\d{2})$/)) { // /.../hoge/YYYY/MM 形式のページ
  54. return name.replace(/.+\/([^/]+\/\d{4}\/\d{2})$/, '$1');
  55. }
  56. if (name.match(/.+\/([^/]+\/\d{4})$/)) { // /.../hoge/YYYY 形式のページ
  57. return name.replace(/.+\/([^/]+\/\d{4})$/, '$1');
  58. }
  59. return name.replace(/.+\/(.+)?$/, '$1'); // ページの末尾を拾う
  60. });
  61. swig.setFilter('datetz', function(input, format) {
  62. // timezone
  63. var swigFilters = require('swig/lib/filters');
  64. return swigFilters.date(input, format, app.get('tzoffset'));
  65. });
  66. swig.setFilter('nl2br', function(string) {
  67. return string
  68. .replace(/\n/g, '<br>');
  69. });
  70. swig.setFilter('insertSpaceToEachSlashes', function(string) {
  71. if (string == '/') {
  72. return string;
  73. }
  74. return string.replace(/\//g, ' / ');
  75. });
  76. swig.setFilter('removeLastSlash', function(string) {
  77. if (string == '/') {
  78. return string;
  79. }
  80. return string.substr(0, string.length - 1);
  81. });
  82. swig.setFilter('presentation', function(string) {
  83. // 手抜き
  84. return string
  85. .replace(/[\n]+#/g, '\n\n\n#')
  86. .replace(/\s(https?.+(jpe?g|png|gif))\s/, '\n\n\n![]($1)\n\n\n');
  87. });
  88. swig.setFilter('picture', function(user) {
  89. if (!user) {
  90. return '';
  91. }
  92. user.fbId = user.userId; // migration
  93. if (user.image && user.image != '/images/userpicture.png') {
  94. return user.image;
  95. } else if (user.fbId) {
  96. return '//graph.facebook.com/' + user.fbId + '/picture?size=square';
  97. } else {
  98. return '/images/userpicture.png';
  99. }
  100. });
  101. next();
  102. };
  103. };
  104. exports.adminRequired = function() {
  105. return function(req, res, next) {
  106. if (req.user && '_id' in req.user) {
  107. if (req.user.admin) {
  108. next();
  109. return;
  110. }
  111. return res.redirect('/');
  112. }
  113. return res.redirect('/login');
  114. };
  115. };
  116. exports.loginRequired = function(crowi, app) {
  117. return function(req, res, next) {
  118. var User = crowi.model('User')
  119. if (req.user && '_id' in req.user) {
  120. if (req.user.status === User.STATUS_ACTIVE) {
  121. // Active の人だけ先に進める
  122. return next();
  123. } else if (req.user.status === User.STATUS_REGISTERED) {
  124. return res.redirect('/login/error/registered');
  125. } else if (req.user.status === User.STATUS_SUSPENDED) {
  126. return res.redirect('/login/error/suspended');
  127. } else if (req.user.status === User.STATUS_INVITED) {
  128. return res.redirect('/login/invited');
  129. }
  130. }
  131. req.session.jumpTo = req.originalUrl;
  132. return res.redirect('/login');
  133. };
  134. };
  135. exports.accessTokenParser = function(crowi, app) {
  136. return function(req, res, next) {
  137. var accessToken = req.query.access_token || req.body.access_token || null;
  138. if (!accessToken) {
  139. return next();
  140. }
  141. var User = crowi.model('User')
  142. User.findUserByApiToken(accessToken)
  143. .then(function(userData) {
  144. req.user = userData;
  145. req.skipCsrfVerify = true;
  146. next();
  147. }).catch(function(err) {
  148. next();
  149. });
  150. };
  151. };
  152. // this is for Installer
  153. exports.applicationNotInstalled = function() {
  154. return function(req, res, next) {
  155. var config = req.config;
  156. if (Object.keys(config.crowi).length !== 1) {
  157. req.flash('errorMessage', 'Application already installed.');
  158. return res.redirect('admin'); // admin以外はadminRequiredで'/'にリダイレクトされる
  159. }
  160. return next();
  161. };
  162. };
  163. exports.applicationInstalled = function() {
  164. return function(req, res, next) {
  165. var config = req.config;
  166. if (Object.keys(config.crowi).length === 1) { // app:url is set by process
  167. return res.redirect('/installer');
  168. }
  169. return next();
  170. };
  171. };
  172. exports.awsEnabled = function() {
  173. return function (req, res, next) {
  174. var config = req.config;
  175. if (config.crowi['aws:region'] !== '' && config.crowi['aws:bucket'] !== '' && config.crowi['aws:accessKeyId'] !== '' && config.crowi['aws:secretAccessKey'] !== '') {
  176. req.flash('globalError', 'AWS settings required to use this function. Please ask the administrator.');
  177. return res.redirect('/');
  178. }
  179. return next();
  180. };
  181. };