diff options
| author | David Beguin <dbe@odoo.com> | 2019-02-20 16:54:36 +0000 |
|---|---|---|
| committer | David Beguin <dbe@odoo.com> | 2019-03-15 14:58:28 +0000 |
| commit | fb40c6bf6018f7f208201e1d4a4d413fd2ce3080 (patch) | |
| tree | d6df1ec5f2e7aca08311a51fad7f2ab3162a6806 /addons/website_profile/models/res_users.py | |
| parent | bbedcdb19054527940fb040a0d4f22115d5be265 (diff) | |
[IMP] website(_profile,_forum,_slides): move validation email to profile and use in forum and elearning
Email validation was necessary on the forum to be able to begin to use the forum
(ask or answer questions, vote, etc..)
As the new elearning also uses karma since 705376a982d64fb41800f148a167ef0fc28ce4c6,
the email validation is now also necessary in the eLearning platform.
This is why this commit is moving the email validation process to website_profile
and extend website_slides (eLearning) and website_forum to use this feature.
In function of where the user asked to send him the validation email,
the user is redirected on the forum or on the elearning when he clicks on
'Validate my account' in the received 'email validation' email.
Task ID : 1943788
PR #31321
Diffstat (limited to 'addons/website_profile/models/res_users.py')
| -rw-r--r-- | addons/website_profile/models/res_users.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/addons/website_profile/models/res_users.py b/addons/website_profile/models/res_users.py new file mode 100644 index 00000000000..5bf49be0a78 --- /dev/null +++ b/addons/website_profile/models/res_users.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import hashlib +import uuid + +from datetime import datetime +from werkzeug import urls +from odoo import api, models + +VALIDATION_KARMA_GAIN = 3 + + +class Users(models.Model): + _inherit = 'res.users' + + @api.model + def _generate_profile_token(self, user_id, email): + """Return a token for email validation. This token is valid for the day + and is a hash based on a (secret) uuid generated by the forum module, + the user_id, the email and currently the day (to be updated if necessary). """ + profile_uuid = self.env['ir.config_parameter'].sudo().get_param('website_profile.uuid') + if not profile_uuid: + profile_uuid = str(uuid.uuid4()) + self.env['ir.config_parameter'].sudo().set_param('website_profile.uuid', profile_uuid) + return hashlib.sha256((u'%s-%s-%s-%s' % ( + datetime.now().replace(hour=0, minute=0, second=0, microsecond=0), + profile_uuid, + user_id, + email + )).encode('utf-8')).hexdigest() + + def _send_profile_validation_email(self, **kwargs): + if not self.email: + return False + token = self._generate_profile_token(self.id, self.email) + activation_template = self.env.ref('website_profile.validation_email') + if activation_template: + params = { + 'token': token, + 'user_id': self.id, + 'email': self.email + } + params.update(kwargs) + base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + token_url = base_url + '/profile/validate_email?%s' % urls.url_encode(params) + with self._cr.savepoint(): + activation_template.sudo().with_context(token_url=token_url).send_mail( + self.id, force_send=True, raise_exception=True) + return True + + @api.multi + def _process_profile_validation_token(self, token, email): + self.ensure_one() + validation_token = self._generate_profile_token(self.id, email) + if token == validation_token and self.karma == 0: + return self.write({'karma': VALIDATION_KARMA_GAIN}) + return False |
