swigFunctions.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. ;
  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. let 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. /**
  71. * return true if enabled
  72. */
  73. locals.isEnabledPassport = function() {
  74. const config = crowi.getConfig();
  75. return Config.isEnabledPassport(config);
  76. };
  77. /**
  78. * return true if local strategy has been setup successfully
  79. * used whether restarting the server needed
  80. */
  81. locals.isPassportLocalStrategySetup = function() {
  82. return passportService != null && passportService.isLocalStrategySetup;
  83. };
  84. /**
  85. * return true if enabled and strategy has been setup successfully
  86. */
  87. locals.isLdapSetup = function() {
  88. let config = crowi.getConfig();
  89. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
  90. };
  91. /**
  92. * return true if enabled but strategy has some problem
  93. */
  94. locals.isLdapSetupFailed = function() {
  95. let config = crowi.getConfig();
  96. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
  97. };
  98. locals.passportSamlLoginEnabled = function() {
  99. let config = crowi.getConfig();
  100. return locals.isEnabledPassport() && config.crowi['security:passport-saml:isEnabled'];
  101. };
  102. locals.getSamlMissingMandatoryConfigKeys = function() {
  103. return crowi.passportService.getSamlMissingMandatoryConfigKeys();
  104. };
  105. locals.googleLoginEnabled = function() {
  106. // return false if Passport is enabled
  107. // because official crowi mechanism is not used.
  108. if (locals.isEnabledPassport()) {
  109. return false;
  110. }
  111. let config = crowi.getConfig();
  112. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  113. };
  114. locals.passportGoogleLoginEnabled = function() {
  115. let config = crowi.getConfig();
  116. return locals.isEnabledPassport() && config.crowi['security:passport-google:isEnabled'];
  117. };
  118. locals.passportGitHubLoginEnabled = function() {
  119. let config = crowi.getConfig();
  120. return locals.isEnabledPassport() && config.crowi['security:passport-github:isEnabled'];
  121. };
  122. locals.passportTwitterLoginEnabled = function() {
  123. let config = crowi.getConfig();
  124. return locals.isEnabledPassport() && config.crowi['security:passport-twitter:isEnabled'];
  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.isEnabledPlugins = function() {
  136. let config = crowi.getConfig();
  137. return Config.isEnabledPlugins(config);
  138. };
  139. locals.isEnabledLinebreaks = function() {
  140. let config = crowi.getConfig();
  141. return Config.isEnabledLinebreaks(config);
  142. };
  143. locals.isEnabledLinebreaksInComments = function() {
  144. let config = crowi.getConfig();
  145. return Config.isEnabledLinebreaksInComments(config);
  146. };
  147. locals.customCss = function() {
  148. return Config.customCss();
  149. };
  150. locals.pageBreakSeparator = function() {
  151. let config = crowi.getConfig();
  152. return Config.pageBreakSeparator(config);
  153. };
  154. locals.pageBreakCustomSeparator = function() {
  155. let config = crowi.getConfig();
  156. return Config.pageBreakCustomSeparator(config);
  157. };
  158. locals.customScript = function() {
  159. return Config.customScript();
  160. };
  161. locals.customHeader = function() {
  162. let config = crowi.getConfig();
  163. return Config.customHeader(config);
  164. };
  165. locals.theme = function() {
  166. let config = crowi.getConfig();
  167. return Config.theme(config);
  168. };
  169. locals.customTitle = function(page) {
  170. const config = crowi.getConfig();
  171. return Config.customTitle(config, page);
  172. };
  173. locals.behaviorType = function() {
  174. let config = crowi.getConfig();
  175. return Config.behaviorType(config);
  176. };
  177. locals.layoutType = function() {
  178. let config = crowi.getConfig();
  179. return Config.layoutType(config);
  180. };
  181. locals.highlightJsStyle = function() {
  182. let config = crowi.getConfig();
  183. return Config.highlightJsStyle(config);
  184. };
  185. locals.highlightJsStyleBorder = function() {
  186. let config = crowi.getConfig();
  187. return Config.highlightJsStyleBorder(config);
  188. };
  189. locals.isEnabledTimeline = function() {
  190. let config = crowi.getConfig();
  191. return Config.isEnabledTimeline(config);
  192. };
  193. locals.isUploadable = function() {
  194. let config = crowi.getConfig();
  195. return Config.isUploadable(config);
  196. };
  197. locals.isEnabledAttachTitleHeader = function() {
  198. let config = crowi.getConfig();
  199. return Config.isEnabledAttachTitleHeader(config);
  200. };
  201. locals.parentPath = function(path) {
  202. if (path == '/') {
  203. return path;
  204. }
  205. if (path.match(/.+\/$/)) {
  206. return path;
  207. }
  208. return path + '/';
  209. };
  210. locals.isUserPageList = function(path) {
  211. if (path.match(/^\/user\/[^/]+\/$/)) {
  212. return true;
  213. }
  214. return false;
  215. };
  216. locals.isTopPage = function() {
  217. let path = req.path || '';
  218. if (path === '/') {
  219. return true;
  220. }
  221. return false;
  222. };
  223. locals.isTrashPage = function() {
  224. let path = req.path || '';
  225. if (path.match(/^\/trash\/.*/)) {
  226. return true;
  227. }
  228. return false;
  229. };
  230. locals.isDeletablePage = function() {
  231. let Page = crowi.model('Page');
  232. let path = req.path || '';
  233. return Page.isDeletableName(path);
  234. };
  235. locals.userPageRoot = function(user) {
  236. if (!user || !user.username) {
  237. return '';
  238. }
  239. return '/user/' + user.username;
  240. };
  241. locals.css = {
  242. grant: function(pageData) {
  243. if (!pageData) {
  244. return '';
  245. }
  246. switch (pageData.grant) {
  247. case Page.GRANT_PUBLIC:
  248. return 'grant-public';
  249. case Page.GRANT_RESTRICTED:
  250. return 'grant-restricted';
  251. //case Page.GRANT_SPECIFIED:
  252. // return 'grant-specified';
  253. // break;
  254. case Page.GRANT_OWNER:
  255. return 'grant-owner';
  256. default:
  257. break;
  258. }
  259. return '';
  260. },
  261. userStatus: function(user) {
  262. //debug('userStatus', user._id, user.usename, user.status);
  263. switch (user.status) {
  264. case User.STATUS_REGISTERED:
  265. return 'label-info';
  266. case User.STATUS_ACTIVE:
  267. return 'label-success';
  268. case User.STATUS_SUSPENDED:
  269. return 'label-warning';
  270. case User.STATUS_DELETED:
  271. return 'label-danger';
  272. case User.STATUS_INVITED:
  273. return 'label-info';
  274. default:
  275. break;
  276. }
  277. return '';
  278. },
  279. };
  280. };