Skip to content

Commit

Permalink
Merge branch 'master' of github.com:brack3t/django-braces
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethlove committed Jul 27, 2013
2 parents a85a525 + 77c344d commit ba79567
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
24 changes: 16 additions & 8 deletions braces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,26 @@ def get_group_required(self):
"following types: string, unicode, list, or tuple.")
return self.group_required

def check_membership(self, group):
""" Check required group(s) """
if not group in self.request.user.groups.values_list('name',
flat=True):
return False
return True

def dispatch(self, request, *args, **kwargs):
required_group = self.get_group_required()
if not required_group in request.user.groups.values_list('name',
flat=True):
in_group = self.check_membership(self.get_group_required())

if not in_group:
if self.raise_exception:
raise PermissionDenied
else:
return redirect_to_login(request.get_full_path(),
self.get_login_url(),
self.get_redirect_field_name())
return super(GroupRequiredMixin, self).dispatch(request, *args,
**kwargs)
return redirect_to_login(
request.get_full_path(),
self.get_login_url(),
self.get_redirect_field_name())
return super(GroupRequiredMixin, self).dispatch(
request, *args, **kwargs)


class UserFormKwargsMixin(object):
Expand Down
27 changes: 26 additions & 1 deletion docs/access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ The group required view mixin ensures that the requesting user is in the group o

.. note::
The mixin assumes you're using Django's default Group model and that your user model provides ``groups`` as a ManyToMany relationship.
If this **is not** the case, you'll need to override `dispatch` in the mixin to handle your custom set up.
If this **is not** the case, you'll need to override `check_membership` in the mixin to handle your custom set up.

Standard Django Usage
---------------------

::

Expand All @@ -112,6 +115,28 @@ The group required view mixin ensures that the requesting user is in the group o
group_required = u'editors'


Custom Group Usage
------------------

::

from braces.views import GroupRequiredMixin


class SomeProtectedView(GroupRequiredMixin, TemplateView):

#required
group_required = u'editors'

def check_membership(self, group):
...
# Check some other system for group membership
if user_in_group:
return True
else:
return False


SuperuserRequiredMixin
----------------------

Expand Down

0 comments on commit ba79567

Please sign in to comment.