swigFunctions.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.isEnabledTimeline = function() {
  104. var config = crowi.getConfig()
  105. return Config.isEnabledTimeline(config);
  106. }
  107. locals.slackConfigured = function() {
  108. var config = crowi.getConfig()
  109. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  110. return true;
  111. }
  112. return false;
  113. };
  114. locals.isUploadable = function() {
  115. var config = crowi.getConfig()
  116. return Config.isUploadable(config);
  117. };
  118. locals.parentPath = function(path) {
  119. if (path == '/') {
  120. return path;
  121. }
  122. if (path.match(/.+\/$/)) {
  123. return path;
  124. }
  125. return path + '/';
  126. };
  127. locals.isUserPageList = function(path) {
  128. if (path.match(/^\/user\/[^\/]+\/$/)) {
  129. return true;
  130. }
  131. return false;
  132. };
  133. locals.isTopPage = function() {
  134. var path = req.path || '';
  135. if (path === '/') {
  136. return true;
  137. }
  138. return false;
  139. };
  140. locals.isTrashPage = function() {
  141. var path = req.path || '';
  142. if (path.match(/^\/trash\/.*/)) {
  143. return true;
  144. }
  145. return false;
  146. };
  147. locals.isDeletablePage = function() {
  148. var Page = crowi.model('Page');
  149. var path = req.path || '';
  150. return Page.isDeletableName(path);
  151. };
  152. locals.userPageRoot = function(user) {
  153. if (!user || !user.username) {
  154. return '';
  155. }
  156. return '/user/' + user.username;
  157. };
  158. locals.css = {
  159. grant: function (pageData) {
  160. if (!pageData) {
  161. return '';
  162. }
  163. switch (pageData.grant) {
  164. case Page.GRANT_PUBLIC:
  165. return 'grant-public';
  166. case Page.GRANT_RESTRICTED:
  167. return 'grant-restricted';
  168. //case Page.GRANT_SPECIFIED:
  169. // return 'grant-specified';
  170. // break;
  171. case Page.GRANT_OWNER:
  172. return 'grant-owner';
  173. default:
  174. break;
  175. }
  176. return '';
  177. },
  178. userStatus: function (user) {
  179. //debug('userStatus', user._id, user.usename, user.status);
  180. switch (user.status) {
  181. case User.STATUS_REGISTERED:
  182. return 'label-info';
  183. case User.STATUS_ACTIVE:
  184. return 'label-success';
  185. case User.STATUS_SUSPENDED:
  186. return 'label-warning';
  187. case User.STATUS_DELETED:
  188. return 'label-danger';
  189. case User.STATUS_INVITED:
  190. return 'label-info';
  191. default:
  192. break;
  193. }
  194. return '';
  195. },
  196. };
  197. };