def has_permission_to (self, action_str, object, id=None, any=False): if isinstance(object, str): raise Exception( 'Argument 2 in user.has_permission_to was a string; The proper syntax is has_permission_to(action, object)!') content_type = get_content_type_for_model(object) object_id = 0 if not isclass(object): object_id = object.id action = Action.get_by_name(action_str) allAction = Action.get_by_name('ALL') #Checks if the group is permitted perms = Permission.objects.filter(content_type=content_type, object_id=object_id, group=self, negative=False, ) for perm in perms: if action in perm.get_valid_actions(): return True if allAction in perm.get_valid_actions(): return True if self.parent: return self.parent.has_permission_to(action, object, id=id, any=any) return False
def who_has_permission_to(self, perm): try: content_type = get_content_type_for_model(self) id = self.id users = [] #object = content_type.get_object_for_this_type(id=id) perm = Action.get_by_name(perm.upper()) adminPerm = Action.get_by_name("ALL") for u in Permission.objects.filter(content_type=content_type, negative=False, object_id=id): if perm in u.get_valid_actions(): if u.user and u.user not in users: users.append(u.user) if u.group: for user in u.group.members.all(): if user and user not in users: users.append(user) if adminPerm in u.get_valid_actions(): if u.user and u.user not in users: users.append(u.user) if u.group: for user in u.group.members.all(): if user and user not in users: users.append(user) return users except Exception, e: return []
def has_permission_to(self, action_str, object, id=None, any=False): if isinstance(object, str): raise Exception( 'Argument 2 in user.has_permission_to was a string; The proper syntax is has_permission_to(action, object)!' ) content_type = get_content_type_for_model(object) object_id = 0 if not isclass(object): object_id = object.id action = Action.get_by_name(action_str) allAction = Action.get_by_name('ALL') #Checks if the group is permitted perms = Permission.objects.filter( content_type=content_type, object_id=object_id, group=self, negative=False, ) for perm in perms: if action in perm.get_valid_actions(): return True if allAction in perm.get_valid_actions(): return True if self.parent: return self.parent.has_permission_to(action, object, id=id, any=any) return False