swigFunctions.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. module.exports = function(crowi, req, locals) {
  2. const debug = require('debug')('growi:lib:swigFunctions');
  3. const stringWidth = require('string-width');
  4. const Page = crowi.model('Page');
  5. const User = crowi.model('User');
  6. const {
  7. configManager,
  8. cdnResourcesService,
  9. passportService,
  10. appService,
  11. aclService,
  12. fileUploadService,
  13. customizeService,
  14. } = crowi;
  15. debug('initializing swigFunctions');
  16. locals.nodeVersion = function() {
  17. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  18. };
  19. locals.npmVersion = function() {
  20. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  21. };
  22. locals.yarnVersion = function() {
  23. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  24. };
  25. locals.growiVersion = function() {
  26. return crowi.version;
  27. };
  28. // token getter
  29. locals.csrf = function() {
  30. return req.csrfToken;
  31. };
  32. locals.getAppTitleFontSize = function(appTitle) {
  33. const appTitleWidth = stringWidth(appTitle);
  34. let fontSize = 22;
  35. if (appTitleWidth < 13) { /* do nothing */ }
  36. else if (appTitleWidth < 21) {
  37. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  38. }
  39. else {
  40. fontSize = 11;
  41. }
  42. return fontSize;
  43. };
  44. /**
  45. * @see ConfigManager#getConfig
  46. */
  47. locals.getConfig = configManager.getConfig.bind(configManager);
  48. /**
  49. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  50. */
  51. locals.getConfigFromDB = configManager.getConfigFromDB.bind(configManager);
  52. /**
  53. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  54. */
  55. locals.getConfigFromEnvVars = configManager.getConfigFromEnvVars.bind(configManager);
  56. /**
  57. * pass service class to swig
  58. */
  59. locals.appService = appService;
  60. locals.aclService = aclService;
  61. locals.fileUploadService = fileUploadService;
  62. locals.customizeService = customizeService;
  63. locals.noCdn = function() {
  64. return cdnResourcesService.noCdn();
  65. };
  66. locals.cdnScriptTag = function(name) {
  67. return cdnResourcesService.getScriptTagByName(name);
  68. };
  69. locals.cdnScriptTagsByGroup = function(group) {
  70. const tags = cdnResourcesService.getScriptTagsByGroup(group);
  71. return tags.join('\n');
  72. };
  73. locals.cdnStyleTag = function(name) {
  74. return cdnResourcesService.getStyleTagByName(name);
  75. };
  76. locals.cdnStyleTagsByGroup = function(group) {
  77. const tags = cdnResourcesService.getStyleTagsByGroup(group);
  78. return tags.join('\n');
  79. };
  80. locals.cdnHighlightJsStyleTag = function(styleName) {
  81. return cdnResourcesService.getHighlightJsStyleTag(styleName);
  82. };
  83. /**
  84. * return true if enabled and strategy has been setup successfully
  85. */
  86. locals.isLdapSetup = function() {
  87. return (
  88. configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  89. && passportService.isLdapStrategySetup
  90. );
  91. };
  92. /**
  93. * return true if enabled but strategy has some problem
  94. */
  95. locals.isLdapSetupFailed = function() {
  96. return (
  97. configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  98. && !passportService.isLdapStrategySetup
  99. );
  100. };
  101. locals.getSamlMissingMandatoryConfigKeys = function() {
  102. return crowi.passportService.getSamlMissingMandatoryConfigKeys();
  103. };
  104. locals.searchConfigured = function() {
  105. if (crowi.getSearcher()) {
  106. return true;
  107. }
  108. return false;
  109. };
  110. locals.isHackmdSetup = function() {
  111. return process.env.HACKMD_URI != null;
  112. };
  113. locals.parentPath = function(path) {
  114. if (path === '/') {
  115. return path;
  116. }
  117. if (path.match(/.+\/$/)) {
  118. return path;
  119. }
  120. return `${path}/`;
  121. };
  122. locals.isUserPageList = function(path) {
  123. if (path.match(/^\/user\/[^/]+\/$/)) {
  124. return true;
  125. }
  126. return false;
  127. };
  128. locals.isTopPage = function() {
  129. const path = req.path || '';
  130. if (path === '/') {
  131. return true;
  132. }
  133. return false;
  134. };
  135. locals.isTrashPage = function() {
  136. const path = req.path || '';
  137. if (path.match(/^\/trash\/.*/)) {
  138. return true;
  139. }
  140. return false;
  141. };
  142. locals.isDeletablePage = function() {
  143. const Page = crowi.model('Page');
  144. const path = req.path || '';
  145. return Page.isDeletableName(path);
  146. };
  147. locals.userPageRoot = function(user) {
  148. if (!user || !user.username) {
  149. return '';
  150. }
  151. return `/user/${user.username}`;
  152. };
  153. locals.css = {
  154. grant(pageData) {
  155. if (!pageData) {
  156. return '';
  157. }
  158. switch (pageData.grant) {
  159. case Page.GRANT_PUBLIC:
  160. return 'grant-public';
  161. case Page.GRANT_RESTRICTED:
  162. return 'grant-restricted';
  163. // case Page.GRANT_SPECIFIED:
  164. // return 'grant-specified';
  165. // break;
  166. case Page.GRANT_OWNER:
  167. return 'grant-owner';
  168. default:
  169. break;
  170. }
  171. return '';
  172. },
  173. userStatus(user) {
  174. switch (user.status) {
  175. case User.STATUS_REGISTERED:
  176. return 'label-info';
  177. case User.STATUS_ACTIVE:
  178. return 'label-success';
  179. case User.STATUS_SUSPENDED:
  180. return 'label-warning';
  181. case User.STATUS_DELETED:
  182. return 'label-danger';
  183. case User.STATUS_INVITED:
  184. return 'label-info';
  185. default:
  186. break;
  187. }
  188. return '';
  189. },
  190. };
  191. };