swigFunctions.js 9.3 KB

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