acl.js 2.6 KB

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