def uaccessible_targets(name, withid = True, returnlist = False): """Return the list of the targets that the user can access with the ID or without the ID of the target""" # Check for required fields if not name: return utils.response("ERROR: The name is required ", 417) user_data = user.User.query.filter_by(name=name).first() if user_data is None: return utils.response('ERROR: No user with the name "' + name + \ '" in the database.', 417) target_list = user_data.accessible_target_list() formatted_target_list = [] for each_target in target_list: # We show only ssh targets, other types will not be handle here if each_target.show_targettype() == "ssh": data = "" if withid: data = str(each_target.id) + " " data = data + each_target.show_name() + " " + \ each_target.show_hostname() + " " + \ each_target.show_comment() formatted_target_list.append(data) if returnlist: return [target.show_name() for target in target_list if target.show_targettype() == "ssh"] if withid: # We need to be sorted by target ID. Not so easy cause ID are strings formatted_target_list.sort(key=naturalkeys) #naturalkey is a method right above return utils.response("\n".join(formatted_target_list), 200)
def uaccessible_targets(name, withid = True, returnlist = False): """Return the list of the targets that the user can access with the ID or without the ID of the target""" # Check for required fields if not name: return utils.response("ERROR: The name is required ", 417) user_data = user.User.query.filter_by(name=name).first() if user_data is None: return utils.response('ERROR: No user with the name "' + name + \ '" in the database.', 417) target_list = user_data.accessible_target_list() formatted_target_list = [] for each_target in target_list: # We show only ssh targets, other types will not be handle here if each_target.show_targettype() == "ssh": data = "" if withid: data = str(each_target.id) + " " data = data + each_target.show_name() + " " + \ each_target.show_hostname() + " " + \ each_target.show_comment() formatted_target_list.append(data) if returnlist: return [target.show_name() for target in target_list if target.show_targettype() == "ssh"] if withid: # We need to be sorted by target ID. Not so easy cause ID are strings formatted_target_list.sort(key=naturalkeys) #naturalkey is a method right above return utils.response("\n".join(formatted_target_list), 200)
def accessible_target_list(self, style="object"): """Return targets accessible to the users (as object or names list)""" targets = [] output = "Accessible directly: " # 1. list all the directly attached targets for target in self.targets: targets.append(target) output = output + target.show_name() + " ; " output = output + "\nAccessible through usergroups: " # 2. list all the targets accessible through usergroup for usergroup in self.usergroups: output = output + "\n" + usergroup.show_name() + ": " for target in usergroup.accessible_target_list(mode="obj"): output = output + target.show_name() + " ; " if target not in targets: targets.append(target) output = output + "\nAccessible through targetgroups: " # 3. list all the targets accessible through targetgroup for targetgroup in self.targetgroups: output = output + "\n" + targetgroup.show_name() + ": " for target in targetgroup.accessible_target_list(): output = output + target.show_name() + " ; " if target not in targets: targets.append(target) # return target objects or names depending of style if style == "names": targetnames = [] for target in targets: targetnames.append(target.show_name()) targets = sorted(targetnames) elif style == "details": targets = output return targets
def accessible_target_list(self, style="object"): """Return targets accessible to the users (as object or names list)""" targets = [] output = "Accessible directly: " # 1. list all the directly attached targets for target in self.targets: targets.append(target) output = output + target.show_name() + " ; " output = output + "\nAccessible through usergroups: " # 2. list all the targets accessible through usergroup for usergroup in self.usergroups: output = output + "\n" + usergroup.show_name() + ": " for target in usergroup.accessible_target_list(mode="obj"): output = output + target.show_name() + " ; " if target not in targets: targets.append(target) output = output + "\nAccessible through targetgroups: " # 3. list all the targets accessible through targetgroup for targetgroup in self.targetgroups: output = output + "\n" + targetgroup.show_name() + ": " for target in targetgroup.accessible_target_list(): output = output + target.show_name() + " ; " if target not in targets: targets.append(target) # return target objects or names depending of style if style == "names": targetnames = [] for target in targets: targetnames.append(target.show_name()) targets = sorted(targetnames) elif style == "details": targets = output return targets