swigFunctions.js 8.1 KB

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