swigFunctions.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.isEnabledPlugins = function() {
  97. let config = crowi.getConfig();
  98. return Config.isEnabledPlugins(config);
  99. };
  100. locals.isEnabledLinebreaks = function() {
  101. let config = crowi.getConfig();
  102. return Config.isEnabledLinebreaks(config);
  103. };
  104. locals.isEnabledLinebreaksInComments = function() {
  105. let config = crowi.getConfig();
  106. return Config.isEnabledLinebreaksInComments(config);
  107. };
  108. locals.customCss = function() {
  109. return Config.customCss();
  110. };
  111. locals.customScript = function() {
  112. return Config.customScript();
  113. };
  114. locals.customHeader = function() {
  115. let config = crowi.getConfig();
  116. return Config.customHeader(config);
  117. };
  118. locals.theme = function() {
  119. let config = crowi.getConfig();
  120. return Config.theme(config);
  121. };
  122. locals.customTitle = function(page) {
  123. const config = crowi.getConfig();
  124. return Config.customTitle(config, page);
  125. };
  126. locals.behaviorType = function() {
  127. let config = crowi.getConfig();
  128. return Config.behaviorType(config);
  129. };
  130. locals.layoutType = function() {
  131. let config = crowi.getConfig();
  132. return Config.layoutType(config);
  133. };
  134. locals.highlightJsStyle = function() {
  135. let config = crowi.getConfig();
  136. return Config.highlightJsStyle(config);
  137. };
  138. locals.highlightJsStyleBorder = function() {
  139. let config = crowi.getConfig();
  140. return Config.highlightJsStyleBorder(config);
  141. };
  142. locals.isEnabledTimeline = function() {
  143. let config = crowi.getConfig();
  144. return Config.isEnabledTimeline(config);
  145. };
  146. locals.isUploadable = function() {
  147. let config = crowi.getConfig();
  148. return Config.isUploadable(config);
  149. };
  150. locals.isEnabledAttachTitleHeader = function() {
  151. let config = crowi.getConfig();
  152. return Config.isEnabledAttachTitleHeader(config);
  153. };
  154. locals.parentPath = function(path) {
  155. if (path == '/') {
  156. return path;
  157. }
  158. if (path.match(/.+\/$/)) {
  159. return path;
  160. }
  161. return path + '/';
  162. };
  163. locals.isUserPageList = function(path) {
  164. if (path.match(/^\/user\/[^/]+\/$/)) {
  165. return true;
  166. }
  167. return false;
  168. };
  169. locals.isTopPage = function() {
  170. let path = req.path || '';
  171. if (path === '/') {
  172. return true;
  173. }
  174. return false;
  175. };
  176. locals.isTrashPage = function() {
  177. let path = req.path || '';
  178. if (path.match(/^\/trash\/.*/)) {
  179. return true;
  180. }
  181. return false;
  182. };
  183. locals.isDeletablePage = function() {
  184. let Page = crowi.model('Page');
  185. let path = req.path || '';
  186. return Page.isDeletableName(path);
  187. };
  188. locals.userPageRoot = function(user) {
  189. if (!user || !user.username) {
  190. return '';
  191. }
  192. return '/user/' + user.username;
  193. };
  194. locals.css = {
  195. grant: function(pageData) {
  196. if (!pageData) {
  197. return '';
  198. }
  199. switch (pageData.grant) {
  200. case Page.GRANT_PUBLIC:
  201. return 'grant-public';
  202. case Page.GRANT_RESTRICTED:
  203. return 'grant-restricted';
  204. //case Page.GRANT_SPECIFIED:
  205. // return 'grant-specified';
  206. // break;
  207. case Page.GRANT_OWNER:
  208. return 'grant-owner';
  209. default:
  210. break;
  211. }
  212. return '';
  213. },
  214. userStatus: function(user) {
  215. //debug('userStatus', user._id, user.usename, user.status);
  216. switch (user.status) {
  217. case User.STATUS_REGISTERED:
  218. return 'label-info';
  219. case User.STATUS_ACTIVE:
  220. return 'label-success';
  221. case User.STATUS_SUSPENDED:
  222. return 'label-warning';
  223. case User.STATUS_DELETED:
  224. return 'label-danger';
  225. case User.STATUS_INVITED:
  226. return 'label-info';
  227. default:
  228. break;
  229. }
  230. return '';
  231. },
  232. };
  233. };