swigFunctions.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('crowi:lib:swigFunctions')
  3. , Page = crowi.model('Page')
  4. , Config = crowi.model('Config')
  5. , User = crowi.model('User')
  6. ;
  7. // token getter
  8. locals._csrf = function() {
  9. return req.csrfToken;
  10. };
  11. locals.facebookLoginEnabled = function() {
  12. var config = crowi.getConfig()
  13. return config.crowi['facebook:appId'] && config.crowi['facebook:secret'];
  14. };
  15. locals.googleLoginEnabled = function() {
  16. var config = crowi.getConfig()
  17. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  18. };
  19. locals.searchConfigured = function() {
  20. if (crowi.getSearcher()) {
  21. return true;
  22. }
  23. return false;
  24. };
  25. locals.slackConfigured = function() {
  26. var config = crowi.getConfig()
  27. if (Config.hasSlackToken(config)) {
  28. return true;
  29. }
  30. return false;
  31. };
  32. locals.isUploadable = function() {
  33. var config = crowi.getConfig()
  34. return Config.isUploadable(config);
  35. };
  36. locals.isUserPageList = function(path) {
  37. if (path.match(/^\/user\/[^\/]+\/$/)) {
  38. return true;
  39. }
  40. return false;
  41. };
  42. locals.isTopPage = function() {
  43. var path = req.path || '';
  44. if (path === '/') {
  45. return true;
  46. }
  47. return false;
  48. };
  49. locals.isTrashPage = function() {
  50. var path = req.path || '';
  51. if (path.match(/^\/trash\/.*/)) {
  52. return true;
  53. }
  54. return false;
  55. };
  56. locals.isDeletablePage = function() {
  57. var Page = crowi.model('Page');
  58. var path = req.path || '';
  59. return Page.isDeletableName(path);
  60. };
  61. locals.userPageRoot = function(user) {
  62. if (!user || !user.username) {
  63. return '';
  64. }
  65. return '/user/' + user.username;
  66. };
  67. locals.css = {
  68. grant: function (pageData) {
  69. if (!pageData) {
  70. return '';
  71. }
  72. switch (pageData.grant) {
  73. case Page.GRANT_PUBLIC:
  74. return 'grant-public';
  75. case Page.GRANT_RESTRICTED:
  76. return 'grant-restricted';
  77. //case Page.GRANT_SPECIFIED:
  78. // return 'grant-specified';
  79. // break;
  80. case Page.GRANT_OWNER:
  81. return 'grant-owner';
  82. default:
  83. break;
  84. }
  85. return '';
  86. },
  87. userStatus: function (user) {
  88. //debug('userStatus', user._id, user.usename, user.status);
  89. switch (user.status) {
  90. case User.STATUS_REGISTERED:
  91. return 'label-info';
  92. case User.STATUS_ACTIVE:
  93. return 'label-success';
  94. case User.STATUS_SUSPENDED:
  95. return 'label-warning';
  96. case User.STATUS_DELETED:
  97. return 'label-danger';
  98. case User.STATUS_INVITED:
  99. return 'label-info';
  100. default:
  101. break;
  102. }
  103. return '';
  104. },
  105. };
  106. };