~linaro-validation/django-restricted-resource/trunk

« back to all changes in this revision

Viewing changes to django_restricted_resource/models.py

  • Committer: Zygmunt Krynicki
  • Date: 2011-09-09 17:35:52 UTC
  • Revision ID: zygmunt.krynicki@linaro.org-20110909173552-xere6gdoto5bgec1
Fix RestrictedResource.is_owned_by for group owner and member accessing user

Previusly a case where the accessing user was a member of an owning group was
not handled. Now a resource.is_owned_by(user) returns true when user is a
member of resource.group.

Show diffs side-by-side

added added

removed removed

Lines of Context:
117
117
        """
118
118
        if principal is None:
119
119
            return False
120
 
        if not isinstance(principal,
121
 
                          (User, Group, AnonymousUser, type(None))):
 
120
        # If the principal is an User then this object is owned by that user or
 
121
        # the group the user belongs to.
 
122
        if isinstance(principal, (User, AnonymousUser, type(None))):
 
123
            user = filter_bogus_users(principal)
 
124
            if user is None:
 
125
                return False
 
126
            if self.user is not None:
 
127
                return self.user == user
 
128
            else:
 
129
                return self.group in user.groups.all()
 
130
        # If the principal is a Group then this object is owned by that group
 
131
        elif isinstance(principal, Group):
 
132
            group = principal
 
133
            return self.group == group
 
134
        else:
122
135
            raise TypeError("Expected User or Group instance as argument")
123
 
        return self.user == principal or self.group == principal
124
136
 
125
137
    def get_access_type(self, principal):
126
138
        """