Example #1
0
    def has_perm(self, user, perm, obj=None):
        # check codename, return false if its a malformed codename
        try:
            perm_type = perm.split('.')[-1].split('_')[0]
            codename = perm.split('.')[1]
        except IndexError:
            return False

        # check group and user permissions, it check the regular users permissions and
        # the custom groups user permissions
        if perm in self.get_all_permissions(user):
            return True

        if not obj:
            return False

        # they are non-admin, should not view any content with status=False - GJQ
        if hasattr(obj, "status") and not obj.status:
            return False

        # object anonymous and use bits
        if perm_type == 'view':
            has_attr_aov = hasattr(obj, "allow_anonymous_view")
            has_attr_auv = hasattr(obj, "allow_user_view")
            has_attr_amv = hasattr(obj, "allow_member_view")
            if all([has_attr_aov, has_attr_auv, has_attr_amv]):
                if obj.allow_anonymous_view:
                    return True
                if user.is_authenticated() and obj.allow_user_view:
                    return True
                if user.profile.is_member and obj.allow_member_view:
                    return True

        if perm_type == 'change':
            has_attr_aue = hasattr(obj, "allow_user_edit")
            has_attr_ame = hasattr(obj, "allow_member_edit")
            if all([has_attr_aue, has_attr_ame]):
                if user.is_authenticated() and obj.allow_user_edit:
                    return True
                if user.profile.is_member and obj.allow_member_edit:
                    return True

        # no anonymous user currently
        if not user.is_authenticated():
            return False

        # check creator and owner
        if hasattr(obj, 'creator'):
            if obj.creator_id == user.id:
                return True
        if hasattr(obj, 'owner'):
            if obj.owner_id == user.id:
                return True

        if not isinstance(obj, Model):
            return False

        # lets check the search index for view permissions
        # before we ever hit the database, faster
        if 'view' in perm:
            try:  # test for an index and make the query
                from haystack import connections
                site = connections['default'].unified_index()

                index = site.get_index(obj.__class__)
                if can_view(user, obj):
                    return True
            except AssertionError:
                raise
            except:
                pass

        # check the permissions on the object level of groups or user
        perm = '%s.%s' % (obj.pk, perm)
        if perm in self.get_all_object_permissions(user, obj):
            return True
Example #2
0
    def has_perm(self, user, perm, obj=None):
        # check codename, return false if its a malformed codename
        try:
            perm_type = perm.split('.')[-1].split('_')[0]
            codename = perm.split('.')[1]
        except IndexError:
            return False

        # check group and user permissions, it check the regular users permissions and
        # the custom groups user permissions
        if perm in self.get_all_permissions(user):
            return True

        if not obj:
            return False

        # they are non-admin, should not view any content with status=False - GJQ
        if hasattr(obj, "status") and not obj.status:
            return False

        # object anonymous and use bits
        if perm_type == 'view':
            has_attr_aov = hasattr(obj, "allow_anonymous_view")
            has_attr_auv = hasattr(obj, "allow_user_view")
            has_attr_amv = hasattr(obj, "allow_member_view")
            if all([has_attr_aov, has_attr_auv, has_attr_amv]):
                if obj.allow_anonymous_view:
                    return True
                if user.is_authenticated() and obj.allow_user_view:
                    return True
                if user.profile.is_member and obj.allow_member_view:
                    return True

        if perm_type == 'change':
            has_attr_aue = hasattr(obj, "allow_user_edit")
            has_attr_ame = hasattr(obj, "allow_member_edit")
            if all([has_attr_aue, has_attr_ame]):
                if user.is_authenticated() and obj.allow_user_edit:
                    return True
                if user.profile.is_member and obj.allow_member_edit:
                    return True

        # no anonymous user currently
        if not user.is_authenticated():
            return False

        # check creator and owner
        if hasattr(obj, 'creator'):
            if obj.creator_id == user.id:
                return True
        if hasattr(obj, 'owner'):
            if obj.owner_id == user.id:
                return True


        if not isinstance(obj, Model):
            return False

        # lets check the search index for view permissions
        # before we ever hit the database, faster
        if 'view' in perm:
            try:  # test for an index and make the query
                from haystack import connections
                site = connections['default'].unified_index()

                index = site.get_index(obj.__class__)
                if can_view(user, obj):
                    return True
            except:
                pass

        # check the permissions on the object level of groups or user
        perm = '%s.%s' % (obj.pk, perm)
        if perm in self.get_all_object_permissions(user, obj):
            return True