swigFunctions.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('growi:lib:swigFunctions')
  3. , stringWidth = require('string-width')
  4. , Page = crowi.model('Page')
  5. , Config = crowi.model('Config')
  6. , User = crowi.model('User')
  7. , passportService = crowi.passportService
  8. ;
  9. debug('initializing swigFunctions');
  10. locals.nodeVersion = function() {
  11. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  12. };
  13. locals.npmVersion = function() {
  14. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  15. };
  16. locals.yarnVersion = function() {
  17. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  18. };
  19. locals.growiVersion = function() {
  20. return crowi.version;
  21. };
  22. // token getter
  23. locals.csrf = function() {
  24. return req.csrfToken;
  25. };
  26. locals.getAppTitleFontSize = function(appTitle) {
  27. let appTitleWidth = stringWidth(appTitle);
  28. let fontSize = 22;
  29. if (appTitleWidth < 13) { /* do nothing */ }
  30. else if (appTitleWidth < 21) {
  31. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  32. }
  33. else {
  34. fontSize = 11;
  35. }
  36. return fontSize;
  37. };
  38. /**
  39. * return app title
  40. */
  41. locals.appTitle = function() {
  42. var config = crowi.getConfig();
  43. return Config.appTitle(config);
  44. };
  45. /**
  46. * return true if enabled
  47. */
  48. locals.isEnabledPassport = function() {
  49. var config = crowi.getConfig();
  50. return Config.isEnabledPassport(config);
  51. };
  52. /**
  53. * return true if local strategy has been setup successfully
  54. * used whether restarting the server needed
  55. */
  56. locals.isPassportLocalStrategySetup = function() {
  57. return passportService != null && passportService.isLocalStrategySetup;
  58. };
  59. /**
  60. * return true if enabled and strategy has been setup successfully
  61. */
  62. locals.isLdapSetup = function() {
  63. var config = crowi.getConfig();
  64. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
  65. };
  66. /**
  67. * return true if enabled but strategy has some problem
  68. */
  69. locals.isLdapSetupFailed = function() {
  70. var config = crowi.getConfig();
  71. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
  72. };
  73. locals.googleLoginEnabled = function() {
  74. // return false if Passport is enabled
  75. // because official crowi mechanism is not used.
  76. if (locals.isEnabledPassport()) {
  77. return false;
  78. }
  79. var config = crowi.getConfig();
  80. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  81. };
  82. locals.passportGoogleLoginEnabled = function() {
  83. var config = crowi.getConfig();
  84. return locals.isEnabledPassport() && config.crowi['security:passport-google:isEnabled'];
  85. };
  86. locals.passportGitHubLoginEnabled = function() {
  87. var config = crowi.getConfig();
  88. return locals.isEnabledPassport() && config.crowi['security:passport-github:isEnabled'];
  89. };
  90. locals.searchConfigured = function() {
  91. if (crowi.getSearcher()) {
  92. return true;
  93. }
  94. return false;
  95. };
  96. locals.isEnabledPlugins = function() {
  97. var config = crowi.getConfig();
  98. return Config.isEnabledPlugins(config);
  99. };
  100. locals.isEnabledLinebreaks = function() {
  101. var config = crowi.getConfig();
  102. return Config.isEnabledLinebreaks(config);
  103. };
  104. locals.isEnabledLinebreaksInComments = function() {
  105. var config = crowi.getConfig();
  106. return Config.isEnabledLinebreaksInComments(config);
  107. };
  108. locals.customCss = function() {
  109. return Config.customCss();
  110. };
  111. locals.customScript = function() {
  112. return Config.customScript();
  113. };
  114. locals.customHeader = function() {
  115. var config = crowi.getConfig();
  116. return Config.customHeader(config);
  117. };
  118. locals.theme = function() {
  119. var config = crowi.getConfig();
  120. return Config.theme(config);
  121. };
  122. locals.customTitle = function(page) {
  123. const config = crowi.getConfig();
  124. return Config.customTitle(config, page);
  125. };
  126. locals.behaviorType = function() {
  127. var config = crowi.getConfig();
  128. return Config.behaviorType(config);
  129. };
  130. locals.layoutType = function() {
  131. var config = crowi.getConfig();
  132. return Config.layoutType(config);
  133. };
  134. locals.highlightJsStyle = function() {
  135. var config = crowi.getConfig();
  136. return Config.highlightJsStyle(config);
  137. };
  138. locals.highlightJsStyleBorder = function() {
  139. var config = crowi.getConfig();
  140. return Config.highlightJsStyleBorder(config);
  141. };
  142. locals.isEnabledTimeline = function() {
  143. var config = crowi.getConfig();
  144. return Config.isEnabledTimeline(config);
  145. };
  146. locals.slackConfigured = function() {
  147. var config = crowi.getConfig();
  148. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  149. return true;
  150. }
  151. return false;
  152. };
  153. locals.isUploadable = function() {
  154. var config = crowi.getConfig();
  155. return Config.isUploadable(config);
  156. };
  157. locals.isEnabledAttachTitleHeader = function() {
  158. var config = crowi.getConfig();
  159. return Config.isEnabledAttachTitleHeader(config);
  160. };
  161. locals.parentPath = function(path) {
  162. if (path == '/') {
  163. return path;
  164. }
  165. if (path.match(/.+\/$/)) {
  166. return path;
  167. }
  168. return path + '/';
  169. };
  170. locals.isUserPageList = function(path) {
  171. if (path.match(/^\/user\/[^/]+\/$/)) {
  172. return true;
  173. }
  174. return false;
  175. };
  176. locals.isTopPage = function() {
  177. var path = req.path || '';
  178. if (path === '/') {
  179. return true;
  180. }
  181. return false;
  182. };
  183. locals.isTrashPage = function() {
  184. var path = req.path || '';
  185. if (path.match(/^\/trash\/.*/)) {
  186. return true;
  187. }
  188. return false;
  189. };
  190. locals.isDeletablePage = function() {
  191. var Page = crowi.model('Page');
  192. var path = req.path || '';
  193. return Page.isDeletableName(path);
  194. };
  195. locals.userPageRoot = function(user) {
  196. if (!user || !user.username) {
  197. return '';
  198. }
  199. return '/user/' + user.username;
  200. };
  201. locals.css = {
  202. grant: function(pageData) {
  203. if (!pageData) {
  204. return '';
  205. }
  206. switch (pageData.grant) {
  207. case Page.GRANT_PUBLIC:
  208. return 'grant-public';
  209. case Page.GRANT_RESTRICTED:
  210. return 'grant-restricted';
  211. //case Page.GRANT_SPECIFIED:
  212. // return 'grant-specified';
  213. // break;
  214. case Page.GRANT_OWNER:
  215. return 'grant-owner';
  216. default:
  217. break;
  218. }
  219. return '';
  220. },
  221. userStatus: function(user) {
  222. //debug('userStatus', user._id, user.usename, user.status);
  223. switch (user.status) {
  224. case User.STATUS_REGISTERED:
  225. return 'label-info';
  226. case User.STATUS_ACTIVE:
  227. return 'label-success';
  228. case User.STATUS_SUSPENDED:
  229. return 'label-warning';
  230. case User.STATUS_DELETED:
  231. return 'label-danger';
  232. case User.STATUS_INVITED:
  233. return 'label-info';
  234. default:
  235. break;
  236. }
  237. return '';
  238. },
  239. };
  240. };