consent.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from os import path
  15. from typing import Optional
  16. from synapse.config import ConfigError
  17. from ._base import Config
  18. DEFAULT_CONFIG = """\
  19. # User Consent configuration
  20. #
  21. # for detailed instructions, see
  22. # https://matrix-org.github.io/synapse/latest/consent_tracking.html
  23. #
  24. # Parts of this section are required if enabling the 'consent' resource under
  25. # 'listeners', in particular 'template_dir' and 'version'.
  26. #
  27. # 'template_dir' gives the location of the templates for the HTML forms.
  28. # This directory should contain one subdirectory per language (eg, 'en', 'fr'),
  29. # and each language directory should contain the policy document (named as
  30. # '<version>.html') and a success page (success.html).
  31. #
  32. # 'version' specifies the 'current' version of the policy document. It defines
  33. # the version to be served by the consent resource if there is no 'v'
  34. # parameter.
  35. #
  36. # 'server_notice_content', if enabled, will send a user a "Server Notice"
  37. # asking them to consent to the privacy policy. The 'server_notices' section
  38. # must also be configured for this to work. Notices will *not* be sent to
  39. # guest users unless 'send_server_notice_to_guests' is set to true.
  40. #
  41. # 'block_events_error', if set, will block any attempts to send events
  42. # until the user consents to the privacy policy. The value of the setting is
  43. # used as the text of the error.
  44. #
  45. # 'require_at_registration', if enabled, will add a step to the registration
  46. # process, similar to how captcha works. Users will be required to accept the
  47. # policy before their account is created.
  48. #
  49. # 'policy_name' is the display name of the policy users will see when registering
  50. # for an account. Has no effect unless `require_at_registration` is enabled.
  51. # Defaults to "Privacy Policy".
  52. #
  53. #user_consent:
  54. # template_dir: res/templates/privacy
  55. # version: 1.0
  56. # server_notice_content:
  57. # msgtype: m.text
  58. # body: >-
  59. # To continue using this homeserver you must review and agree to the
  60. # terms and conditions at %(consent_uri)s
  61. # send_server_notice_to_guests: true
  62. # block_events_error: >-
  63. # To continue using this homeserver you must review and agree to the
  64. # terms and conditions at %(consent_uri)s
  65. # require_at_registration: false
  66. # policy_name: Privacy Policy
  67. #
  68. """
  69. class ConsentConfig(Config):
  70. section = "consent"
  71. def __init__(self, *args):
  72. super().__init__(*args)
  73. self.user_consent_version: Optional[str] = None
  74. self.user_consent_template_dir: Optional[str] = None
  75. self.user_consent_server_notice_content = None
  76. self.user_consent_server_notice_to_guests = False
  77. self.block_events_without_consent_error = None
  78. self.user_consent_at_registration = False
  79. self.user_consent_policy_name = "Privacy Policy"
  80. def read_config(self, config, **kwargs):
  81. consent_config = config.get("user_consent")
  82. self.terms_template = self.read_template("terms.html")
  83. if consent_config is None:
  84. return
  85. self.user_consent_version = str(consent_config["version"])
  86. self.user_consent_template_dir = self.abspath(consent_config["template_dir"])
  87. if not isinstance(self.user_consent_template_dir, str) or not path.isdir(
  88. self.user_consent_template_dir
  89. ):
  90. raise ConfigError(
  91. "Could not find template directory '%s'"
  92. % (self.user_consent_template_dir,)
  93. )
  94. self.user_consent_server_notice_content = consent_config.get(
  95. "server_notice_content"
  96. )
  97. self.block_events_without_consent_error = consent_config.get(
  98. "block_events_error"
  99. )
  100. self.user_consent_server_notice_to_guests = bool(
  101. consent_config.get("send_server_notice_to_guests", False)
  102. )
  103. self.user_consent_at_registration = bool(
  104. consent_config.get("require_at_registration", False)
  105. )
  106. self.user_consent_policy_name = consent_config.get(
  107. "policy_name", "Privacy Policy"
  108. )
  109. def generate_config_section(self, **kwargs):
  110. return DEFAULT_CONFIG