middlewares.js 5.6 KB

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