swigFunctions.js 6.5 KB

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