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
def grant_permissions (self, actions, object, **kwargs): from_date = None to_date = None negative = False #Set time limits, if set in func-call if 'from_date' in kwargs: from_date = kwargs['from_date'] if 'to_date' in kwargs: to_date = kwargs['to_date'] #Set negative to negative value in kwargs if 'negative' in kwargs: negative = True #Make it possible to set permissions for classes object_id = 0 if not isclass(object): object_id = object.id #Get info about the object content_type = get_content_type_for_model(object) perm = Permission( group=self, content_type=content_type, object_id=object_id, from_date=from_date, to_date=to_date, negative=negative, ) perm.save() for p in Action.get_list_by_names(actions): perm.actions.add(p) perm.save() self.invalidate_permission_tree_for_members()
def grant_permissions(self, actions, object, **kwargs): from_date = None to_date = None negative = False #Set time limits, if set in func-call if 'from_date' in kwargs: from_date = kwargs['from_date'] if 'to_date' in kwargs: to_date = kwargs['to_date'] #Set negative to negative value in kwargs if 'negative' in kwargs: negative = True #Make it possible to set permissions for classes object_id = 0 if not isclass(object): object_id = object.id #Get info about the object content_type = get_content_type_for_model(object) perm = Permission( group=self, content_type=content_type, object_id=object_id, from_date=from_date, to_date=to_date, negative=negative, ) perm.save() for p in Action.get_list_by_names(actions): perm.actions.add(p) perm.save() self.invalidate_permission_tree_for_members()