def getGuiDialogs(self, objectType): factory = ObjectFactory.getInstance() if objectType not in factory.getObjectTypes(): raise GOsaException( C.make_error("OBJECT_UNKNOWN_TYPE", type=objectType)) return factory.getObjectDialogs(objectType)
def searchForObjectDetails(self, user, extension, attribute, search_filter, attributes, skip_values, options=None): """ Search selectable items valid for the attribute "extension.attribute". This is used to add new groups to the users groupMembership attribute. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. object_factory = ObjectFactory.getInstance() be_data = object_factory.getObjectBackendParameters( extension, attribute) if not be_data: raise GOsaException( C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information object_type, object_attribute, _, _ = be_data[attribute] return self.searchObjects(user, object_type, object_attribute, search_filter, attributes, skip_values, options)
def getContainerTree(self, user, base, object_type=None): table = inspect(ObjectInfoIndex) o2 = aliased(ObjectInfoIndex) base = ObjectProxy.get_adjusted_dn(base, self.env.base) query = and_(getattr(ObjectInfoIndex, "_adjusted_parent_dn") == base, getattr(ObjectInfoIndex, "_type").in_(self.containers)) count = func.count(getattr(o2, "_parent_dn")) parent_join_condition = getattr(o2, "_parent_dn") == getattr(ObjectInfoIndex, "dn") with make_session() as session: query_result = session.query(ObjectInfoIndex, count) \ .outerjoin(o2, and_(getattr(o2, "_invisible").is_(False), parent_join_condition)) \ .filter(query) \ .group_by(*table.c) res = {} factory = ObjectFactory.getInstance() for item, children in query_result: self.update_res(res, item, user, 1) if item.dn in res: res[item.dn]['hasChildren'] = children > 0 res[item.dn]['adjusted_dn'] = ObjectProxy.get_adjusted_dn(item.dn, self.env.base) if object_type is not None: # check if object_type is allowed in this container allowed = factory.getAllowedSubElementsForObject(res[item.dn]['tag'], includeInvisible=False) if "*" in object_type: # all allowed res[item.dn]['allowed_move_target'] = True elif isinstance(object_type, list): res[item.dn]['allowed_move_target'] = len(set(object_type).intersection(allowed)) > 0 else: res[item.dn]['allowed_move_target'] = object_type in allowed return res
def getTemplateI18N(self, language): templates = [] factory = ObjectFactory.getInstance() for otype in factory.getObjectTypes(): templates += factory.getObjectTemplateNames(otype) return factory.getNamedI18N(list(set(templates)), language=language)
def getObjectDetails(self, extension, attribute, names, attributes): """ This method is used to complete object information shown in the gui. e.g. The groupMembership table just knows the groups cn attribute. To be able to show the description too, it uses this method. #TODO: @fabian - this function is about 95% the same than the one # above. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. of = ObjectFactory.getInstance() be_data = of.getObjectBackendParameters(extension, attribute) if not be_data: raise GOsaException(C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information otype, oattr, foreignMatchAttr, matchAttr = be_data[attribute] #@UnusedVariable # Create a list of attributes that will be requested if oattr not in attributes: attributes.append(oattr) attrs = dict([(x, 1) for x in attributes]) # Start the query and bring the result in a usable form index = PluginRegistry.getInstance("ObjectIndex") query = {oattr: {"in_": names}} if otype != "*": if of.isBaseType(otype): query["_type"] = otype else: query["extension"] = otype res = index.search(query, attrs) result = {} mapping = {} for entry in names: _id = len(result) mapping[entry] = _id result[_id] = None for entry in res: item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[attr] = entry[attr] if attr in ['dn', '_type'] else entry[attr][0] else: item[attr] = "" if item[oattr] in mapping: _id = mapping[item[oattr]] result[_id] = item return {"result": result, "map": mapping}
def __init__(self): self.env = Environment.getInstance() # Load container mapping from object Factory factory = ObjectFactory.getInstance() self.containers = [] for ot, info in factory.getObjectTypes().items(): if 'container' in info: self.containers.append(ot)
def __init__(self): self.env = Environment.getInstance() self.log = logging.getLogger(__name__) # Load container mapping from object Factory factory = ObjectFactory.getInstance() self.containers = [] for ot, info in factory.getObjectTypes().items(): if 'container' in info: self.containers.append(ot) self.__fuzzy_similarity_threshold = self.env.config.getfloat("backend.fuzzy-threshold", default=0.3)
def __init__(self): self.env = Environment.getInstance() self.log = logging.getLogger(__name__) # Load container mapping from object Factory factory = ObjectFactory.getInstance() self.containers = [] for ot, info in factory.getObjectTypes().items(): if 'container' in info: self.containers.append(ot) self.__fuzzy_similarity_threshold = self.env.config.getfloat( "backend.fuzzy-threshold", default=0.3)
def isContainerForObjectType(self, user, container_dn, object_type): with make_session() as session: container_type_query = session.query(getattr(ObjectInfoIndex, "_type")).filter( getattr(ObjectInfoIndex, "dn") == container_dn).one() container_type = container_type_query[0] allowed = ObjectFactory.getInstance().getAllowedSubElementsForObject(container_type) if object_type is None: return True elif "*" in object_type: # all allowed return True elif isinstance(object_type, list): return len(set(object_type).intersection(allowed)) > 0 else: return object_type in allowed
def searchForObjectDetails(self, user, extension, attribute, search_filter, attributes, skip_values, options=None): """ Search selectable items valid for the attribute "extension.attribute". This is used to add new groups to the users groupMembership attribute. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. object_factory = ObjectFactory.getInstance() be_data = object_factory.getObjectBackendParameters(extension, attribute) if not be_data: raise GOsaException(C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information object_type, object_attribute, _, _ = be_data[attribute] return self.searchObjects(user, object_type, object_attribute, search_filter, attributes, skip_values, options)
def isContainerForObjectType(self, user, container_dn, object_type): with make_session() as session: container_type_query = session.query( getattr(ObjectInfoIndex, "_type")).filter( getattr(ObjectInfoIndex, "dn") == container_dn).one() container_type = container_type_query[0] allowed = ObjectFactory.getInstance( ).getAllowedSubElementsForObject(container_type) if object_type is None: return True elif "*" in object_type: # all allowed return True elif isinstance(object_type, list): return len(set(object_type).intersection(allowed)) > 0 else: return object_type in allowed
def getAllowedSubElementsForObjectWithActions(self, user, base=None, locale=None): factory = ObjectFactory.getInstance() res = {} languages = [locale] if len(locale.split('-')) == 2: languages.append(locale.split('-')[0]) t = gettext.translation('messages', resource_filename("gosa.backend", "locale"), fallback=True, languages=languages) for obj_type, actions in factory.getAllowedSubElementsForObjectWithActions(user, base).items(): xml = factory.getXMLSchema(obj_type) if xml is not None: res[obj_type] = { "actions": actions, "displayName": t.gettext(xml.DisplayName.text) } return res
def getContainerTree(self, user, base, object_type=None): types = [] table = inspect(ObjectInfoIndex) o2 = aliased(ObjectInfoIndex) for container in self.containers: types.append(getattr(ObjectInfoIndex, "_type") == container) query = and_( getattr(ObjectInfoIndex, "_adjusted_parent_dn") == base, or_(*types)) with make_session() as session: query_result = session.query(ObjectInfoIndex, func.count(getattr(o2, "_adjusted_parent_dn"))) \ .outerjoin(o2, and_(getattr(o2, "_invisible").is_(False), getattr(o2, "_adjusted_parent_dn") == getattr(ObjectInfoIndex, "dn"))) \ .filter(query) \ .group_by(*table.c) res = {} factory = ObjectFactory.getInstance() for item, children in query_result: self.update_res(res, item, user, 1) if object_type is not None and item.dn in res: res[item.dn]['hasChildren'] = children > 0 # check if object_type is allowed in this container allowed = factory.getAllowedSubElementsForObject( res[item.dn]['tag'], includeInvisible=False) if "*" in object_type: # all allowed res[item.dn]['allowed_move_target'] = True elif isinstance(object_type, list): res[item.dn]['allowed_move_target'] = len( set(object_type).intersection(allowed)) > 0 else: res[item. dn]['allowed_move_target'] = object_type in allowed return res
def getAvailableObjectNames(self, only_base_objects=False, base=None, locale=None): factory = ObjectFactory.getInstance() if base is not None: res = factory.getAllowedSubElementsForObject(base) else: res = factory.getAvailableObjectNames(only_base_objects, base) if locale is None: return res else: languages = [locale] if len(locale.split('-')) == 2: languages.append(locale.split('-')[0]) t = gettext.translation('messages', resource_filename("gosa.backend", "locale"), fallback=True, languages=languages) translated = {} for name in res: xml = factory.getXMLSchema(name) if xml is not None: translated[name] = t.gettext(xml.DisplayName.text) else: self.log.info('no xml found for %s' % name) return translated
def getObjectDetails(self, extension, attribute, names, attributes): """ This method is used to complete object information shown in the gui. e.g. The groupMembership table just knows the groups cn attribute. To be able to show the description too, it uses this method. #TODO: @fabian - this function is about 95% the same than the one # above. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. of = ObjectFactory.getInstance() be_data = of.getObjectBackendParameters(extension, attribute) if not be_data: raise GOsaException( C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information otype, oattr, foreignMatchAttr, matchAttr = be_data[ attribute] #@UnusedVariable # Create a list of attributes that will be requested if oattr not in attributes: attributes.append(oattr) attrs = dict([(x, 1) for x in attributes]) # Start the query and bring the result in a usable form index = PluginRegistry.getInstance("ObjectIndex") res = index.search( {'or_': { '_type': otype, '_extensions': otype, oattr: names }}, attrs) result = {} mapping = {} for entry in names: _id = len(result) mapping[entry] = _id result[_id] = None for entry in res: item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[ attr] = entry[attr] if attr == 'dn' else entry[attr][0] else: item[attr] = "" if item[oattr] in mapping: _id = mapping[item[oattr]] result[_id] = item return {"result": result, "map": mapping}
def searchForObjectDetails(self, user, extension, attribute, search_filter, attributes, skip_values): """ Search selectable items valid for the attribute "extension.attribute". This is used to add new groups to the users groupMembership attribute. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. object_factory = ObjectFactory.getInstance() be_data = object_factory.getObjectBackendParameters(extension, attribute) if not be_data: raise GOsaException(C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information object_type, object_attribute, _, _ = be_data[attribute] # Create a list of attributes that will be requested if object_attribute not in attributes: attributes.append(object_attribute) attrs = dict([(x, 1) for x in attributes]) if not "dn" in attrs: attrs.update({'dn': 1}) # Start the query and format the result index = PluginRegistry.getInstance("ObjectIndex") search_result = index.search({ 'or_': { '_type': object_type, 'extension': object_type }, object_attribute: '%{}%'.format(search_filter) if len(search_filter) > 0 else '%' }, attrs) if not search_result: search_result = index.search({ '_type': object_type, object_attribute: '%{}%'.format(search_filter) if len(search_filter) > 0 else '%' }, attrs) result = [] # Do we have read permissions for the requested attribute env = Environment.getInstance() topic = "%s.objects.%s" % (env.domain, object_type) acl_resolver = PluginRegistry.getInstance("ACLResolver") for entry in search_result: if not acl_resolver.check(user, topic, "s", base=entry['dn']): continue item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[attr] = entry[attr] if attr == "dn" else entry[attr][0] else: item[attr] = "" item['__identifier__'] = item[object_attribute] # Skip values that are in the skip list if skip_values and item['__identifier__'] in skip_values: continue result.append(item) return result
def process(self, obj, key, valDict, value): types = ObjectFactory.getInstance().getAttributeTypes() valDict[key]['value'] = types['String'].convert_to(valDict[key]['type'], [value]) return key, valDict
def getAllowedSubElementsForObjectWithActions(self, user, base=None): factory = ObjectFactory.getInstance() return factory.getAllowedSubElementsForObjectWithActions(user, base)
def getAvailableObjectNames(self, only_base_objects=False, base=None): factory = ObjectFactory.getInstance() if base is not None: return factory.getAllowedSubElementsForObject(base) else: return factory.getAvailableObjectNames(only_base_objects, base)
def searchObjects(self, user, object_type, object_attribute, search_filter, attributes, skip_values, options=None): # Create a list of attributes that will be requested if object_attribute is not None and object_attribute not in attributes: attributes.append(object_attribute) attrs = dict([(x, 1) for x in attributes]) if "dn" not in attrs: attrs.update({'dn': 1}) # Start the query and format the result index = PluginRegistry.getInstance("ObjectIndex") query = {} object_factory = ObjectFactory.getInstance() if len(search_filter) > 0: if object_attribute is None or object_attribute == '*' or \ (options is not None and options.get('fullText', False) is True): query["*"] = search_filter else: query[object_attribute] = '%{}%'.format(search_filter) elif object_attribute is not None: query[object_attribute] = '%' types = [] extensions = [] if object_type != "*": if object_factory.isBaseType(object_type): types.append(object_type) else: extensions.append(object_type) if options is not None and 'filter' in options: for key, values in options['filter'].items(): if key == "_type" or key == "types" or key == "extension": if isinstance(values, list): for type in values: if object_factory.isBaseType(type): types.append(type) else: extensions.append(type) elif key not in query: query[key] = values else: if isinstance(query[key], list): query[key].append(values) else: query[key] = [query[key]] query[key].append(values) del options['filter'] if len(types): query["_type"] = {"in_": types} if len(extensions): query["extension"] = {"in_": extensions} search_result = index.search(query, attrs, options) result = [] # Do we have read permissions for the requested attribute env = Environment.getInstance() topic = "%s.objects.%s" % (env.domain, object_type) acl_resolver = PluginRegistry.getInstance("ACLResolver") for entry in search_result: if not acl_resolver.check(user, topic, "s", base=entry['dn']): continue item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[attr] = entry[attr] if attr in ["dn", "_type"] else entry[attr][0] else: item[attr] = "" item['__identifier__'] = item[object_attribute] # Skip values that are in the skip list if skip_values and item['__identifier__'] in skip_values: continue result.append(item) return result
def searchForObjectDetails(self, user, extension, attribute, search_filter, attributes, skip_values): """ Search selectable items valid for the attribute "extension.attribute". This is used to add new groups to the users groupMembership attribute. """ # Extract the the required information about the object # relation out of the BackendParameters for the given extension. object_factory = ObjectFactory.getInstance() be_data = object_factory.getObjectBackendParameters( extension, attribute) if not be_data: raise GOsaException( C.make_error("BACKEND_PARAMETER_MISSING", extension=extension, attribute=attribute)) # Collection basic information object_type, object_attribute, _, _ = be_data[attribute] # Create a list of attributes that will be requested if object_attribute not in attributes: attributes.append(object_attribute) attrs = dict([(x, 1) for x in attributes]) if not "dn" in attrs: attrs.update({'dn': 1}) # Start the query and format the result index = PluginRegistry.getInstance("ObjectIndex") search_result = index.search( { 'or_': { '_type': object_type, 'extension': object_type }, object_attribute: '%{}%'.format(search_filter) if len(search_filter) > 0 else '%' }, attrs) if not search_result: search_result = index.search( { '_type': object_type, object_attribute: '%{}%'.format(search_filter) if len(search_filter) > 0 else '%' }, attrs) result = [] # Do we have read permissions for the requested attribute env = Environment.getInstance() topic = "%s.objects.%s" % (env.domain, object_type) acl_resolver = PluginRegistry.getInstance("ACLResolver") for entry in search_result: if not acl_resolver.check(user, topic, "s", base=entry['dn']): continue item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[ attr] = entry[attr] if attr == "dn" else entry[attr][0] else: item[attr] = "" item['__identifier__'] = item[object_attribute] # Skip values that are in the skip list if skip_values and item['__identifier__'] in skip_values: continue result.append(item) return result
def getGuiDialogs(self, objectType): factory = ObjectFactory.getInstance() if objectType not in factory.getObjectTypes(): raise GOsaException(C.make_error("OBJECT_UNKNOWN_TYPE", type=objectType)) return factory.getObjectDialogs(objectType)
def searchObjects(self, user, object_type, object_attribute, search_filter, attributes, skip_values, options=None): # Create a list of attributes that will be requested if object_attribute is not None and object_attribute not in attributes: attributes.append(object_attribute) attrs = dict([(x, 1) for x in attributes]) if "dn" not in attrs: attrs.update({'dn': 1}) # Start the query and format the result index = PluginRegistry.getInstance("ObjectIndex") query = {} object_factory = ObjectFactory.getInstance() if len(search_filter) > 0: query["*"] = search_filter elif object_attribute is not None: query[object_attribute] = '%' types = [] extensions = [] if object_type != "*": if object_factory.isBaseType(object_type): types.append(object_type) else: extensions.append(object_type) if options is not None and 'filter' in options: for key, values in options['filter'].items(): if key == "_type" or key == "types" or key == "extension": if isinstance(values, list): for type in values: if object_factory.isBaseType(type): types.append(type) else: extensions.append(type) elif key not in query: query[key] = values else: if isinstance(query[key], list): query[key].append(values) else: query[key] = [query[key]] query[key].append(values) del options['filter'] if len(types): query["_type"] = {"in_": types} if len(extensions): query["extension"] = {"in_": extensions} search_result = index.search(query, attrs, options) result = [] # Do we have read permissions for the requested attribute env = Environment.getInstance() topic = "%s.objects.%s" % (env.domain, object_type) acl_resolver = PluginRegistry.getInstance("ACLResolver") for entry in search_result: if not acl_resolver.check(user, topic, "s", base=entry['dn']): continue item = {} for attr in attributes: if attr in entry and len(entry[attr]): item[attr] = entry[attr] if attr in ["dn", "_type" ] else entry[attr][0] else: item[attr] = "" item['__identifier__'] = item[object_attribute] # Skip values that are in the skip list if skip_values and item['__identifier__'] in skip_values: continue result.append(item) return result
def process(self, obj, key, valDict, value): types = ObjectFactory.getInstance().getAttributeTypes() valDict[key]['value'] = types['String'].convert_to( valDict[key]['type'], [value]) return key, valDict