__init__.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. try: # Python 3.5+
  3. from http import HTTPStatus
  4. except ImportError:
  5. from http import client as HTTPStatus
  6. from pyramid.config import Configurator
  7. from pyramid.authentication import BasicAuthAuthenticationPolicy
  8. from hapic.ext.pyramid import PyramidContext
  9. from pyramid.exceptions import NotFound
  10. from sqlalchemy.exc import OperationalError
  11. from tracim_backend.extensions import hapic
  12. from tracim_backend.config import CFG
  13. from tracim_backend.lib.utils.request import TracimRequest
  14. from tracim_backend.lib.utils.authentification import basic_auth_check_credentials
  15. from tracim_backend.lib.utils.authentification import BASIC_AUTH_WEBUI_REALM
  16. from tracim_backend.lib.utils.authorization import AcceptAllAuthorizationPolicy
  17. from tracim_backend.lib.utils.authorization import TRACIM_DEFAULT_PERM
  18. from tracim_backend.lib.utils.cors import add_cors_support
  19. from tracim_backend.lib.webdav import WebdavAppFactory
  20. from tracim_backend.views import BASE_API_V2
  21. from tracim_backend.views.contents_api.html_document_controller import HTMLDocumentController # nopep8
  22. from tracim_backend.views.contents_api.threads_controller import ThreadController
  23. from tracim_backend.views.core_api.session_controller import SessionController
  24. from tracim_backend.views.core_api.system_controller import SystemController
  25. from tracim_backend.views.core_api.user_controller import UserController
  26. from tracim_backend.views.core_api.workspace_controller import WorkspaceController
  27. from tracim_backend.views.contents_api.comment_controller import CommentController
  28. from tracim_backend.views.contents_api.file_controller import FileController
  29. from tracim_backend.views.errors import ErrorSchema
  30. from tracim_backend.exceptions import NotAuthenticated
  31. from tracim_backend.exceptions import UserNotActive
  32. from tracim_backend.exceptions import InvalidId
  33. from tracim_backend.exceptions import InsufficientUserProfile
  34. from tracim_backend.exceptions import InsufficientUserRoleInWorkspace
  35. from tracim_backend.exceptions import WorkspaceNotFoundInTracimRequest
  36. from tracim_backend.exceptions import UserNotFoundInTracimRequest
  37. from tracim_backend.exceptions import ContentNotFoundInTracimRequest
  38. from tracim_backend.exceptions import WorkspaceNotFound
  39. from tracim_backend.exceptions import ContentNotFound
  40. from tracim_backend.exceptions import UserDoesNotExist
  41. from tracim_backend.exceptions import AuthenticationFailed
  42. from tracim_backend.exceptions import ContentTypeNotAllowed
  43. def web(global_config, **local_settings):
  44. """ This function returns a Pyramid WSGI application.
  45. """
  46. settings = global_config
  47. settings.update(local_settings)
  48. # set CFG object
  49. app_config = CFG(settings)
  50. app_config.configure_filedepot()
  51. settings['CFG'] = app_config
  52. configurator = Configurator(settings=settings, autocommit=True)
  53. # Add BasicAuthPolicy
  54. authn_policy = BasicAuthAuthenticationPolicy(
  55. basic_auth_check_credentials,
  56. realm=BASIC_AUTH_WEBUI_REALM,
  57. )
  58. configurator.include(add_cors_support)
  59. # make sure to add this before other routes to intercept OPTIONS
  60. configurator.add_cors_preflight_handler()
  61. # Default authorization : Accept anything.
  62. configurator.set_authorization_policy(AcceptAllAuthorizationPolicy())
  63. configurator.set_authentication_policy(authn_policy)
  64. # INFO - GM - 11-04-2018 - set default perm
  65. # setting default perm is needed to force authentification
  66. # mecanism in all views.
  67. configurator.set_default_permission(TRACIM_DEFAULT_PERM)
  68. # Override default request
  69. configurator.set_request_factory(TracimRequest)
  70. # Pyramids "plugin" include.
  71. configurator.include('pyramid_jinja2')
  72. # Add SqlAlchemy DB
  73. configurator.include('.models')
  74. # set Hapic
  75. context = PyramidContext(
  76. configurator=configurator,
  77. default_error_builder=ErrorSchema(),
  78. debug=app_config.DEBUG,
  79. )
  80. hapic.set_context(context)
  81. # INFO - G.M - 2018-07-04 - global-context exceptions
  82. # Not found
  83. context.handle_exception(NotFound, HTTPStatus.NOT_FOUND)
  84. # Bad request
  85. context.handle_exception(WorkspaceNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  86. context.handle_exception(UserNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  87. context.handle_exception(ContentNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  88. context.handle_exception(WorkspaceNotFound, HTTPStatus.BAD_REQUEST)
  89. context.handle_exception(UserDoesNotExist, HTTPStatus.BAD_REQUEST)
  90. context.handle_exception(ContentNotFound, HTTPStatus.BAD_REQUEST)
  91. context.handle_exception(ContentTypeNotAllowed, HTTPStatus.BAD_REQUEST)
  92. context.handle_exception(InvalidId, HTTPStatus.BAD_REQUEST)
  93. # Auth exception
  94. context.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
  95. context.handle_exception(UserNotActive, HTTPStatus.FORBIDDEN)
  96. context.handle_exception(AuthenticationFailed, HTTPStatus.FORBIDDEN)
  97. context.handle_exception(InsufficientUserRoleInWorkspace, HTTPStatus.FORBIDDEN) # nopep8
  98. context.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
  99. # Internal server error
  100. context.handle_exception(OperationalError, HTTPStatus.INTERNAL_SERVER_ERROR)
  101. context.handle_exception(Exception, HTTPStatus.INTERNAL_SERVER_ERROR)
  102. # Add controllers
  103. session_controller = SessionController()
  104. system_controller = SystemController()
  105. user_controller = UserController()
  106. workspace_controller = WorkspaceController()
  107. comment_controller = CommentController()
  108. html_document_controller = HTMLDocumentController()
  109. thread_controller = ThreadController()
  110. file_controller = FileController()
  111. configurator.include(session_controller.bind, route_prefix=BASE_API_V2)
  112. configurator.include(system_controller.bind, route_prefix=BASE_API_V2)
  113. configurator.include(user_controller.bind, route_prefix=BASE_API_V2)
  114. configurator.include(workspace_controller.bind, route_prefix=BASE_API_V2)
  115. configurator.include(comment_controller.bind, route_prefix=BASE_API_V2)
  116. configurator.include(html_document_controller.bind, route_prefix=BASE_API_V2) # nopep8
  117. configurator.include(thread_controller.bind, route_prefix=BASE_API_V2)
  118. configurator.include(file_controller.bind, route_prefix=BASE_API_V2)
  119. hapic.add_documentation_view(
  120. '/api/v2/doc',
  121. 'Tracim v2 API',
  122. 'API of Tracim v2',
  123. )
  124. return configurator.make_wsgi_app()
  125. def webdav(global_config, **local_settings):
  126. settings = global_config
  127. settings.update(local_settings)
  128. app_factory = WebdavAppFactory(
  129. tracim_config_file_path=settings['__file__'],
  130. )
  131. return app_factory.get_wsgi_app()