swigFunctions.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('crowi: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.parentPath = function(path) {
  147. if (path == '/') {
  148. return path;
  149. }
  150. if (path.match(/.+\/$/)) {
  151. return path;
  152. }
  153. return path + '/';
  154. };
  155. locals.isUserPageList = function(path) {
  156. if (path.match(/^\/user\/[^\/]+\/$/)) {
  157. return true;
  158. }
  159. return false;
  160. };
  161. locals.isTopPage = function() {
  162. var path = req.path || '';
  163. if (path === '/') {
  164. return true;
  165. }
  166. return false;
  167. };
  168. locals.isTrashPage = function() {
  169. var path = req.path || '';
  170. if (path.match(/^\/trash\/.*/)) {
  171. return true;
  172. }
  173. return false;
  174. };
  175. locals.isDeletablePage = function() {
  176. var Page = crowi.model('Page');
  177. var path = req.path || '';
  178. return Page.isDeletableName(path);
  179. };
  180. locals.userPageRoot = function(user) {
  181. if (!user || !user.username) {
  182. return '';
  183. }
  184. return '/user/' + user.username;
  185. };
  186. locals.css = {
  187. grant: function (pageData) {
  188. if (!pageData) {
  189. return '';
  190. }
  191. switch (pageData.grant) {
  192. case Page.GRANT_PUBLIC:
  193. return 'grant-public';
  194. case Page.GRANT_RESTRICTED:
  195. return 'grant-restricted';
  196. //case Page.GRANT_SPECIFIED:
  197. // return 'grant-specified';
  198. // break;
  199. case Page.GRANT_OWNER:
  200. return 'grant-owner';
  201. default:
  202. break;
  203. }
  204. return '';
  205. },
  206. userStatus: function (user) {
  207. //debug('userStatus', user._id, user.usename, user.status);
  208. switch (user.status) {
  209. case User.STATUS_REGISTERED:
  210. return 'label-info';
  211. case User.STATUS_ACTIVE:
  212. return 'label-success';
  213. case User.STATUS_SUSPENDED:
  214. return 'label-warning';
  215. case User.STATUS_DELETED:
  216. return 'label-danger';
  217. case User.STATUS_INVITED:
  218. return 'label-info';
  219. default:
  220. break;
  221. }
  222. return '';
  223. },
  224. };
  225. };