acl.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const logger = require('@alias/logger')('growi:service:AclService'); // eslint-disable-line no-unused-vars
  2. /**
  3. * the service class of AclService
  4. */
  5. class AclService {
  6. constructor(configManager) {
  7. this.configManager = configManager;
  8. this.labels = {
  9. SECURITY_RESTRICT_GUEST_MODE_DENY: 'Deny',
  10. SECURITY_RESTRICT_GUEST_MODE_READONLY: 'Readonly',
  11. SECURITY_REGISTRATION_MODE_OPEN: 'Open',
  12. SECURITY_REGISTRATION_MODE_RESTRICTED: 'Restricted',
  13. SECURITY_REGISTRATION_MODE_CLOSED: 'Closed',
  14. };
  15. }
  16. /**
  17. * @returns Whether Access Control is enabled or not
  18. */
  19. isAclEnabled() {
  20. const wikiMode = this.configManager.getConfig('crowi', 'security:wikiMode');
  21. return wikiMode !== 'public';
  22. }
  23. /**
  24. * @returns Whether wiki mode is set
  25. */
  26. isWikiModeForced() {
  27. const wikiMode = this.configManager.getConfig('crowi', 'security:wikiMode');
  28. const isPrivateOrPublic = wikiMode === 'private' || wikiMode === 'public';
  29. return isPrivateOrPublic;
  30. }
  31. /**
  32. * @returns Whether guest users are allowed to read public pages
  33. */
  34. isGuestAllowedToRead() {
  35. const wikiMode = this.configManager.getConfig('crowi', 'security:wikiMode');
  36. // return false if private wiki mode
  37. if (wikiMode === 'private') {
  38. return false;
  39. }
  40. // return true if public wiki mode
  41. if (wikiMode === 'public') {
  42. return true;
  43. }
  44. const guestMode = this.configManager.getConfig('crowi', 'security:restrictGuestMode');
  45. // 'Readonly' => returns true (allow access to guests)
  46. // 'Deny', null, undefined, '', ... everything else => returns false (requires login)
  47. return guestMode === this.labels.SECURITY_RESTRICT_GUEST_MODE_READONLY;
  48. }
  49. getGuestModeValue() {
  50. return this.isGuestAllowedToRead()
  51. ? this.labels.SECURITY_RESTRICT_GUEST_MODE_READONLY
  52. : this.labels.SECURITY_RESTRICT_GUEST_MODE_DENY;
  53. }
  54. getRestrictGuestModeLabels() {
  55. const labels = {};
  56. labels[this.labels.SECURITY_RESTRICT_GUEST_MODE_DENY] = 'security_setting.guest_mode.deny';
  57. labels[this.labels.SECURITY_RESTRICT_GUEST_MODE_READONLY] = 'security_setting.guest_mode.readonly';
  58. return labels;
  59. }
  60. getRegistrationModeLabels() {
  61. const labels = {};
  62. labels[this.labels.SECURITY_REGISTRATION_MODE_OPEN] = 'security_setting.registration_mode.open';
  63. labels[this.labels.SECURITY_REGISTRATION_MODE_RESTRICTED] = 'security_setting.registration_mode.restricted';
  64. labels[this.labels.SECURITY_REGISTRATION_MODE_CLOSED] = 'security_setting.registration_mode.closed';
  65. return labels;
  66. }
  67. }
  68. module.exports = AclService;