middlewares.js 5.0 KB

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