No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exceptions.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # @package lodel.auth.exceptions
  20. # @brief Defines the specific exceptions used in the authentication process
  21. from lodel.context import LodelContext
  22. LodelContext.expose_modules(globals(), {
  23. 'lodel.logger': 'logger',
  24. 'lodel.plugin.hooks': ['LodelHook']})
  25. ## @brief Handles common errors with a Client
  26. class ClientError(Exception):
  27. ## @brief The logger function to use to log the error message
  28. _loglvl = 'warning'
  29. ## @brief Error string
  30. _err_str = "Error"
  31. ## @brief the hook name to trigger with error
  32. _action = 'lodel2_ui_error'
  33. ## @brief the hook payload
  34. _payload = None
  35. ## @brief Constructor
  36. #
  37. # Log messages are built using the following format :
  38. # "<client infos> : <_err_str>[ : <msg>]"
  39. # @param client Client : object containing the client's data
  40. # @param msg str : message string to use
  41. def __init__(self, client, msg = ""):
  42. msg = self.build_message(client, msg)
  43. if self._loglvl is not None:
  44. logfun = getattr(logger, self._loglvl)
  45. logfun(msg)
  46. super().__init__(msg)
  47. if self._action is not None:
  48. LodelHook.call_hook(self._action, self, self._payload)
  49. ## @brief Builds an error message
  50. # @param client Client
  51. # @param msg str
  52. def build_message(self, client, msg):
  53. res = "%s : %s" % (client, self._err_str)
  54. if len(msg) > 0:
  55. res += " : %s" % msg
  56. return res
  57. ## @brief Handles authentication failure errors
  58. class ClientAuthenticationFailure(ClientError):
  59. ## @brief Log Level
  60. _loglvl = 'security'
  61. ## @brief Error string
  62. _err_str = 'Authentication failure'
  63. ## @brief Hook to trigger
  64. _action = 'lodel2_ui_authentication_failure'
  65. ##@brief Handles permission denied errors
  66. class ClientPermissionDenied(ClientError):
  67. ## @brief Log level
  68. _loglvl = 'security'
  69. ## @brief Error string
  70. _err_str = 'Permission denied'
  71. ## @brief Hook to trigger
  72. _action = 'lodel2_ui_permission_denied'
  73. ##@brief Handles common errors on authentication
  74. class ClientAuthenticationError(ClientError):
  75. ## @brief Log level
  76. _loglvl = 'error'
  77. ## @brief Error string
  78. _err_str = 'Authentication error'
  79. ## @brief Hook to trigger
  80. _action = 'lodel2_ui_error'