swigFunctions.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. module.exports = function(crowi, app, req, locals) {
  2. const debug = require('debug')('growi:lib:swigFunctions');
  3. const stringWidth = require('string-width');
  4. const { pathUtils } = require('growi-commons');
  5. const Page = crowi.model('Page');
  6. const User = crowi.model('User');
  7. const {
  8. configManager,
  9. cdnResourcesService,
  10. passportService,
  11. appService,
  12. aclService,
  13. fileUploadService,
  14. customizeService,
  15. } = crowi;
  16. debug('initializing swigFunctions');
  17. locals.nodeVersion = function() {
  18. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  19. };
  20. locals.npmVersion = function() {
  21. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  22. };
  23. locals.yarnVersion = function() {
  24. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  25. };
  26. locals.growiVersion = function() {
  27. return crowi.version;
  28. };
  29. // token getter
  30. locals.csrf = function() {
  31. return req.csrfToken;
  32. };
  33. locals.getAppTitleFontSize = function(appTitle) {
  34. const appTitleWidth = stringWidth(appTitle);
  35. let fontSize = 22;
  36. if (appTitleWidth < 13) { /* do nothing */ }
  37. else if (appTitleWidth < 21) {
  38. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  39. }
  40. else {
  41. fontSize = 11;
  42. }
  43. return fontSize;
  44. };
  45. /**
  46. * @see ConfigManager#getConfig
  47. */
  48. locals.getConfig = configManager.getConfig.bind(configManager);
  49. /**
  50. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  51. */
  52. locals.getConfigFromDB = configManager.getConfigFromDB.bind(configManager);
  53. /**
  54. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  55. */
  56. locals.getConfigFromEnvVars = configManager.getConfigFromEnvVars.bind(configManager);
  57. /**
  58. * pass service/utils instances to swig
  59. */
  60. locals.appService = appService;
  61. locals.aclService = aclService;
  62. locals.fileUploadService = fileUploadService;
  63. locals.customizeService = customizeService;
  64. locals.pathUtils = pathUtils;
  65. locals.noCdn = function() {
  66. return cdnResourcesService.noCdn();
  67. };
  68. locals.cdnScriptTag = function(name) {
  69. return cdnResourcesService.getScriptTagByName(name);
  70. };
  71. locals.cdnScriptTagsByGroup = function(group) {
  72. const tags = cdnResourcesService.getScriptTagsByGroup(group);
  73. return tags.join('\n');
  74. };
  75. locals.cdnStyleTag = function(name) {
  76. return cdnResourcesService.getStyleTagByName(name);
  77. };
  78. locals.cdnStyleTagsByGroup = function(group) {
  79. const tags = cdnResourcesService.getStyleTagsByGroup(group);
  80. return tags.join('\n');
  81. };
  82. locals.cdnHighlightJsStyleTag = function(styleName) {
  83. return cdnResourcesService.getHighlightJsStyleTag(styleName);
  84. };
  85. /**
  86. * return true if enabled and strategy has been setup successfully
  87. */
  88. locals.isLdapSetup = function() {
  89. return (
  90. configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  91. && passportService.isLdapStrategySetup
  92. );
  93. };
  94. /**
  95. * return true if enabled but strategy has some problem
  96. */
  97. locals.isLdapSetupFailed = function() {
  98. return (
  99. configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  100. && !passportService.isLdapStrategySetup
  101. );
  102. };
  103. locals.getSamlMissingMandatoryConfigKeys = function() {
  104. return crowi.passportService.getSamlMissingMandatoryConfigKeys();
  105. };
  106. locals.searchConfigured = function() {
  107. if (crowi.getSearcher()) {
  108. return true;
  109. }
  110. return false;
  111. };
  112. locals.isHackmdSetup = function() {
  113. return process.env.HACKMD_URI != null;
  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. const path = req.path || '';
  132. if (path === '/') {
  133. return true;
  134. }
  135. return false;
  136. };
  137. locals.isTrashPage = function() {
  138. const path = req.path || '';
  139. if (path.match(/^\/trash\/.*/)) {
  140. return true;
  141. }
  142. return false;
  143. };
  144. locals.isDeletablePage = function() {
  145. const Page = crowi.model('Page');
  146. const 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(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(user) {
  176. switch (user.status) {
  177. case User.STATUS_REGISTERED:
  178. return 'label-info';
  179. case User.STATUS_ACTIVE:
  180. return 'label-success';
  181. case User.STATUS_SUSPENDED:
  182. return 'label-warning';
  183. case User.STATUS_DELETED:
  184. return 'label-danger';
  185. case User.STATUS_INVITED:
  186. return 'label-info';
  187. default:
  188. break;
  189. }
  190. return '';
  191. },
  192. };
  193. };