swigFunctions.js 5.8 KB

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