def get_objects_for_group(group, perms, klass=None, any_perm=False, accept_global_perms=True): """ Returns queryset of objects for which a given ``group`` has *all* permissions present at ``perms``. :param group: ``Group`` instance for which objects would be returned. :param perms: single permission string, or sequence of permission strings which should be checked. If ``klass`` parameter is not given, those should be full permission names rather than only codenames (i.e. ``auth.change_user``). If more than one permission is present within sequence, their content type **must** be the same or ``MixedContentTypeError`` exception would be raised. :param klass: may be a Model, Manager or QuerySet object. If not given this parameter would be computed based on given ``params``. :param any_perm: if True, any of permission in sequence is accepted :param accept_global_perms: if ``True`` takes global permissions into account. If any_perm is set to false then the intersection of matching objects based on global and object based permissions is returned. Default is ``True``. :raises MixedContentTypeError: when computed content type for ``perms`` and/or ``klass`` clashes. :raises WrongAppError: if cannot compute app label for given ``perms``/ ``klass``. Example: Let's assume we have a ``Task`` model belonging to the ``tasker`` app with the default add_task, change_task and delete_task permissions provided by Django:: >>> from guardian.shortcuts import get_objects_for_group >>> from tasker import Task >>> group = Group.objects.create('some group') >>> task = Task.objects.create('some task') >>> get_objects_for_group(group, 'tasker.add_task') [] >>> from guardian.shortcuts import assign_perm >>> assign_perm('tasker.add_task', group, task) >>> get_objects_for_group(group, 'tasker.add_task') [<Task some task>] The permission string can also be an iterable. Continuing with the previous example: >>> get_objects_for_group(group, ['tasker.add_task', 'tasker.delete_task']) [] >>> assign_perm('tasker.delete_task', group, task) >>> get_objects_for_group(group, ['tasker.add_task', 'tasker.delete_task']) [<Task some task>] Global permissions assigned to the group are also taken into account. Continuing with previous example: >>> task_other = Task.objects.create('other task') >>> assign_perm('tasker.change_task', group) >>> get_objects_for_group(group, ['tasker.change_task']) [<Task some task>, <Task other task>] >>> get_objects_for_group(group, ['tasker.change_task'], accept_global_perms=False) [<Task some task>] """ if isinstance(perms, basestring): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError("Given perms must have same app " "label (%s != %s)" % (app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError("ContentType was once computed " "to be %s and another one %s" % (ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = get_content_type(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) elif klass is None: raise WrongAppError("Cannot determine content type") else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model: raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have ``codenames`` list global_perms = set() if accept_global_perms: global_perm_set = group.permissions.values_list('codename', flat=True) for code in codenames: if code in global_perm_set: global_perms.add(code) for code in global_perms: codenames.remove(code) if len(global_perms) > 0 and (len(codenames) == 0 or any_perm): return queryset # Now we should extract list of pk values for which we would filter # queryset group_model = get_group_obj_perms_model(queryset.model) groups_obj_perms_queryset = (group_model.objects.filter( group=group).filter(permission__content_type=ctype)) if len(codenames): groups_obj_perms_queryset = groups_obj_perms_queryset.filter( permission__codename__in=codenames) if group_model.objects.is_generic(): fields = ['object_pk', 'permission__codename'] else: fields = ['content_object__pk', 'permission__codename'] if not any_perm and len(codenames): groups_obj_perms = groups_obj_perms_queryset.values_list(*fields) data = list(groups_obj_perms) keyfunc = lambda t: t[ 0] # sorting/grouping by pk (first in result tuple) data = sorted(data, key=keyfunc) pk_list = [] for pk, group in groupby(data, keyfunc): obj_codenames = set((e[1] for e in group)) if any_perm or codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(pk__in=pk_list) return objects values = groups_obj_perms_queryset.values_list(fields[0], flat=True) if group_model.objects.is_generic(): values = list(values) return queryset.filter(pk__in=values)
def get_objects_for_user(user, perms, klass=None, use_groups=True, any_perm=False, with_superuser=True, accept_global_perms=True): """ Returns queryset of objects for which a given ``user`` has *all* permissions present at ``perms``. If ``perms`` is an empty list, then it returns objects for which a given ``user`` has *any* object permission. :param user: ``User`` or ``AnonymousUser`` instance for which objects would be returned. :param perms: single permission string, or sequence of permission strings which should be checked. If ``klass`` parameter is not given, those should be full permission names rather than only codenames (i.e. ``auth.change_user``). If more than one permission is present within sequence, their content type **must** be the same or ``MixedContentTypeError`` exception would be raised. :param klass: may be a Model, Manager or QuerySet object. If not given this parameter would be computed based on given ``params``. :param use_groups: if ``False``, wouldn't check user's groups object permissions. Default is ``True``. :param any_perm: if True, any of permission in sequence is accepted. Default is ``False``. :param with_superuser: if ``True`` and if ``user.is_superuser`` is set, returns the entire queryset. Otherwise will only return objects the user has explicit permissions. This must be ``True`` for the accept_global_perms parameter to have any affect. Default is ``True``. :param accept_global_perms: if ``True`` takes global permissions into account. Object based permissions are taken into account if more than one permission is handed in in perms and at least one of these perms is not globally set. If any_perm is set to false then the intersection of matching object is returned. Note, that if with_superuser is False, accept_global_perms will be ignored, which means that only object permissions will be checked! Default is ``True``. :raises MixedContentTypeError: when computed content type for ``perms`` and/or ``klass`` clashes. :raises WrongAppError: if cannot compute app label for given ``perms``/ ``klass``. Example:: >>> from django.contrib.auth.models import User >>> from guardian.shortcuts import get_objects_for_user >>> joe = User.objects.get(username='******') >>> get_objects_for_user(joe, 'auth.change_group') [] >>> from guardian.shortcuts import assign_perm >>> group = Group.objects.create('some group') >>> assign_perm('auth.change_group', joe, group) >>> get_objects_for_user(joe, 'auth.change_group') [<Group some group>] The permission string can also be an iterable. Continuing with the previous example: >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group']) [] >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group'], any_perm=True) [<Group some group>] >>> assign_perm('auth.delete_group', joe, group) >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group']) [<Group some group>] Take global permissions into account: >>> jack = User.objects.get(username='******') >>> assign_perm('auth.change_group', jack) # this will set a global permission >>> get_objects_for_user(jack, 'auth.change_group') [<Group some group>] >>> group2 = Group.objects.create('other group') >>> assign_perm('auth.delete_group', jack, group2) >>> get_objects_for_user(jack, ['auth.change_group', 'auth.delete_group']) # this retrieves intersection [<Group other group>] >>> get_objects_for_user(jack, ['auth.change_group', 'auth.delete_group'], any_perm) # this retrieves union [<Group some group>, <Group other group>] If accept_global_perms is set to ``True``, then all assigned global permissions will also be taken into account. - Scenario 1: a user has view permissions generally defined on the model 'books' but no object based permission on a single book instance: - If accept_global_perms is ``True``: List of all books will be returned. - If accept_global_perms is ``False``: list will be empty. - Scenario 2: a user has view permissions generally defined on the model 'books' and also has an object based permission to view book 'Whatever': - If accept_global_perms is ``True``: List of all books will be returned. - If accept_global_perms is ``False``: list will only contain book 'Whatever'. - Scenario 3: a user only has object based permission on book 'Whatever': - If accept_global_perms is ``True``: List will only contain book 'Whatever'. - If accept_global_perms is ``False``: List will only contain book 'Whatever'. - Scenario 4: a user does not have any permission: - If accept_global_perms is ``True``: Empty list. - If accept_global_perms is ``False``: Empty list. """ if isinstance(perms, basestring): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError("Given perms must have same app " "label (%s != %s)" % (app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError("ContentType was once computed " "to be %s and another one %s" % (ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = get_content_type(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) elif klass is None: raise WrongAppError("Cannot determine content type") else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model: raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have ``codenames`` list # First check if user is superuser and if so, return queryset immediately if with_superuser and user.is_superuser: return queryset # Check if the user is anonymous. The # django.contrib.auth.models.AnonymousUser object doesn't work for queries # and it's nice to be able to pass in request.user blindly. if user.is_anonymous(): user = get_anonymous_user() global_perms = set() has_global_perms = False # a superuser has by default assigned global perms for any if accept_global_perms and with_superuser: for code in codenames: if user.has_perm(ctype.app_label + '.' + code): global_perms.add(code) for code in global_perms: codenames.remove(code) # prerequisite: there must be elements in global_perms otherwise just follow the procedure for # object based permissions only AND # 1. codenames is empty, which means that permissions are ONLY set globally, therefore return the full queryset. # OR # 2. any_perm is True, then the global permission beats the object based permission anyway, # therefore return full queryset if len(global_perms) > 0 and (len(codenames) == 0 or any_perm): return queryset # if we have global perms and still some object based perms differing from global perms and any_perm is set # to false, then we have to flag that global perms exist in order to merge object based permissions by user # and by group correctly. Scenario: global perm change_xx and object based perm delete_xx on object A for user, # and object based permission delete_xx on object B for group, to which user is assigned. # get_objects_for_user(user, [change_xx, delete_xx], use_groups=True, any_perm=False, accept_global_perms=True) # must retrieve object A and B. elif len(global_perms) > 0 and (len(codenames) > 0): has_global_perms = True # Now we should extract list of pk values for which we would filter # queryset user_model = get_user_obj_perms_model(queryset.model) user_obj_perms_queryset = (user_model.objects.filter(user=user).filter( permission__content_type=ctype)) if len(codenames): user_obj_perms_queryset = user_obj_perms_queryset.filter( permission__codename__in=codenames) direct_fields = ['content_object__pk', 'permission__codename'] generic_fields = ['object_pk', 'permission__codename'] if user_model.objects.is_generic(): user_fields = generic_fields else: user_fields = direct_fields if use_groups: group_model = get_group_obj_perms_model(queryset.model) group_filters = { 'permission__content_type': ctype, 'group__%s' % get_user_model().groups.field.related_query_name(): user, } if len(codenames): group_filters.update({ 'permission__codename__in': codenames, }) groups_obj_perms_queryset = group_model.objects.filter(**group_filters) if group_model.objects.is_generic(): group_fields = generic_fields else: group_fields = direct_fields if not any_perm and len(codenames) > 1 and not has_global_perms: user_obj_perms = user_obj_perms_queryset.values_list(*user_fields) groups_obj_perms = groups_obj_perms_queryset.values_list( *group_fields) data = list(user_obj_perms) + list(groups_obj_perms) # sorting/grouping by pk (first in result tuple) keyfunc = lambda t: t[0] data = sorted(data, key=keyfunc) pk_list = [] for pk, group in groupby(data, keyfunc): obj_codenames = set((e[1] for e in group)) if codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(pk__in=pk_list) return objects if not any_perm and len(codenames) > 1: counts = user_obj_perms_queryset.values( user_fields[0]).annotate(object_pk_count=Count(user_fields[0])) user_obj_perms_queryset = counts.filter( object_pk_count__gte=len(codenames)) values = user_obj_perms_queryset.values_list(user_fields[0], flat=True) if user_model.objects.is_generic(): values = set(values) q = Q(pk__in=values) if use_groups: values = groups_obj_perms_queryset.values_list(group_fields[0], flat=True) if group_model.objects.is_generic(): values = set(values) q |= Q(pk__in=values) return queryset.filter(q)
def get_objects_for_user(user, perms, klass=None, use_groups=True, any_perm=False, with_superuser=True): """ Returns queryset of objects for which a given ``user`` has *all* permissions present at ``perms``. :param user: ``User`` or ``AnonymousUser`` instance for which objects would be returned. :param perms: single permission string, or sequence of permission strings which should be checked. If ``klass`` parameter is not given, those should be full permission names rather than only codenames (i.e. ``auth.change_user``). If more than one permission is present within sequence, their content type **must** be the same or ``MixedContentTypeError`` exception would be raised. :param klass: may be a Model, Manager or QuerySet object. If not given this parameter would be computed based on given ``params``. :param use_groups: if ``False``, wouldn't check user's groups object permissions. Default is ``True``. :param any_perm: if True, any of permission in sequence is accepted :param with_superuser: if ``True`` returns the entire queryset if not it will only return objects the user has explicit permissions. :raises MixedContentTypeError: when computed content type for ``perms`` and/or ``klass`` clashes. :raises WrongAppError: if cannot compute app label for given ``perms``/ ``klass``. Example:: >>> from django.contrib.auth.models import User >>> from guardian.shortcuts import get_objects_for_user >>> joe = User.objects.get(username='******') >>> get_objects_for_user(joe, 'auth.change_group') [] >>> from guardian.shortcuts import assign_perm >>> group = Group.objects.create('some group') >>> assign_perm('auth.change_group', joe, group) >>> get_objects_for_user(joe, 'auth.change_group') [<Group some group>] The permission string can also be an iterable. Continuing with the previous example: >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group']) [] >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group'], any_perm=True) [<Group some group>] >>> assign_perm('auth.delete_group', joe, group) >>> get_objects_for_user(joe, ['auth.change_group', 'auth.delete_group']) [<Group some group>] """ if isinstance(perms, str): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError("Given perms must have same app " "label (%s != %s)" % (app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError("ContentType was once computed " "to be %s and another one %s" % (ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is None: raise WrongAppError("Cannot determine content type") elif ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = ContentType.objects.get_for_model(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model: raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have ``codenames`` list # First check if user is superuser and if so, return queryset immediately if with_superuser and user.is_superuser: return queryset # Check if the user is anonymous. The # django.contrib.auth.models.AnonymousUser object doesn't work for queries # and it's nice to be able to pass in request.user blindly. if user.is_anonymous(): user = get_anonymous_user() # Now we should extract list of pk values for which we would filter queryset user_model = get_user_obj_perms_model(queryset.model) user_obj_perms_queryset = (user_model.objects.filter(user=user).filter( permission__content_type=ctype).filter( permission__codename__in=codenames)) if user_model.objects.is_generic(): fields = ['object_pk', 'permission__codename'] else: fields = ['content_object__pk', 'permission__codename'] if use_groups: group_model = get_group_obj_perms_model(queryset.model) group_filters = { 'permission__content_type': ctype, 'permission__codename__in': codenames, 'group__%s' % get_user_model().groups.field.related_query_name(): user, } groups_obj_perms_queryset = group_model.objects.filter(**group_filters) if group_model.objects.is_generic(): fields = ['object_pk', 'permission__codename'] else: fields = ['content_object__pk', 'permission__codename'] if not any_perm: user_obj_perms = user_obj_perms_queryset.values_list(*fields) groups_obj_perms = groups_obj_perms_queryset.values_list(*fields) data = list(user_obj_perms) + list(groups_obj_perms) keyfunc = lambda t: t[ 0] # sorting/grouping by pk (first in result tuple) data = sorted(data, key=keyfunc) pk_list = [] for pk, group in groupby(data, keyfunc): obj_codenames = set((e[1] for e in group)) if codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(pk__in=pk_list) return objects if not any_perm and len(codenames) > 1: counts = user_obj_perms_queryset.values( fields[0]).annotate(object_pk_count=Count(fields[0])) user_obj_perms_queryset = counts.filter( object_pk_count__gte=len(codenames)) values = user_obj_perms_queryset.values_list(fields[0], flat=True) if user_model.objects.is_generic(): values = list(values) objects = queryset.filter(pk__in=values) if use_groups: values = groups_obj_perms_queryset.values_list(fields[0], flat=True) if group_model.objects.is_generic(): values = list(values) objects |= queryset.filter(pk__in=values) return objects
def get_objects_for_user(user, perms, klass=None, use_groups=True, any_perm=False, with_superuser=True, accept_global_perms=True, perms_filter='pk__in'): """Return queryset with required permissions.""" if isinstance(perms, six.string_types): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError( "Given perms must have same app label " "({} != {})".format(app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError( "ContentType was once computed to be {} and another " "one {}".format(ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = ContentType.objects.get_for_model(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) elif klass is None: raise WrongAppError("Cannot determine content type") else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model and perms_filter == 'pk__in': raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have `codenames` list # First check if user is superuser and if so, return queryset immediately if with_superuser and user.is_superuser: return queryset # Check if the user is anonymous. The # django.contrib.auth.models.AnonymousUser object doesn't work for queries # and it's nice to be able to pass in request.user blindly. if user.is_anonymous(): user = get_anonymous_user() global_perms = set() has_global_perms = False # a superuser has by default assigned global perms for any if accept_global_perms and with_superuser: for code in codenames: if user.has_perm(ctype.app_label + '.' + code): global_perms.add(code) for code in global_perms: codenames.remove(code) # prerequisite: there must be elements in global_perms otherwise just # follow the procedure for object based permissions only AND # 1. codenames is empty, which means that permissions are ONLY set # globally, therefore return the full queryset. # OR # 2. any_perm is True, then the global permission beats the object # based permission anyway, therefore return full queryset if len(global_perms) > 0 and (len(codenames) == 0 or any_perm): return queryset # if we have global perms and still some object based perms differing # from global perms and any_perm is set to false, then we have to flag # that global perms exist in order to merge object based permissions by # user and by group correctly. Scenario: global perm change_xx and # object based perm delete_xx on object A for user, and object based # permission delete_xx on object B for group, to which user is # assigned. # get_objects_for_user(user, [change_xx, delete_xx], use_groups=True, # any_perm=False, accept_global_perms=True) must retrieve object A and # B. elif len(global_perms) > 0 and (len(codenames) > 0): has_global_perms = True # Now we should extract list of pk values for which we would filter # queryset user_model = get_user_obj_perms_model(queryset.model) user_obj_perms_queryset = (user_model.objects .filter(user=user) .filter(permission__content_type=ctype)) if len(codenames): user_obj_perms_queryset = user_obj_perms_queryset.filter( permission__codename__in=codenames) direct_fields = ['content_object__pk', 'permission__codename'] generic_fields = ['object_pk', 'permission__codename'] if user_model.objects.is_generic(): user_fields = generic_fields else: user_fields = direct_fields if use_groups: group_model = get_group_obj_perms_model(queryset.model) group_filters = { 'permission__content_type': ctype, 'group__{}'.format(get_user_model().groups.field.related_query_name()): user, # pylint: disable=no-member } if len(codenames): group_filters.update({ 'permission__codename__in': codenames, }) groups_obj_perms_queryset = group_model.objects.filter(**group_filters) if group_model.objects.is_generic(): group_fields = generic_fields else: group_fields = direct_fields if not any_perm and len(codenames) and not has_global_perms: user_obj_perms = user_obj_perms_queryset.values_list(*user_fields) groups_obj_perms = groups_obj_perms_queryset.values_list(*group_fields) data = list(user_obj_perms) + list(groups_obj_perms) # sorting/grouping by pk (first in result tuple) data = sorted(data, key=lambda t: t[0]) pk_list = [] for pk, group in groupby(data, lambda t: t[0]): obj_codenames = set((e[1] for e in group)) if codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(**{perms_filter: pk_list}) return objects if not any_perm and len(codenames) > 1: counts = user_obj_perms_queryset.values( user_fields[0]).annotate(object_pk_count=Count(user_fields[0])) user_obj_perms_queryset = counts.filter( object_pk_count__gte=len(codenames)) values = user_obj_perms_queryset.values_list(user_fields[0], flat=True) if user_model.objects.is_generic(): values = list(values) query = Q(**{perms_filter: values}) if use_groups: values = groups_obj_perms_queryset.values_list(group_fields[0], flat=True) if group_model.objects.is_generic(): values = list(values) query |= Q(**{perms_filter: values}) return queryset.filter(query)
def get_objects_for_user(user, perms, klass=None, use_groups=True): """ Returns queryset of objects for which given ``user`` has *all* permissions present at ``perms``. :param user: ``User`` instance for which objects would be returned :param perms: sequence with permissions as strings which should be checked. If ``klass`` parameter is not given, those should be full permission names rather than only codenames (i.e. ``auth.change_user``). If more than one permission is present within sequence, theirs content type **must** be the same or ``MixedContentTypeError`` exception would be raised. For convenience, may be given as single permission (string). :param klass: may be a Model, Manager or QuerySet object. If not given this parameter would be computed based on given ``params``. :param use_groups: if ``False``, wouldn't check user's groups object permissions. Default is ``True``. :raises MixedContentTypeError: when computed content type for ``perms`` and/or ``klass`` clashes. :raises WrongAppError: if cannot compute app label for given ``perms``/ ``klass``. Example:: >>> from guardian.shortcuts import get_objects_for_user >>> joe = User.objects.get(username='******') >>> get_objects_for_user(joe, ['auth.change_group']) [] >>> from guardian.shortcuts import assign >>> group = Group.objects.create('some group') >>> assign('auth.change_group', joe, group) >>> get_objects_for_user(joe, ['auth.change_group']) [<Group some group>] """ if isinstance(perms, basestring): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError("Given perms must have same app " "label (%s != %s)" % (app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError("ContentType was once computed " "to be %s and another one %s" % (ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is None: raise WrongAppError("Cannot determine content type") elif ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = ContentType.objects.get_for_model(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model: raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have ``codenames`` list # First check if user is superuser and if so, return queryset immediately if user.is_superuser: return queryset # Now we should extract list of pk values for which we would filter queryset user_obj_perms = UserObjectPermission.objects\ .filter(user=user)\ .filter(permission__content_type=ctype)\ .filter(permission__codename__in=codenames)\ .values_list('object_pk', 'permission__codename') data = list(user_obj_perms) if use_groups: groups_obj_perms = GroupObjectPermission.objects\ .filter(group__user=user)\ .filter(permission__content_type=ctype)\ .filter(permission__codename__in=codenames)\ .values_list('object_pk', 'permission__codename') data += list(groups_obj_perms) keyfunc = lambda t: t[0] # sorting/grouping by pk (first in result tuple) data = sorted(data, key=keyfunc) pk_list = [] for pk, group in groupby(data, keyfunc): obj_codenames = set((e[1] for e in group)) if codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(pk__in=pk_list) return objects
def get_objects_for_organization(organization, perms, klass=None, any_perm=False, accept_global_perms=True): if isinstance(perms, str): perms = [perms] ctype = None app_label = None codenames = set() # Compute codenames set and ctype if possible for perm in perms: if '.' in perm: new_app_label, codename = perm.split('.', 1) if app_label is not None and app_label != new_app_label: raise MixedContentTypeError("Given perms must have same app " "label (%s != %s)" % (app_label, new_app_label)) else: app_label = new_app_label else: codename = perm codenames.add(codename) if app_label is not None: new_ctype = ContentType.objects.get(app_label=app_label, permission__codename=codename) if ctype is not None and ctype != new_ctype: raise MixedContentTypeError("ContentType was once computed " "to be %s and another one %s" % (ctype, new_ctype)) else: ctype = new_ctype # Compute queryset and ctype if still missing if ctype is None and klass is not None: queryset = _get_queryset(klass) ctype = ContentType.objects.get_for_model(queryset.model) elif ctype is not None and klass is None: queryset = _get_queryset(ctype.model_class()) elif klass is None: raise WrongAppError("Cannot determine content type") else: queryset = _get_queryset(klass) if ctype.model_class() != queryset.model: raise MixedContentTypeError("Content type for given perms and " "klass differs") # At this point, we should have both ctype and queryset and they should # match which means: ctype.model_class() == queryset.model # we should also have ``codenames`` list # Now we should extract list of pk values for which we would filter queryset organization_model = get_organization_obj_perms_model(queryset.model) organizations_obj_perms_queryset = (organization_model.objects.filter( organization=organization).filter(permission__content_type=ctype)) if len(codenames): organizations_obj_perms_queryset = organizations_obj_perms_queryset.filter( permission__codename__in=codenames) if organization_model.objects.is_generic(): fields = ['object_pk', 'permission__codename'] else: fields = ['content_object__pk', 'permission__codename'] if not any_perm and len(codenames): organizations_obj_perms = organizations_obj_perms_queryset.values_list( *fields) data = list(organizations_obj_perms) keyfunc = lambda t: t[ 0] # sorting/organizationing by pk (first in result tuple) data = sorted(data, key=keyfunc) pk_list = [] for pk, organization in groupby(data, keyfunc): obj_codenames = set((e[1] for e in organization)) if any_perm or codenames.issubset(obj_codenames): pk_list.append(pk) objects = queryset.filter(pk__in=pk_list) return objects values = organizations_obj_perms_queryset.values_list(fields[0], flat=True) if organization_model.objects.is_generic(): values = list(values) return queryset.filter(pk__in=values)