swigFunctions.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // return false if Passport is enabled
  65. // because official crowi mechanism is not used.
  66. if (locals.isEnabledPassport()) {
  67. return false;
  68. }
  69. var config = crowi.getConfig()
  70. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  71. };
  72. locals.searchConfigured = function() {
  73. if (crowi.getSearcher()) {
  74. return true;
  75. }
  76. return false;
  77. };
  78. locals.isEnabledPlugins = function() {
  79. var config = crowi.getConfig()
  80. return Config.isEnabledPlugins(config);
  81. }
  82. locals.isEnabledLinebreaks = function() {
  83. var config = crowi.getConfig()
  84. return Config.isEnabledLinebreaks(config);
  85. }
  86. locals.isEnabledLinebreaksInComments = function() {
  87. var config = crowi.getConfig()
  88. return Config.isEnabledLinebreaksInComments(config);
  89. }
  90. locals.customCss = function() {
  91. return Config.customCss();
  92. }
  93. locals.customScript = function() {
  94. return Config.customScript();
  95. }
  96. locals.customHeader = function() {
  97. var config = crowi.getConfig()
  98. return Config.customHeader(config);
  99. }
  100. locals.theme = function() {
  101. var config = crowi.getConfig()
  102. return Config.theme(config);
  103. }
  104. locals.customTitle = function(path) {
  105. var config = crowi.getConfig();
  106. var title = Config.customTitle(config);
  107. var app_title = config.crowi['app:title'] ? config.crowi['app:title'] : 'Crowi';
  108. var custom_title = title.replace('{{sitename}}', app_title).replace('{{path}}', path);
  109. return custom_title;
  110. }
  111. locals.behaviorType = function() {
  112. var config = crowi.getConfig()
  113. return Config.behaviorType(config);
  114. }
  115. locals.layoutType = function() {
  116. var config = crowi.getConfig()
  117. return Config.layoutType(config);
  118. }
  119. locals.highlightJsStyle = function() {
  120. var config = crowi.getConfig()
  121. return Config.highlightJsStyle(config);
  122. }
  123. locals.highlightJsStyleBorder = function() {
  124. var config = crowi.getConfig()
  125. return Config.highlightJsStyleBorder(config);
  126. }
  127. locals.isEnabledTimeline = function() {
  128. var config = crowi.getConfig()
  129. return Config.isEnabledTimeline(config);
  130. }
  131. locals.slackConfigured = function() {
  132. var config = crowi.getConfig()
  133. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  134. return true;
  135. }
  136. return false;
  137. };
  138. locals.isUploadable = function() {
  139. var config = crowi.getConfig()
  140. return Config.isUploadable(config);
  141. };
  142. locals.parentPath = function(path) {
  143. if (path == '/') {
  144. return path;
  145. }
  146. if (path.match(/.+\/$/)) {
  147. return path;
  148. }
  149. return path + '/';
  150. };
  151. locals.isUserPageList = function(path) {
  152. if (path.match(/^\/user\/[^\/]+\/$/)) {
  153. return true;
  154. }
  155. return false;
  156. };
  157. locals.isTopPage = function() {
  158. var path = req.path || '';
  159. if (path === '/') {
  160. return true;
  161. }
  162. return false;
  163. };
  164. locals.isTrashPage = function() {
  165. var path = req.path || '';
  166. if (path.match(/^\/trash\/.*/)) {
  167. return true;
  168. }
  169. return false;
  170. };
  171. locals.isDeletablePage = function() {
  172. var Page = crowi.model('Page');
  173. var path = req.path || '';
  174. return Page.isDeletableName(path);
  175. };
  176. locals.userPageRoot = function(user) {
  177. if (!user || !user.username) {
  178. return '';
  179. }
  180. return '/user/' + user.username;
  181. };
  182. locals.css = {
  183. grant: function (pageData) {
  184. if (!pageData) {
  185. return '';
  186. }
  187. switch (pageData.grant) {
  188. case Page.GRANT_PUBLIC:
  189. return 'grant-public';
  190. case Page.GRANT_RESTRICTED:
  191. return 'grant-restricted';
  192. //case Page.GRANT_SPECIFIED:
  193. // return 'grant-specified';
  194. // break;
  195. case Page.GRANT_OWNER:
  196. return 'grant-owner';
  197. default:
  198. break;
  199. }
  200. return '';
  201. },
  202. userStatus: function (user) {
  203. //debug('userStatus', user._id, user.usename, user.status);
  204. switch (user.status) {
  205. case User.STATUS_REGISTERED:
  206. return 'label-info';
  207. case User.STATUS_ACTIVE:
  208. return 'label-success';
  209. case User.STATUS_SUSPENDED:
  210. return 'label-warning';
  211. case User.STATUS_DELETED:
  212. return 'label-danger';
  213. case User.STATUS_INVITED:
  214. return 'label-info';
  215. default:
  216. break;
  217. }
  218. return '';
  219. },
  220. };
  221. };