middlewares.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if (user.image && user.image != '/images/userpicture.png') {
  113. return user.image;
  114. } else {
  115. return '/images/userpicture.png';
  116. }
  117. });
  118. next();
  119. };
  120. };
  121. exports.adminRequired = function() {
  122. return function(req, res, next) {
  123. if (req.user && '_id' in req.user) {
  124. if (req.user.admin) {
  125. next();
  126. return;
  127. }
  128. return res.redirect('/');
  129. }
  130. return res.redirect('/login');
  131. };
  132. };
  133. exports.loginRequired = function(crowi, app) {
  134. return function(req, res, next) {
  135. var User = crowi.model('User')
  136. if (req.user && '_id' in req.user) {
  137. if (req.user.status === User.STATUS_ACTIVE) {
  138. // Active の人だけ先に進める
  139. return next();
  140. } else if (req.user.status === User.STATUS_REGISTERED) {
  141. return res.redirect('/login/error/registered');
  142. } else if (req.user.status === User.STATUS_SUSPENDED) {
  143. return res.redirect('/login/error/suspended');
  144. } else if (req.user.status === User.STATUS_INVITED) {
  145. return res.redirect('/login/invited');
  146. }
  147. }
  148. // is api path
  149. var path = req.path || '';
  150. if (path.match(/^\/_api\/.+$/)) {
  151. return res.sendStatus(403);
  152. }
  153. req.session.jumpTo = req.originalUrl;
  154. return res.redirect('/login');
  155. };
  156. };
  157. exports.accessTokenParser = function(crowi, app) {
  158. return function(req, res, next) {
  159. var accessToken = req.query.access_token || req.body.access_token || null;
  160. if (!accessToken) {
  161. return next();
  162. }
  163. var User = crowi.model('User')
  164. User.findUserByApiToken(accessToken)
  165. .then(function(userData) {
  166. req.user = userData;
  167. req.skipCsrfVerify = true;
  168. next();
  169. }).catch(function(err) {
  170. next();
  171. });
  172. };
  173. };
  174. // this is for Installer
  175. exports.applicationNotInstalled = function() {
  176. return function(req, res, next) {
  177. var config = req.config;
  178. if (Object.keys(config.crowi).length !== 1) {
  179. req.flash('errorMessage', 'Application already installed.');
  180. return res.redirect('admin'); // admin以外はadminRequiredで'/'にリダイレクトされる
  181. }
  182. return next();
  183. };
  184. };
  185. exports.applicationInstalled = function() {
  186. return function(req, res, next) {
  187. var config = req.config;
  188. if (Object.keys(config.crowi).length === 1) { // app:url is set by process
  189. return res.redirect('/installer');
  190. }
  191. return next();
  192. };
  193. };
  194. exports.awsEnabled = function() {
  195. return function (req, res, next) {
  196. var config = req.config;
  197. if (config.crowi['aws:region'] !== '' && config.crowi['aws:bucket'] !== '' && config.crowi['aws:accessKeyId'] !== '' && config.crowi['aws:secretAccessKey'] !== '') {
  198. req.flash('globalError', 'AWS settings required to use this function. Please ask the administrator.');
  199. return res.redirect('/');
  200. }
  201. return next();
  202. };
  203. };