swigFunctions.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. module.exports = function(crowi, app, req, locals) {
  2. const debug = require('debug')('growi:lib:swigFunctions');
  3. const stringWidth = require('string-width');
  4. const Page = crowi.model('Page');
  5. const Config = crowi.model('Config');
  6. const User = crowi.model('User');
  7. const {
  8. configManager,
  9. cdnResourcesService,
  10. passportService,
  11. appService,
  12. fileUploadService,
  13. } = crowi;
  14. debug('initializing swigFunctions');
  15. locals.nodeVersion = function() {
  16. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  17. };
  18. locals.npmVersion = function() {
  19. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  20. };
  21. locals.yarnVersion = function() {
  22. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  23. };
  24. locals.growiVersion = function() {
  25. return crowi.version;
  26. };
  27. // token getter
  28. locals.csrf = function() {
  29. return req.csrfToken;
  30. };
  31. locals.getAppTitleFontSize = function(appTitle) {
  32. const appTitleWidth = stringWidth(appTitle);
  33. let fontSize = 22;
  34. if (appTitleWidth < 13) { /* do nothing */ }
  35. else if (appTitleWidth < 21) {
  36. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  37. }
  38. else {
  39. fontSize = 11;
  40. }
  41. return fontSize;
  42. };
  43. /**
  44. * @see ConfigManager#getConfig
  45. */
  46. locals.getConfig = configManager.getConfig.bind(configManager);
  47. /**
  48. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  49. */
  50. locals.getConfigFromDB = configManager.getConfigFromDB.bind(configManager);
  51. /**
  52. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  53. */
  54. locals.getConfigFromEnvVars = configManager.getConfigFromEnvVars.bind(configManager);
  55. /**
  56. * pass service class to swig
  57. */
  58. locals.appService = appService;
  59. locals.fileUploadService = fileUploadService;
  60. locals.noCdn = function() {
  61. return !!process.env.NO_CDN;
  62. };
  63. locals.cdnScriptTag = function(name) {
  64. return cdnResourcesService.getScriptTagByName(name);
  65. };
  66. locals.cdnScriptTagsByGroup = function(group) {
  67. const tags = cdnResourcesService.getScriptTagsByGroup(group);
  68. return tags.join('\n');
  69. };
  70. locals.cdnStyleTag = function(name) {
  71. return cdnResourcesService.getStyleTagByName(name);
  72. };
  73. locals.cdnStyleTagsByGroup = function(group) {
  74. const tags = cdnResourcesService.getStyleTagsByGroup(group);
  75. return tags.join('\n');
  76. };
  77. locals.cdnHighlightJsStyleTag = function(styleName) {
  78. return cdnResourcesService.getHighlightJsStyleTag(styleName);
  79. };
  80. /**
  81. * return true if local strategy has been setup successfully
  82. * used whether restarting the server needed
  83. */
  84. locals.isPassportLocalStrategySetup = function() {
  85. return passportService != null && passportService.isLocalStrategySetup;
  86. };
  87. /**
  88. * return true if enabled and strategy has been setup successfully
  89. */
  90. locals.isLdapSetup = function() {
  91. return (
  92. configManager.getConfig('crowi', 'security:isEnabledPassport')
  93. && configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  94. && passportService.isLdapStrategySetup
  95. );
  96. };
  97. /**
  98. * return true if enabled but strategy has some problem
  99. */
  100. locals.isLdapSetupFailed = function() {
  101. return (
  102. configManager.getConfig('crowi', 'security:isEnabledPassport')
  103. && configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  104. && !passportService.isLdapStrategySetup
  105. );
  106. };
  107. locals.getSamlMissingMandatoryConfigKeys = function() {
  108. // return an empty array if Passport is not enabled
  109. // because crowi.passportService is null.
  110. if (!configManager.getConfig('crowi', 'security:isEnabledPassport')) {
  111. return [];
  112. }
  113. return crowi.passportService.getSamlMissingMandatoryConfigKeys();
  114. };
  115. locals.googleLoginEnabled = function() {
  116. // return false if Passport is enabled
  117. // because official crowi mechanism is not used.
  118. if (configManager.getConfig('crowi', 'security:isEnabledPassport')) {
  119. return false;
  120. }
  121. return (
  122. configManager.getConfig('crowi', 'google:clientId')
  123. && configManager.getConfig('crowi', 'google:clientSecret')
  124. );
  125. };
  126. locals.searchConfigured = function() {
  127. if (crowi.getSearcher()) {
  128. return true;
  129. }
  130. return false;
  131. };
  132. locals.isHackmdSetup = function() {
  133. return process.env.HACKMD_URI != null;
  134. };
  135. locals.isEnabledLinebreaks = function() {
  136. const config = crowi.getConfig();
  137. return Config.isEnabledLinebreaks(config);
  138. };
  139. locals.isEnabledLinebreaksInComments = function() {
  140. const config = crowi.getConfig();
  141. return Config.isEnabledLinebreaksInComments(config);
  142. };
  143. locals.pageBreakSeparator = function() {
  144. const config = crowi.getConfig();
  145. return Config.pageBreakSeparator(config);
  146. };
  147. locals.pageBreakCustomSeparator = function() {
  148. const config = crowi.getConfig();
  149. return Config.pageBreakCustomSeparator(config);
  150. };
  151. locals.customCss = function() {
  152. const customizeService = crowi.customizeService;
  153. return customizeService.getCustomCss();
  154. };
  155. locals.customScript = function() {
  156. const customizeService = crowi.customizeService;
  157. return customizeService.getCustomScript();
  158. };
  159. locals.customHeader = function() {
  160. return configManager.getConfig('crowi', 'customize:header') || '';
  161. };
  162. locals.customTitle = function(page) {
  163. const customizeService = crowi.customizeService;
  164. return customizeService.generateCustomTitle(page);
  165. };
  166. locals.behaviorType = function() {
  167. return configManager.getConfig('crowi', 'customize:behavior');
  168. };
  169. locals.layoutType = function() {
  170. return configManager.getConfig('crowi', 'customize:layout');
  171. };
  172. locals.highlightJsStyle = function() {
  173. return configManager.getConfig('crowi', 'customize:highlightJsStyle');
  174. };
  175. locals.highlightJsStyleBorder = function() {
  176. return configManager.getConfig('crowi', 'customize:highlightJsStyleBorder');
  177. };
  178. locals.isEnabledTimeline = function() {
  179. return configManager.getConfig('crowi', 'customize:isEnabledTimeline');
  180. };
  181. locals.parentPath = function(path) {
  182. if (path === '/') {
  183. return path;
  184. }
  185. if (path.match(/.+\/$/)) {
  186. return path;
  187. }
  188. return `${path}/`;
  189. };
  190. locals.isUserPageList = function(path) {
  191. if (path.match(/^\/user\/[^/]+\/$/)) {
  192. return true;
  193. }
  194. return false;
  195. };
  196. locals.isTopPage = function() {
  197. const path = req.path || '';
  198. if (path === '/') {
  199. return true;
  200. }
  201. return false;
  202. };
  203. locals.isTrashPage = function() {
  204. const path = req.path || '';
  205. if (path.match(/^\/trash\/.*/)) {
  206. return true;
  207. }
  208. return false;
  209. };
  210. locals.isDeletablePage = function() {
  211. const Page = crowi.model('Page');
  212. const path = req.path || '';
  213. return Page.isDeletableName(path);
  214. };
  215. locals.userPageRoot = function(user) {
  216. if (!user || !user.username) {
  217. return '';
  218. }
  219. return `/user/${user.username}`;
  220. };
  221. locals.css = {
  222. grant(pageData) {
  223. if (!pageData) {
  224. return '';
  225. }
  226. switch (pageData.grant) {
  227. case Page.GRANT_PUBLIC:
  228. return 'grant-public';
  229. case Page.GRANT_RESTRICTED:
  230. return 'grant-restricted';
  231. // case Page.GRANT_SPECIFIED:
  232. // return 'grant-specified';
  233. // break;
  234. case Page.GRANT_OWNER:
  235. return 'grant-owner';
  236. default:
  237. break;
  238. }
  239. return '';
  240. },
  241. userStatus(user) {
  242. switch (user.status) {
  243. case User.STATUS_REGISTERED:
  244. return 'label-info';
  245. case User.STATUS_ACTIVE:
  246. return 'label-success';
  247. case User.STATUS_SUSPENDED:
  248. return 'label-warning';
  249. case User.STATUS_DELETED:
  250. return 'label-danger';
  251. case User.STATUS_INVITED:
  252. return 'label-info';
  253. default:
  254. break;
  255. }
  256. return '';
  257. },
  258. };
  259. };