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.

71 lines
2.5 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. import urllib
  23. import json
  24. from openerp import models, fields, api
  25. class ResBannedRemote(models.Model):
  26. _name = 'res.banned.remote'
  27. _rec_name = 'remote'
  28. _GEOLOCALISATION_URL = "http://ip-api.com/json/{}"
  29. # Default Section
  30. def _default_ban_date(self):
  31. return fields.Datetime.now()
  32. # Column Section
  33. description = fields.Text(
  34. string='Description', compute='_compute_description', store=True)
  35. ban_date = fields.Datetime(
  36. string='Ban Date', required=True, default=_default_ban_date)
  37. remote = fields.Char(string='Remote ID', required=True)
  38. active = fields.Boolean(
  39. string='Active', help="Uncheck this box to unban the remote",
  40. default=True)
  41. attempt_ids = fields.Many2many(
  42. comodel_name='res.authentication.attempt', string='Attempts',
  43. compute='_compute_attempt_ids')
  44. # Compute Section
  45. @api.multi
  46. @api.depends('remote')
  47. def _compute_description(self):
  48. for item in self:
  49. url = self._GEOLOCALISATION_URL.format(item.remote)
  50. res = json.loads(urllib.urlopen(url).read())
  51. item.description = ''
  52. for k, v in res.iteritems():
  53. item.description += '%s : %s\n' % (k, v)
  54. @api.multi
  55. def _compute_attempt_ids(self):
  56. for item in self:
  57. attempt_obj = self.env['res.authentication.attempt']
  58. item.attempt_ids = attempt_obj.search_last_failed(item.remote).ids