swigFunctions.js 7.1 KB

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