swigFunctions.js 6.9 KB

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