swigFunctions.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('growi:lib:swigFunctions')
  3. , Page = crowi.model('Page')
  4. , Config = crowi.model('Config')
  5. , User = crowi.model('User')
  6. , passportService = crowi.passportService
  7. ;
  8. locals.nodeVersion = function() {
  9. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  10. }
  11. locals.npmVersion = function() {
  12. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  13. }
  14. locals.yarnVersion = function() {
  15. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  16. }
  17. locals.growiVersion = function() {
  18. return crowi.version;
  19. }
  20. // token getter
  21. locals.csrf = function() {
  22. return req.csrfToken;
  23. };
  24. locals.getAppTitleFontSize = function(appTitle) {
  25. let fontSize = 22;
  26. if (appTitle.length < 13) { /* do nothing */ }
  27. else if (appTitle.length < 21) {
  28. fontSize -= 3 * (Math.floor((appTitle.length - 13) / 3) + 1);
  29. }
  30. else {
  31. fontSize = 11;
  32. }
  33. return fontSize;
  34. }
  35. /**
  36. * return app title
  37. */
  38. locals.appTitle = function() {
  39. var config = crowi.getConfig();
  40. return Config.appTitle(config);
  41. }
  42. /**
  43. * return true if enabled
  44. */
  45. locals.isEnabledPassport = function() {
  46. var config = crowi.getConfig();
  47. return Config.isEnabledPassport(config);
  48. }
  49. /**
  50. * return true if local strategy has been setup successfully
  51. * used whether restarting the server needed
  52. */
  53. locals.isPassportLocalStrategySetup = function() {
  54. return passportService != null && passportService.isLocalStrategySetup;
  55. }
  56. /**
  57. * return true if enabled and strategy has been setup successfully
  58. */
  59. locals.isLdapSetup = function() {
  60. var config = crowi.getConfig()
  61. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
  62. }
  63. /**
  64. * return true if enabled but strategy has some problem
  65. */
  66. locals.isLdapSetupFailed = function() {
  67. var config = crowi.getConfig()
  68. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
  69. }
  70. locals.googleLoginEnabled = function() {
  71. // return false if Passport is enabled
  72. // because official crowi mechanism is not used.
  73. if (locals.isEnabledPassport()) {
  74. return false;
  75. }
  76. var config = crowi.getConfig()
  77. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  78. };
  79. locals.searchConfigured = function() {
  80. if (crowi.getSearcher()) {
  81. return true;
  82. }
  83. return false;
  84. };
  85. locals.isEnabledPlugins = function() {
  86. var config = crowi.getConfig()
  87. return Config.isEnabledPlugins(config);
  88. }
  89. locals.isEnabledLinebreaks = function() {
  90. var config = crowi.getConfig()
  91. return Config.isEnabledLinebreaks(config);
  92. }
  93. locals.isEnabledLinebreaksInComments = function() {
  94. var config = crowi.getConfig()
  95. return Config.isEnabledLinebreaksInComments(config);
  96. }
  97. locals.customCss = function() {
  98. return Config.customCss();
  99. }
  100. locals.customScript = function() {
  101. return Config.customScript();
  102. }
  103. locals.customHeader = function() {
  104. var config = crowi.getConfig()
  105. return Config.customHeader(config);
  106. }
  107. locals.theme = function() {
  108. var config = crowi.getConfig()
  109. return Config.theme(config);
  110. }
  111. locals.customTitle = function(page) {
  112. const config = crowi.getConfig();
  113. return Config.customTitle(config, page);
  114. }
  115. locals.behaviorType = function() {
  116. var config = crowi.getConfig();
  117. return Config.behaviorType(config);
  118. }
  119. locals.layoutType = function() {
  120. var config = crowi.getConfig()
  121. return Config.layoutType(config);
  122. }
  123. locals.highlightJsStyle = function() {
  124. var config = crowi.getConfig()
  125. return Config.highlightJsStyle(config);
  126. }
  127. locals.highlightJsStyleBorder = function() {
  128. var config = crowi.getConfig()
  129. return Config.highlightJsStyleBorder(config);
  130. }
  131. locals.isEnabledTimeline = function() {
  132. var config = crowi.getConfig()
  133. return Config.isEnabledTimeline(config);
  134. }
  135. locals.slackConfigured = function() {
  136. var config = crowi.getConfig()
  137. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  138. return true;
  139. }
  140. return false;
  141. };
  142. locals.isUploadable = function() {
  143. var config = crowi.getConfig()
  144. return Config.isUploadable(config);
  145. };
  146. locals.isEnabledAttachTitleHeader = function() {
  147. var config = crowi.getConfig()
  148. return Config.isEnabledAttachTitleHeader(config);
  149. };
  150. locals.parentPath = function(path) {
  151. if (path == '/') {
  152. return path;
  153. }
  154. if (path.match(/.+\/$/)) {
  155. return path;
  156. }
  157. return path + '/';
  158. };
  159. locals.isUserPageList = function(path) {
  160. if (path.match(/^\/user\/[^\/]+\/$/)) {
  161. return true;
  162. }
  163. return false;
  164. };
  165. locals.isTopPage = function() {
  166. var path = req.path || '';
  167. if (path === '/') {
  168. return true;
  169. }
  170. return false;
  171. };
  172. locals.isTrashPage = function() {
  173. var path = req.path || '';
  174. if (path.match(/^\/trash\/.*/)) {
  175. return true;
  176. }
  177. return false;
  178. };
  179. locals.isDeletablePage = function() {
  180. var Page = crowi.model('Page');
  181. var path = req.path || '';
  182. return Page.isDeletableName(path);
  183. };
  184. locals.userPageRoot = function(user) {
  185. if (!user || !user.username) {
  186. return '';
  187. }
  188. return '/user/' + user.username;
  189. };
  190. locals.css = {
  191. grant: function (pageData) {
  192. if (!pageData) {
  193. return '';
  194. }
  195. switch (pageData.grant) {
  196. case Page.GRANT_PUBLIC:
  197. return 'grant-public';
  198. case Page.GRANT_RESTRICTED:
  199. return 'grant-restricted';
  200. //case Page.GRANT_SPECIFIED:
  201. // return 'grant-specified';
  202. // break;
  203. case Page.GRANT_OWNER:
  204. return 'grant-owner';
  205. default:
  206. break;
  207. }
  208. return '';
  209. },
  210. userStatus: function (user) {
  211. //debug('userStatus', user._id, user.usename, user.status);
  212. switch (user.status) {
  213. case User.STATUS_REGISTERED:
  214. return 'label-info';
  215. case User.STATUS_ACTIVE:
  216. return 'label-success';
  217. case User.STATUS_SUSPENDED:
  218. return 'label-warning';
  219. case User.STATUS_DELETED:
  220. return 'label-danger';
  221. case User.STATUS_INVITED:
  222. return 'label-info';
  223. default:
  224. break;
  225. }
  226. return '';
  227. },
  228. };
  229. };