consent_config.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from os import path
  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://github.com/matrix-org/synapse/blob/master/docs/consent_tracking.md
  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 = None
  74. self.user_consent_template_dir = 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 path.isdir(self.user_consent_template_dir):
  88. raise ConfigError(
  89. "Could not find template directory '%s'"
  90. % (self.user_consent_template_dir,)
  91. )
  92. self.user_consent_server_notice_content = consent_config.get(
  93. "server_notice_content"
  94. )
  95. self.block_events_without_consent_error = consent_config.get(
  96. "block_events_error"
  97. )
  98. self.user_consent_server_notice_to_guests = bool(
  99. consent_config.get("send_server_notice_to_guests", False)
  100. )
  101. self.user_consent_at_registration = bool(
  102. consent_config.get("require_at_registration", False)
  103. )
  104. self.user_consent_policy_name = consent_config.get(
  105. "policy_name", "Privacy Policy"
  106. )
  107. def generate_config_section(self, **kwargs):
  108. return DEFAULT_CONFIG