acl.ts 2.2 KB

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