middlewares.js 6.7 KB

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