res_users.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import hashlib
  4. import uuid
  5. from datetime import datetime
  6. from werkzeug import urls
  7. from odoo import api, models
  8. VALIDATION_KARMA_GAIN = 3
  9. class Users(models.Model):
  10. _inherit = 'res.users'
  11. @property
  12. def SELF_READABLE_FIELDS(self):
  13. return super().SELF_READABLE_FIELDS + ['karma']
  14. @property
  15. def SELF_WRITEABLE_FIELDS(self):
  16. return super().SELF_WRITEABLE_FIELDS + [
  17. 'country_id', 'city', 'website', 'website_description', 'website_published',
  18. ]
  19. @api.model
  20. def _generate_profile_token(self, user_id, email):
  21. """Return a token for email validation. This token is valid for the day
  22. and is a hash based on a (secret) uuid generated by the forum module,
  23. the user_id, the email and currently the day (to be updated if necessary). """
  24. profile_uuid = self.env['ir.config_parameter'].sudo().get_param('website_profile.uuid')
  25. if not profile_uuid:
  26. profile_uuid = str(uuid.uuid4())
  27. self.env['ir.config_parameter'].sudo().set_param('website_profile.uuid', profile_uuid)
  28. return hashlib.sha256((u'%s-%s-%s-%s' % (
  29. datetime.now().replace(hour=0, minute=0, second=0, microsecond=0),
  30. profile_uuid,
  31. user_id,
  32. email
  33. )).encode('utf-8')).hexdigest()
  34. def _send_profile_validation_email(self, **kwargs):
  35. if not self.email:
  36. return False
  37. token = self._generate_profile_token(self.id, self.email)
  38. activation_template = self.env.ref('website_profile.validation_email')
  39. if activation_template:
  40. params = {
  41. 'token': token,
  42. 'user_id': self.id,
  43. 'email': self.email
  44. }
  45. params.update(kwargs)
  46. token_url = self.get_base_url() + '/profile/validate_email?%s' % urls.url_encode(params)
  47. with self._cr.savepoint():
  48. activation_template.sudo().with_context(token_url=token_url).send_mail(
  49. self.id, force_send=True, raise_exception=True)
  50. return True
  51. def _process_profile_validation_token(self, token, email):
  52. self.ensure_one()
  53. validation_token = self._generate_profile_token(self.id, email)
  54. if token == validation_token and self.karma == 0:
  55. return self.write({'karma': VALIDATION_KARMA_GAIN})
  56. return False