Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small change to_groups_required_decorator #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions flask_stormpath/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
from flask import current_app
from flask.ext.login import current_user

not_authorized = 'You are not authorized to view this page, ' \
'please contact your system administrator'
please_login = 'please login to view this page'

def groups_required(groups, all=True):

def healthy_groups_required(groups, all=True):
"""
This decorator requires that a user be part of one or more Groups before
they are granted access.
Expand Down Expand Up @@ -44,17 +48,14 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)

# If the user is NOT authenticated, this user is unauthorized.
elif not current_user.is_authenticated():
return current_app.login_manager.unauthorized()

# If the user authenticated, and the all flag is set, we need to
# see if the user is a member of *ALL* groups.
if all and not current_user.has_groups(groups):
if not current_user.is_authenticated():
current_app.login_manager.login_message = please_login
return current_app.login_manager.unauthorized()

# If the all flag is NOT set, we need to make sure the user is a
# member of at least one group.
elif not current_user.has_groups(groups, all=False):
# If the user authenticated, we need to check if
# he belongs to one / all of the groups (depends on the all flag)
if not current_user.has_groups(groups, all = all):
current_app.login_manager.login_message = not_authorized
return current_app.login_manager.unauthorized()

# Lastly, if the user has successfully passsed all authentication /
Expand Down