middlewares.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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('normalizeDateInPath', function(path) {
  62. var patterns = [
  63. [/20(\d{2})(\d{2})(\d{2})(.+)/g, '20$1/$2/$3/$4'],
  64. [/20(\d{2})(\d{2})(\d{2})/g, '20$1/$2/$3'],
  65. [/20(\d{2})(\d{2})(.+)/g, '20$1/$2/$3'],
  66. [/20(\d{2})(\d{2})/g, '20$1/$2'],
  67. [/20(\d{2})_(\d{1,2})_(\d{1,2})_?(.+)/g, '20$1/$2/$3/$4'],
  68. [/20(\d{2})_(\d{1,2})_(\d{1,2})/g, '20$1/$2/$3'],
  69. [/20(\d{2})_(\d{1,2})_?(.+)/g, '20$1/$2/$3'],
  70. [/20(\d{2})_(\d{1,2})/g, '20$1/$2'],
  71. ];
  72. for (var i = 0; i < patterns.length ; i++) {
  73. var mat = patterns[i][0];
  74. var rep = patterns[i][1];
  75. if (path.match(mat)) {
  76. return path.replace(mat, rep);
  77. }
  78. }
  79. return path;
  80. });
  81. swig.setFilter('datetz', function(input, format) {
  82. // timezone
  83. var swigFilters = require('swig/lib/filters');
  84. return swigFilters.date(input, format, app.get('tzoffset'));
  85. });
  86. swig.setFilter('nl2br', function(string) {
  87. return string
  88. .replace(/\n/g, '<br>');
  89. });
  90. swig.setFilter('insertSpaceToEachSlashes', function(string) {
  91. if (string == '/') {
  92. return string;
  93. }
  94. return string.replace(/\//g, ' / ');
  95. });
  96. swig.setFilter('removeLastSlash', function(string) {
  97. if (string == '/') {
  98. return string;
  99. }
  100. return string.substr(0, string.length - 1);
  101. });
  102. swig.setFilter('presentation', function(string) {
  103. // 手抜き
  104. return string
  105. .replace(/[\n]+#/g, '\n\n\n#')
  106. .replace(/\s(https?.+(jpe?g|png|gif))\s/, '\n\n\n![]($1)\n\n\n');
  107. });
  108. swig.setFilter('picture', function(user) {
  109. if (!user) {
  110. return '';
  111. }
  112. user.fbId = user.userId; // migration
  113. if (user.image && user.image != '/images/userpicture.png') {
  114. return user.image;
  115. } else if (user.fbId) {
  116. return '//graph.facebook.com/' + user.fbId + '/picture?size=square';
  117. } else {
  118. return '/images/userpicture.png';
  119. }
  120. });
  121. next();
  122. };
  123. };
  124. exports.adminRequired = function() {
  125. return function(req, res, next) {
  126. if (req.user && '_id' in req.user) {
  127. if (req.user.admin) {
  128. next();
  129. return;
  130. }
  131. return res.redirect('/');
  132. }
  133. return res.redirect('/login');
  134. };
  135. };
  136. exports.loginRequired = function(crowi, app) {
  137. return function(req, res, next) {
  138. var User = crowi.model('User')
  139. if (req.user && '_id' in req.user) {
  140. if (req.user.status === User.STATUS_ACTIVE) {
  141. // Active の人だけ先に進める
  142. return next();
  143. } else if (req.user.status === User.STATUS_REGISTERED) {
  144. return res.redirect('/login/error/registered');
  145. } else if (req.user.status === User.STATUS_SUSPENDED) {
  146. return res.redirect('/login/error/suspended');
  147. } else if (req.user.status === User.STATUS_INVITED) {
  148. return res.redirect('/login/invited');
  149. }
  150. }
  151. // is api path
  152. var path = req.path || '';
  153. if (path.match(/^\/_api\/.+$/)) {
  154. return res.sendStatus(403);
  155. }
  156. req.session.jumpTo = req.originalUrl;
  157. return res.redirect('/login');
  158. };
  159. };
  160. exports.accessTokenParser = function(crowi, app) {
  161. return function(req, res, next) {
  162. var accessToken = req.query.access_token || req.body.access_token || null;
  163. if (!accessToken) {
  164. return next();
  165. }
  166. var User = crowi.model('User')
  167. User.findUserByApiToken(accessToken)
  168. .then(function(userData) {
  169. req.user = userData;
  170. req.skipCsrfVerify = true;
  171. next();
  172. }).catch(function(err) {
  173. next();
  174. });
  175. };
  176. };
  177. // this is for Installer
  178. exports.applicationNotInstalled = function() {
  179. return function(req, res, next) {
  180. var config = req.config;
  181. if (Object.keys(config.crowi).length !== 1) {
  182. req.flash('errorMessage', 'Application already installed.');
  183. return res.redirect('admin'); // admin以外はadminRequiredで'/'にリダイレクトされる
  184. }
  185. return next();
  186. };
  187. };
  188. exports.applicationInstalled = function() {
  189. return function(req, res, next) {
  190. var config = req.config;
  191. if (Object.keys(config.crowi).length === 1) { // app:url is set by process
  192. return res.redirect('/installer');
  193. }
  194. return next();
  195. };
  196. };
  197. exports.awsEnabled = function() {
  198. return function (req, res, next) {
  199. var config = req.config;
  200. if (config.crowi['aws:region'] !== '' && config.crowi['aws:bucket'] !== '' && config.crowi['aws:accessKeyId'] !== '' && config.crowi['aws:secretAccessKey'] !== '') {
  201. req.flash('globalError', 'AWS settings required to use this function. Please ask the administrator.');
  202. return res.redirect('/');
  203. }
  204. return next();
  205. };
  206. };