Skip to content

Commit

Permalink
Added check for anonymous user. Closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Ceratto committed Feb 7, 2013
1 parent e28d4fb commit f735c15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cork/cork.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,23 @@ def current_user(self):
return User(username, self, session=session)
raise AuthException("Unknown user: %s" % username)

@property
def user_is_anonymous(self):
"""Check if the current user is anonymous.
:returns: True if the user is anonymous, False otherwise
:raises: AuthException if the session username is unknown
"""
try:
username = self._beaker_session['username']
except KeyError:
return True

if username not in self._store.users:
raise AuthException("Unknown user: %s" % username)

return False

def user(self, username):
"""Existing user
Expand Down
7 changes: 7 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ def test_successful_require_role():
aaa.require(username='admin', role='admin', fixed_role=True)
aaa.require(username='admin', role='user')

@with_setup(setup_mockedadmin, teardown_dir)
def test_authenticated_is_not__anonymous():
assert not aaa.user_is_anonymous

@with_setup(setup_mockedadmin, teardown_dir)
def test_update_nonexistent_role():
Expand Down Expand Up @@ -376,6 +379,10 @@ def test_update_email():
def test_get_current_user_unauth():
aaa.current_user['username']

@with_setup(setup_mocked_unauthenticated, teardown_dir)
def test_unauth_is_anonymous():
assert aaa.user_is_anonymous

@raises(AuthException)
@with_setup(setup_mockedadmin, teardown_dir)
def test_get_current_user_nonexistent():
Expand Down

0 comments on commit f735c15

Please sign in to comment.