swigFunctions.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 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() {
  112. var config = crowi.getConfig();
  113. var title = Config.customTitle(config);
  114. var app_title = Config.appTitle(config);
  115. var custom_title = title.replace('{{sitename}}', app_title);
  116. return custom_title;
  117. }
  118. locals.behaviorType = function() {
  119. var config = crowi.getConfig()
  120. return Config.behaviorType(config);
  121. }
  122. locals.layoutType = function() {
  123. var config = crowi.getConfig()
  124. return Config.layoutType(config);
  125. }
  126. locals.highlightJsStyle = function() {
  127. var config = crowi.getConfig()
  128. return Config.highlightJsStyle(config);
  129. }
  130. locals.highlightJsStyleBorder = function() {
  131. var config = crowi.getConfig()
  132. return Config.highlightJsStyleBorder(config);
  133. }
  134. locals.isEnabledTimeline = function() {
  135. var config = crowi.getConfig()
  136. return Config.isEnabledTimeline(config);
  137. }
  138. locals.slackConfigured = function() {
  139. var config = crowi.getConfig()
  140. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  141. return true;
  142. }
  143. return false;
  144. };
  145. locals.isUploadable = function() {
  146. var config = crowi.getConfig()
  147. return Config.isUploadable(config);
  148. };
  149. locals.parentPath = function(path) {
  150. if (path == '/') {
  151. return path;
  152. }
  153. if (path.match(/.+\/$/)) {
  154. return path;
  155. }
  156. return path + '/';
  157. };
  158. locals.isUserPageList = function(path) {
  159. if (path.match(/^\/user\/[^\/]+\/$/)) {
  160. return true;
  161. }
  162. return false;
  163. };
  164. locals.isTopPage = function() {
  165. var path = req.path || '';
  166. if (path === '/') {
  167. return true;
  168. }
  169. return false;
  170. };
  171. locals.isTrashPage = function() {
  172. var path = req.path || '';
  173. if (path.match(/^\/trash\/.*/)) {
  174. return true;
  175. }
  176. return false;
  177. };
  178. locals.isDeletablePage = function() {
  179. var Page = crowi.model('Page');
  180. var path = req.path || '';
  181. return Page.isDeletableName(path);
  182. };
  183. locals.userPageRoot = function(user) {
  184. if (!user || !user.username) {
  185. return '';
  186. }
  187. return '/user/' + user.username;
  188. };
  189. locals.css = {
  190. grant: function (pageData) {
  191. if (!pageData) {
  192. return '';
  193. }
  194. switch (pageData.grant) {
  195. case Page.GRANT_PUBLIC:
  196. return 'grant-public';
  197. case Page.GRANT_RESTRICTED:
  198. return 'grant-restricted';
  199. //case Page.GRANT_SPECIFIED:
  200. // return 'grant-specified';
  201. // break;
  202. case Page.GRANT_OWNER:
  203. return 'grant-owner';
  204. default:
  205. break;
  206. }
  207. return '';
  208. },
  209. userStatus: function (user) {
  210. //debug('userStatus', user._id, user.usename, user.status);
  211. switch (user.status) {
  212. case User.STATUS_REGISTERED:
  213. return 'label-info';
  214. case User.STATUS_ACTIVE:
  215. return 'label-success';
  216. case User.STATUS_SUSPENDED:
  217. return 'label-warning';
  218. case User.STATUS_DELETED:
  219. return 'label-danger';
  220. case User.STATUS_INVITED:
  221. return 'label-info';
  222. default:
  223. break;
  224. }
  225. return '';
  226. },
  227. };
  228. };