swigFunctions.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.customHeader = function() {
  81. var config = crowi.getConfig()
  82. return Config.customHeader(config);
  83. }
  84. locals.behaviorType = function() {
  85. var config = crowi.getConfig()
  86. return Config.behaviorType(config);
  87. }
  88. locals.layoutType = function() {
  89. var config = crowi.getConfig()
  90. return Config.layoutType(config);
  91. }
  92. locals.highlightJsStyle = function() {
  93. var config = crowi.getConfig()
  94. return Config.highlightJsStyle(config);
  95. }
  96. locals.highlightJsStyleBorder = function() {
  97. var config = crowi.getConfig()
  98. return Config.highlightJsStyleBorder(config);
  99. }
  100. locals.isEnabledTimeline = function() {
  101. var config = crowi.getConfig()
  102. return Config.isEnabledTimeline(config);
  103. }
  104. locals.slackConfigured = function() {
  105. var config = crowi.getConfig()
  106. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  107. return true;
  108. }
  109. return false;
  110. };
  111. locals.isUploadable = function() {
  112. var config = crowi.getConfig()
  113. return Config.isUploadable(config);
  114. };
  115. locals.parentPath = function(path) {
  116. if (path == '/') {
  117. return path;
  118. }
  119. if (path.match(/.+\/$/)) {
  120. return path;
  121. }
  122. return path + '/';
  123. };
  124. locals.isUserPageList = function(path) {
  125. if (path.match(/^\/user\/[^\/]+\/$/)) {
  126. return true;
  127. }
  128. return false;
  129. };
  130. locals.isTopPage = function() {
  131. var path = req.path || '';
  132. if (path === '/') {
  133. return true;
  134. }
  135. return false;
  136. };
  137. locals.isTrashPage = function() {
  138. var path = req.path || '';
  139. if (path.match(/^\/trash\/.*/)) {
  140. return true;
  141. }
  142. return false;
  143. };
  144. locals.isDeletablePage = function() {
  145. var Page = crowi.model('Page');
  146. var path = req.path || '';
  147. return Page.isDeletableName(path);
  148. };
  149. locals.userPageRoot = function(user) {
  150. if (!user || !user.username) {
  151. return '';
  152. }
  153. return '/user/' + user.username;
  154. };
  155. locals.css = {
  156. grant: function (pageData) {
  157. if (!pageData) {
  158. return '';
  159. }
  160. switch (pageData.grant) {
  161. case Page.GRANT_PUBLIC:
  162. return 'grant-public';
  163. case Page.GRANT_RESTRICTED:
  164. return 'grant-restricted';
  165. //case Page.GRANT_SPECIFIED:
  166. // return 'grant-specified';
  167. // break;
  168. case Page.GRANT_OWNER:
  169. return 'grant-owner';
  170. default:
  171. break;
  172. }
  173. return '';
  174. },
  175. userStatus: function (user) {
  176. //debug('userStatus', user._id, user.usename, user.status);
  177. switch (user.status) {
  178. case User.STATUS_REGISTERED:
  179. return 'label-info';
  180. case User.STATUS_ACTIVE:
  181. return 'label-success';
  182. case User.STATUS_SUSPENDED:
  183. return 'label-warning';
  184. case User.STATUS_DELETED:
  185. return 'label-danger';
  186. case User.STATUS_INVITED:
  187. return 'label-info';
  188. default:
  189. break;
  190. }
  191. return '';
  192. },
  193. };
  194. };