validators.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from typing import Type
  18. from flask_babel import lazy_gettext as _
  19. from marshmallow import ValidationError
  20. from sqlalchemy.engine.url import make_url
  21. from sqlalchemy.exc import ArgumentError
  22. from superset import security_manager
  23. def sqlalchemy_uri_validator(
  24. uri: str, exception: Type[ValidationError] = ValidationError
  25. ) -> None:
  26. """
  27. Check if a user has submitted a valid SQLAlchemy URI
  28. """
  29. try:
  30. make_url(uri.strip())
  31. except (ArgumentError, AttributeError):
  32. raise exception(
  33. _(
  34. "Invalid connnection string, a valid string follows: "
  35. " 'DRIVER://USER:PASSWORD@DB-HOST/DATABASE-NAME'"
  36. " <p>Example:'postgresql://user:password@your-postgres-db/database'</p>"
  37. )
  38. )
  39. def schema_allows_csv_upload(database, schema):
  40. if not database.allow_csv_upload:
  41. return False
  42. schemas = database.get_schema_access_for_csv_upload()
  43. if schemas:
  44. return schema in schemas
  45. return (
  46. security_manager.database_access(database)
  47. or security_manager.all_datasource_access()
  48. )