swigFunctions.js 9.3 KB

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