You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2015/02/16 12:44:43 UTC

[12/37] allura git commit: [#7823] ticket:724 Change EmailAddress.canonical behavior on invalid emails

[#7823] ticket:724 Change EmailAddress.canonical behavior on invalid emails

Return None, instead of 'nobody@example.com'.


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/0d6a4431
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/0d6a4431
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/0d6a4431

Branch: refs/heads/ib/4542
Commit: 0d6a44318b399203a9e732d276b6ebf873e36d8c
Parents: 1c2d973
Author: Igor Bondarenko <je...@gmail.com>
Authored: Thu Feb 12 13:10:32 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri Feb 13 20:48:35 2015 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/auth.py | 32 +++++++++++++++-------------
 Allura/allura/lib/utils.py        | 38 +++++++++++++++++++++++++++++++++-
 Allura/allura/model/auth.py       | 28 +++++++++++++++++--------
 Allura/allura/tests/test_utils.py | 19 ++++++++++++++++-
 4 files changed, 92 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/0d6a4431/Allura/allura/controllers/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/auth.py b/Allura/allura/controllers/auth.py
index 453ad8a..2183032 100644
--- a/Allura/allura/controllers/auth.py
+++ b/Allura/allura/controllers/auth.py
@@ -232,7 +232,8 @@ class AuthController(BaseController):
         user.set_tool_data('allura', pwd_reset_preserve_session=session.id)  # else the first password set causes this session to be invalidated
         if require_email:
             em = user.claim_address(email)
-            em.send_verification_link()
+            if em:
+                em.send_verification_link()
             flash('User "%s" registered. Verification link was sent to your email.' % username)
         else:
             plugin.AuthenticationProvider.get(request).login(user)
@@ -490,22 +491,25 @@ class PreferencesController(BaseController):
 
             elif mail_util.isvalid(new_addr['addr']):
                 em = M.EmailAddress.create(new_addr['addr'])
-                user.email_addresses.append(em.email)
-                em.claimed_by_user_id = user._id
-
-                confirmed_emails = filter(lambda email: email.confirmed, claimed_emails)
-                if not confirmed_emails:
-                    if not admin:
-                        em.send_verification_link()
+                if em:
+                    user.email_addresses.append(em.email)
+                    em.claimed_by_user_id = user._id
+
+                    confirmed_emails = filter(lambda email: email.confirmed, claimed_emails)
+                    if not confirmed_emails:
+                        if not admin:
+                            em.send_verification_link()
+                        else:
+                            AuthController()._verify_addr(em)
                     else:
-                        AuthController()._verify_addr(em)
-                else:
-                    em.send_claim_attempt()
+                        em.send_claim_attempt()
 
-                if not admin:
-                    flash('A verification email has been sent.  Please check your email and click to confirm.')
+                    if not admin:
+                        flash('A verification email has been sent.  Please check your email and click to confirm.')
 
-                h.auditlog_user('New email address: %s', new_addr['addr'], user=user)
+                    h.auditlog_user('New email address: %s', new_addr['addr'], user=user)
+                else:
+                    flash('Email address %s is invalid' % new_addr['addr'], 'error')
             else:
                 flash('Email address %s is invalid' % new_addr['addr'], 'error')
         if not primary_addr and not user.get_pref('email_address') and user.email_addresses:

http://git-wip-us.apache.org/repos/asf/allura/blob/0d6a4431/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 35bc039..7a56603 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -48,6 +48,7 @@ import html5lib.sanitizer
 
 from ew import jinja2_ew as ew
 from ming.utils import LazyProperty
+from ming.odm.odmsession import ODMCursor
 
 
 MARKDOWN_EXTENSIONS = ['.markdown', '.mdown', '.mkdn', '.mkd', '.md']
@@ -555,4 +556,39 @@ def ip_address(request):
     ip = request.remote_addr
     if tg.config.get('ip_address_header'):
         ip = request.headers.get(tg.config['ip_address_header']) or ip
-    return ip
\ No newline at end of file
+    return ip
+
+
+class EmptyCursor(ODMCursor):
+    """Ming cursor with no results"""
+
+    def __init__(self, *args, **kw):
+        pass
+
+    @property
+    def extensions(self):
+        return []
+
+    def count(self):
+        return 0
+
+    def _next_impl(self):
+        raise StopIteration
+
+    def next(self):
+        raise StopIteration
+
+    def options(self, **kw):
+        return self
+
+    def limit(self, limit):
+        return self
+
+    def skip(self, skip):
+        return self
+
+    def hint(self, index_or_name):
+        return self
+
+    def sort(self, *args, **kw):
+        return self

http://git-wip-us.apache.org/repos/asf/allura/blob/0d6a4431/Allura/allura/model/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index e8bd8a6..0b92443 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -128,7 +128,11 @@ class EmailAddress(MappedClass):
         '''Equivalent to Ming's query.get but calls self.canonical on address
         before lookup. You should always use this instead of query.get'''
         if kw.get('email'):
-            kw['email'] = cls.canonical(kw['email'])
+            email = cls.canonical(kw['email'])
+            if email is not None:
+                kw['email'] = email
+            else:
+                return None
         return cls.query.get(**kw)
 
     @classmethod
@@ -137,7 +141,11 @@ class EmailAddress(MappedClass):
         before lookup. You should always use this instead of query.find'''
         if q:
             if q.get('email'):
-                q['email'] = cls.canonical(q['email'])
+                email = cls.canonical(q['email'])
+                if email is not None:
+                    q['email'] = email
+                else:
+                    return utils.EmptyCursor()
             return cls.query.find(q)
         return cls.query.find()
 
@@ -152,7 +160,8 @@ class EmailAddress(MappedClass):
     @classmethod
     def create(cls, addr):
         addr = cls.canonical(addr)
-        return cls(email=addr)
+        if addr is not None:
+            return cls(email=addr)
 
     @classmethod
     def canonical(cls, addr):
@@ -163,7 +172,7 @@ class EmailAddress(MappedClass):
             user, domain = addr.split('@')
             return '%s@%s' % (user, domain.lower())
         else:
-            return 'nobody@example.com'
+            return None
 
     def send_claim_attempt(self):
         confirmed_email = self.find(dict(email=self.email, confirmed=True)).all()
@@ -666,11 +675,12 @@ class User(MappedClass, ActivityNode, ActivityObject, SearchIndexable):
     def claim_address(self, email_address):
         addr = EmailAddress.canonical(email_address)
         email_addr = EmailAddress.create(addr)
-        email_addr.claimed_by_user_id = self._id
-        if addr not in self.email_addresses:
-            self.email_addresses.append(addr)
-        session(email_addr).flush(email_addr)
-        return email_addr
+        if email_addr:
+            email_addr.claimed_by_user_id = self._id
+            if addr not in self.email_addresses:
+                self.email_addresses.append(addr)
+            session(email_addr).flush(email_addr)
+            return email_addr
 
     @classmethod
     def register(cls, doc, make_project=True):

http://git-wip-us.apache.org/repos/asf/allura/blob/0d6a4431/Allura/allura/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_utils.py b/Allura/allura/tests/test_utils.py
index 209d0df..e5f9c43 100644
--- a/Allura/allura/tests/test_utils.py
+++ b/Allura/allura/tests/test_utils.py
@@ -23,7 +23,7 @@ from os import path
 
 from webob import Request
 from mock import Mock, patch
-from nose.tools import assert_equal
+from nose.tools import assert_equal, assert_raises
 from pygments import highlight
 from pygments.lexers import get_lexer_for_filename
 from tg import config
@@ -278,3 +278,20 @@ def test_ip_address_header_not_set():
     with h.push_config(config, **{'ip_address_header': 'X_FORWARDED_FOR'}):
         assert_equal(utils.ip_address(req),
                      '1.2.3.4')
+
+
+def test_empty_cursor():
+    """EmptyCursors conforms to specification of Ming's ODMCursor"""
+    cursor = utils.EmptyCursor()
+    assert_equal(cursor.count(), 0)
+    assert_equal(cursor.first(), None)
+    assert_equal(cursor.all(), [])
+    assert_equal(cursor.limit(10), cursor)
+    assert_equal(cursor.skip(10), cursor)
+    assert_equal(cursor.sort('name', 1), cursor)
+    assert_equal(cursor.hint('index'), cursor)
+    assert_equal(cursor.extensions, [])
+    assert_equal(cursor.options(arg1='val1', arg2='val2'), cursor)
+    assert_raises(ValueError, cursor.one)
+    assert_raises(StopIteration, cursor.next)
+    assert_raises(StopIteration, cursor._next_impl)