swigFunctions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. , passportService = crowi.passportService
  7. ;
  8. locals.nodeVersion = function() {
  9. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  10. }
  11. locals.npmVersion = function() {
  12. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  13. }
  14. locals.yarnVersion = function() {
  15. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  16. }
  17. locals.crowiVersion = function() {
  18. return crowi.version;
  19. }
  20. // token getter
  21. locals.csrf = function() {
  22. return req.csrfToken;
  23. };
  24. /**
  25. * return true if enabled
  26. */
  27. locals.isEnabledPassport = function() {
  28. var config = crowi.getConfig()
  29. return Config.isEnabledPassport(config);
  30. }
  31. /**
  32. * return true if local strategy has been setup successfully
  33. * used whether restarting the server needed
  34. */
  35. locals.isPassportLocalStrategySetup = function() {
  36. return passportService != null && passportService.isLocalStrategySetup;
  37. }
  38. /**
  39. * return true if enabled and strategy has been setup successfully
  40. */
  41. locals.isLdapSetup = function() {
  42. var config = crowi.getConfig()
  43. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
  44. }
  45. /**
  46. * return true if enabled but strategy has some problem
  47. */
  48. locals.isLdapSetupFailed = function() {
  49. var config = crowi.getConfig()
  50. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
  51. }
  52. locals.googleLoginEnabled = function() {
  53. var config = crowi.getConfig()
  54. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  55. };
  56. locals.searchConfigured = function() {
  57. if (crowi.getSearcher()) {
  58. return true;
  59. }
  60. return false;
  61. };
  62. locals.isEnabledPlugins = function() {
  63. var config = crowi.getConfig()
  64. return Config.isEnabledPlugins(config);
  65. }
  66. locals.isEnabledLinebreaks = function() {
  67. var config = crowi.getConfig()
  68. return Config.isEnabledLinebreaks(config);
  69. }
  70. locals.isEnabledLinebreaksInComments = function() {
  71. var config = crowi.getConfig()
  72. return Config.isEnabledLinebreaksInComments(config);
  73. }
  74. locals.customCss = function() {
  75. return Config.customCss();
  76. }
  77. locals.customScript = function() {
  78. return Config.customScript();
  79. }
  80. locals.behaviorType = function() {
  81. var config = crowi.getConfig()
  82. return Config.behaviorType(config);
  83. }
  84. locals.layoutType = function() {
  85. var config = crowi.getConfig()
  86. return Config.layoutType(config);
  87. }
  88. locals.isEnabledTimeline = function() {
  89. var config = crowi.getConfig()
  90. return Config.isEnabledTimeline(config);
  91. }
  92. locals.slackConfigured = function() {
  93. var config = crowi.getConfig()
  94. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  95. return true;
  96. }
  97. return false;
  98. };
  99. locals.isUploadable = function() {
  100. var config = crowi.getConfig()
  101. return Config.isUploadable(config);
  102. };
  103. locals.parentPath = function(path) {
  104. if (path == '/') {
  105. return path;
  106. }
  107. if (path.match(/.+\/$/)) {
  108. return path;
  109. }
  110. return path + '/';
  111. };
  112. locals.isUserPageList = function(path) {
  113. if (path.match(/^\/user\/[^\/]+\/$/)) {
  114. return true;
  115. }
  116. return false;
  117. };
  118. locals.isTopPage = function() {
  119. var path = req.path || '';
  120. if (path === '/') {
  121. return true;
  122. }
  123. return false;
  124. };
  125. locals.isTrashPage = function() {
  126. var path = req.path || '';
  127. if (path.match(/^\/trash\/.*/)) {
  128. return true;
  129. }
  130. return false;
  131. };
  132. locals.isDeletablePage = function() {
  133. var Page = crowi.model('Page');
  134. var path = req.path || '';
  135. return Page.isDeletableName(path);
  136. };
  137. locals.userPageRoot = function(user) {
  138. if (!user || !user.username) {
  139. return '';
  140. }
  141. return '/user/' + user.username;
  142. };
  143. locals.css = {
  144. grant: function (pageData) {
  145. if (!pageData) {
  146. return '';
  147. }
  148. switch (pageData.grant) {
  149. case Page.GRANT_PUBLIC:
  150. return 'grant-public';
  151. case Page.GRANT_RESTRICTED:
  152. return 'grant-restricted';
  153. //case Page.GRANT_SPECIFIED:
  154. // return 'grant-specified';
  155. // break;
  156. case Page.GRANT_OWNER:
  157. return 'grant-owner';
  158. default:
  159. break;
  160. }
  161. return '';
  162. },
  163. userStatus: function (user) {
  164. //debug('userStatus', user._id, user.usename, user.status);
  165. switch (user.status) {
  166. case User.STATUS_REGISTERED:
  167. return 'label-info';
  168. case User.STATUS_ACTIVE:
  169. return 'label-success';
  170. case User.STATUS_SUSPENDED:
  171. return 'label-warning';
  172. case User.STATUS_DELETED:
  173. return 'label-danger';
  174. case User.STATUS_INVITED:
  175. return 'label-info';
  176. default:
  177. break;
  178. }
  179. return '';
  180. },
  181. };
  182. };