Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #373 from lnielsen/anonymoususer
Browse files Browse the repository at this point in the history
Add support for custom AnonymousUser class.
  • Loading branch information
Matt Wright committed May 2, 2015
2 parents 8a62b5f + a458168 commit 6541640
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions flask_security/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
:license: MIT, see LICENSE for more details.
"""

__version__ = '1.7.4'

from .core import Security, RoleMixin, UserMixin, AnonymousUser, current_user
from .datastore import SQLAlchemyUserDatastore, MongoEngineUserDatastore, PeeweeUserDatastore
from .decorators import auth_token_required, http_auth_required, \
Expand All @@ -21,3 +19,5 @@
from .signals import confirm_instructions_sent, password_reset, \
reset_password_instructions_sent, user_confirmed, user_registered
from .utils import login_user, logout_user, url_for_security

__version__ = '1.7.4'
18 changes: 10 additions & 8 deletions flask_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ def _token_loader(token):
return user
except:
pass
return AnonymousUser()
return _security.login_manager.anonymous_user()


def _identity_loader():
if not isinstance(current_user._get_current_object(), AnonymousUser):
if not isinstance(current_user._get_current_object(), AnonymousUserMixin):
identity = Identity(current_user.id)
return identity

Expand All @@ -218,9 +218,9 @@ def _on_identity_loaded(sender, identity):
identity.user = current_user


def _get_login_manager(app):
def _get_login_manager(app, anonymous_user):
lm = LoginManager()
lm.anonymous_user = AnonymousUser
lm.anonymous_user = anonymous_user or AnonymousUser
lm.login_view = '%s.login' % cv('BLUEPRINT_NAME', app=app)
lm.user_loader(_user_loader)
lm.token_loader(_token_loader)
Expand Down Expand Up @@ -258,14 +258,14 @@ def _get_serializer(app, name):
return URLSafeTimedSerializer(secret_key=secret_key, salt=salt)


def _get_state(app, datastore, **kwargs):
def _get_state(app, datastore, anonymous_user=None, **kwargs):
for key, value in get_config(app).items():
kwargs[key.lower()] = value

kwargs.update(dict(
app=app,
datastore=datastore,
login_manager=_get_login_manager(app),
login_manager=_get_login_manager(app, anonymous_user),
principal=_get_principal(app),
pwd_context=_get_pwd_context(app),
remember_token_serializer=_get_serializer(app, 'remember'),
Expand Down Expand Up @@ -399,7 +399,8 @@ def init_app(self, app, datastore=None, register_blueprint=True,
login_form=None, confirm_register_form=None,
register_form=None, forgot_password_form=None,
reset_password_form=None, change_password_form=None,
send_confirmation_form=None, passwordless_login_form=None):
send_confirmation_form=None, passwordless_login_form=None,
anonymous_user=None):
"""Initializes the Flask-Security extension for the specified
application and datastore implentation.
Expand All @@ -425,7 +426,8 @@ def init_app(self, app, datastore=None, register_blueprint=True,
reset_password_form=reset_password_form,
change_password_form=change_password_form,
send_confirmation_form=send_confirmation_form,
passwordless_login_form=passwordless_login_form)
passwordless_login_form=passwordless_login_form,
anonymous_user=anonymous_user)

if register_blueprint:
app.register_blueprint(create_blueprint(state, __name__))
Expand Down

0 comments on commit 6541640

Please sign in to comment.