def verify_mail(request, user, new_mail): log.info('Trying to verify mail address for user {!r}.'.format(user)) log.debug('Mail address: {!s}.'.format(new_mail)) # Start by removing mail address from any other user old_user_docs = request.db.profiles.find({ 'mailAliases': {'$elemMatch': {'email': new_mail, 'verified': True}} }) steal_count = 0 for old_user_doc in old_user_docs: old_user = User(old_user_doc) if old_user: log.debug('Found old user {!r} with mail address ({!s}) already verified.'.format(old_user, new_mail)) log.debug('Old user mail BEFORE: {!s}.'.format(old_user.get_mail())) log.debug('Old user mail aliases BEFORE: {!r}.'.format(old_user.get_mail_aliases())) if old_user.get_mail() == new_mail: old_user.set_mail('') mails = [m for m in old_user.get_mail_aliases() if m['email'] != new_mail] old_user.set_mail_aliases(mails) log.debug('Old user mail AFTER: {!s}.'.format(old_user.get_mail())) log.debug('Old user mail aliases AFTER: {!r}.'.format(old_user.get_mail_aliases())) old_user.retrieve_modified_ts(request.db.profiles) old_user.save(request) steal_count += 1 # Add the verified mail address to the requesting user user.add_verified_email(new_mail) log.info('Mail address verified for user {!r}.'.format(user)) request.stats.count('dashboard/verify_mail_stolen', steal_count) request.stats.count('dashboard/verify_mail_completed', 1) return user, _('Email {obj} verified')
def test_steal_verified_mail(self): self.set_logged(user='******') response_form = self.testapp.get('/profile/emails/') form = response_form.forms[self.formname] mail = '*****@*****.**' form['mail'].value = mail with patch.object(UserDB, 'exists_by_field', clear=True): UserDB.exists_by_field.return_value = True response = form.submit('add') self.assertEqual(response.status, '200 OK') old_user = self.db.profiles.find_one({'_id': ObjectId('012345678901234567890123')}) old_user = User(old_user) self.assertIn(mail, [ma['email'] for ma in old_user.get_mail_aliases()]) email_doc = self.db.verifications.find_one({ 'model_name': 'mailAliases', 'user_oid': ObjectId('901234567890123456789012'), 'obj_id': mail }) response = self.testapp.post( '/profile/emails-actions/', {'identifier': 3, 'action': 'verify', 'code': email_doc['code']} ) response_json = json.loads(response.body) self.assertEqual(response_json['result'], 'ok') old_user = self.db.profiles.find_one({'_id': ObjectId('012345678901234567890123')}) old_user = User(old_user) self.assertNotIn(mail, [ma['email'] for ma in old_user.get_mail_aliases()])
def verificate_code(request, model_name, code): unverified = request.db.verifications.find_one( { "model_name": model_name, "code": code, }) if not unverified: msg = "Could not find un-verified code {!r}, model {!r}" log.debug(msg.format(code, model_name)) return obj_id = unverified['obj_id'] if obj_id: msg = "Code {!r} ({!s}) marked as verified" log.debug(msg.format(code, str(obj_id))) user = request.userdb.get_user_by_oid(unverified['user_oid']) old_verified = request.db.verifications.find_and_modify( { "model_name": model_name, "obj_id": unverified['obj_id'], "verified": True }, remove=True) old_user = None if old_verified: old_user = request.userdb.get_user_by_oid(old_verified['user_oid']) if model_name == 'norEduPersonNIN': if not old_user: old_user_doc = request.db.profiles.find_one({ 'norEduPersonNIN': obj_id }) if old_user_doc: old_user = User(old_user_doc) if old_user: nins = [nin for nin in old_user.get_nins() if nin != obj_id] old_user.set_nins(nins) addresses = [a for a in old_user.get_addresses() if not a['verified']] old_user.set_addresses(addresses) user.add_verified_nin(obj_id) user.retrieve_address(request, obj_id) # Reset session eduPersonIdentityProofing on NIN verification request.session['eduPersonIdentityProofing'] = None msg = _('National identity number {obj} verified') elif model_name == 'mobile': if not old_user: old_user_doc = request.db.profiles.find_one({ 'mobile': {'$elemMatch': {'mobile': obj_id, 'verified': True}} }) if old_user_doc: old_user = User(old_user_doc) if old_user: mobiles = [m for m in old_user.get_mobiles() if m['mobile'] != obj_id] old_user.set_mobiles(mobiles) user.add_verified_mobile(obj_id) msg = _('Mobile {obj} verified') elif model_name == 'mailAliases': if not old_user: old_user_doc = request.db.profiles.find_one({ 'mailAliases': {'email': obj_id, 'verified': True} }) if old_user_doc: old_user = User(old_user_doc) if old_user: if old_user.get_mail() == obj_id: old_user.set_mail('') mails = [m for m in old_user.get_mail_aliases() if m['email'] != obj_id] old_user.set_mail_aliases(mails) user.add_verified_email(obj_id) msg = _('Email {obj} verified') request.session.flash(msg.format(obj=obj_id), queue='forms') user.save(request) if old_user: old_user.save(request) request.db.verifications.update({'_id': unverified['_id']}, {'verified': True}) return obj_id