termsservlet.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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 __future__ import absolute_import
  16. from twisted.web.resource import Resource
  17. import logging
  18. from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError
  19. from sydent.terms.terms import get_terms
  20. from sydent.http.auth import authIfV2
  21. from sydent.db.terms import TermsStore
  22. from sydent.db.accounts import AccountStore
  23. logger = logging.getLogger(__name__)
  24. class TermsServlet(Resource):
  25. isLeaf = True
  26. def __init__(self, syd):
  27. self.sydent = syd
  28. @jsonwrap
  29. def render_GET(self, request):
  30. """
  31. Get the terms that must be agreed to in order to use this service
  32. Returns: Object describing the terms that require agreement
  33. """
  34. send_cors(request)
  35. terms = get_terms(self.sydent)
  36. return terms.getForClient()
  37. @jsonwrap
  38. def render_POST(self, request):
  39. """
  40. Mark a set of terms and conditions as having been agreed to
  41. """
  42. send_cors(request)
  43. account = authIfV2(self.sydent, request, False)
  44. args = get_args(request, ("user_accepts",))
  45. user_accepts = args["user_accepts"]
  46. terms = get_terms(self.sydent)
  47. unknown_urls = list(set(user_accepts) - terms.getUrlSet())
  48. if len(unknown_urls) > 0:
  49. raise MatrixRestError(
  50. 400, "M_UNKNOWN", "Unrecognised URLs: %s" % (', '.join(unknown_urls),))
  51. termsStore = TermsStore(self.sydent)
  52. termsStore.addAgreedUrls(account.userId, user_accepts)
  53. all_accepted_urls = termsStore.getAgreedUrls(account.userId)
  54. if terms.urlListIsSufficient(all_accepted_urls):
  55. accountStore = AccountStore(self.sydent)
  56. accountStore.setConsentVersion(account.userId, terms.getMasterVersion())
  57. return {}
  58. def render_OPTIONS(self, request):
  59. send_cors(request)
  60. return b''