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.

58 lines
2.1 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Tracks Authentication Attempts and Prevents Brute-force Attacks module
  5. # Copyright (C) 2015-Today GRAP (http://www.grap.coop)
  6. # @author Sylvain LE GAL (https://twitter.com/legalsylvain)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import models, fields, api
  23. from openerp.tools.translate import _
  24. class ResAuthenticationAttempt(models.Model):
  25. _name = 'res.authentication.attempt'
  26. _order = 'attempt_date desc'
  27. _ATTEMPT_RESULT = [
  28. ('successfull', _('Successfull')),
  29. ('failed', _('Failed')),
  30. ('banned', _('Banned')),
  31. ]
  32. # Column Section
  33. attempt_date = fields.Datetime(string='Attempt Date')
  34. login = fields.Char(string='Tried Login')
  35. remote = fields.Char(string='Remote ID')
  36. result = fields.Selection(
  37. selection=_ATTEMPT_RESULT, string='Authentication Result')
  38. # Custom Section
  39. @api.model
  40. def search_last_failed(self, remote):
  41. last_ok = self.search(
  42. [('result', '=', 'successfull'), ('remote', '=', remote)],
  43. order='attempt_date desc', limit=1)
  44. if last_ok:
  45. return self.search([
  46. ('remote', '=', remote),
  47. ('attempt_date', '>', last_ok.attempt_date)])
  48. else:
  49. return self.search([('remote', '=', remote)])