def __init__(self, definition, values=None, source=None, expiration_date=None): """ definition can be given by its name, an alias or an oid """ self.definition = None if definition in ATTRIBUTE_MAPPING: self.definition = definition else: d = get_def_name_from_oid(definition) if d: self.definition = d else: self.definition = get_def_name_from_alias(definition) if not self.definition: raise Exception("Definition not found.") self.values = list() if values: for value in values: if convert_from_string(self.definition, value): self.values.append(value.encode("utf-8")) if isinstance(source, AttributeSource): self.source_id = source.id else: self.source_id = -1 """ISO8601""" try: iso8601_to_datetime(expiration_date) self.expiration_date = expiration_date except: self.expiration_date = None
def load_listed_attributes_with_source(self, definitions, source): if not source: return if self.user: defs = [] for d in definitions: if d in ATTRIBUTE_MAPPING: defs.append(d) else: df = get_def_name_from_oid(d) if df: defs.append(df) else: df = get_def_name_from_alias(d) if df: defs.append(df) if defs: logger.info('load_listed_attributes: \ attributes required are %s from %s' % (defs, source)) attributes_provided = \ listed_attributes_with_source_call.send(sender=None, user=self.user, definitions=defs, source=source) for attrs in attributes_provided: logger.info('load_listed_attributes: \ attributes_call connected to function %s' % \ attrs[0].__name__) logger.info('load_listed_attributes: \ attributes provided are %s' % str(attrs[1])) self.load_by_dic(attrs[1]) else: logger.info('load_listed_attributes: no definitions \ of attributes to load with %s' % str(definitions))
def load_listed_attributes(self, definitions): ''' definitions can be given by its name, an alias or an oid ''' if self.user: defs = [] for d in definitions: if d in ATTRIBUTE_MAPPING: defs.append(d) else: df = get_def_name_from_oid(d) if df: defs.append(df) else: df = get_def_name_from_alias(d) if df: defs.append(df) if defs: logger.info('load_listed_attributes: \ attributes required are %s' % defs) attributes_provided = listed_attributes_call.send( sender=None, user=self.user, definitions=defs) for attrs in attributes_provided: logger.info('load_listed_attributes: \ attributes_call connected to function %s' % \ attrs[0].__name__) logger.info('load_listed_attributes: \ attributes provided are %s' % str(attrs[1])) self.load_by_dic(attrs[1]) else: logger.info('load_listed_attributes: no definitions \ of attributes to load with %s' % str(definitions))
def __init__(self, definition, values=None, source=None, expiration_date=None): ''' definition can be given by its name, an alias or an oid ''' self.definition = None if definition in ATTRIBUTE_MAPPING: self.definition = definition else: d = get_def_name_from_oid(definition) if d: self.definition = d else: self.definition = get_def_name_from_alias(definition) if not self.definition: raise Exception('Definition not found.') self.values = list() if values: for value in values: if convert_from_string(self.definition, value): self.values.append(value.encode('utf-8')) if isinstance(source, AttributeSource): self.source_id = source.id else: self.source_id = -1 '''ISO8601''' try: iso8601_to_datetime(expiration_date) self.expiration_date = expiration_date except: self.expiration_date = None
def load_listed_attributes_with_source(self, definitions, source, auth_source=False): if not source: return if self.user: defs = [] for d in definitions: if d in ATTRIBUTE_MAPPING: defs.append(d) else: df = get_def_name_from_oid(d) if df: defs.append(df) else: df = get_def_name_from_alias(d) if df: defs.append(df) if defs: logger.info( "load_listed_attributes_with_source: \ attributes required are %s from %s" % (defs, source) ) attributes_provided = listed_attributes_with_source_call.send( sender=None, user=self.user, definitions=defs, source=source, auth_source=auth_source ) for attrs in attributes_provided: logger.info( "load_listed_attributes_with_source: \ attributes_call connected to function %s" % attrs[0].__name__ ) if attrs[1]: logger.info( "load_listed_attributes_with_source: \ attributes provided are %s" % str(attrs[1]) ) self.load_by_dic(attrs[1]) else: logger.info( "load_listed_attributes_with_source: \ no attributes provided" ) else: logger.info( "load_listed_attributes: no definitions \ of attributes to load with %s" % str(definitions) )
def load_listed_attributes(self, definitions): """ definitions can be given by its name, an alias or an oid """ if self.user: defs = [] for d in definitions: if d in ATTRIBUTE_MAPPING: defs.append(d) else: df = get_def_name_from_oid(d) if df: defs.append(df) else: df = get_def_name_from_alias(d) if df: defs.append(df) if defs: logger.info( "load_listed_attributes: \ attributes required are %s" % defs ) attributes_provided = listed_attributes_call.send(sender=None, user=self.user, definitions=defs) for attrs in attributes_provided: logger.info( "load_listed_attributes: \ attributes_call connected to function %s" % attrs[0].__name__ ) if attrs[1]: logger.info( "load_listed_attributes: \ attributes provided are %s" % str(attrs[1]) ) self.load_by_dic(attrs[1]) else: logger.info( "load_listed_attributes: \ no attributes provided" ) else: logger.info( "load_listed_attributes: no definitions \ of attributes to load with %s" % str(definitions) )
def get_attributes(user, definitions=None, source=None, auth_source=False, **kwargs): """ Return attributes dictionnary Dictionnary format: attributes = dict() data_from_source = list() a1 = dict() a1['oid'] = definition_name Or a1['definition'] = definition_name definition may be the definition name like 'gn' or an alias like 'givenName' Or a1['name'] = attribute_name_in_ns a1['namespace'] = ns_name a1['values'] = list_of_values data_from_source.append(a1) ... data_from_source.append(a2) attributes[source_name] = data_from_source First attempt on 'definition' key. Else, definition is searched by 'name' and 'namespece' keys. """ from models import AttributeSource try: AttributeSource.objects.get(name=SOURCE_NAME) except: logger.debug("USER_PROFILE source is inactive") return None if source and source.name != SOURCE_NAME: logger.debug("USER_PROFILE source ignored as source %r is required", source.name) return None attributes = dict() data = [] field_names = set() user_profile_fields = getattr(user, "USER_PROFILE", []) if not user_profile_fields: user_profile_fields = user._meta.get_all_field_names() for field in user_profile_fields: if isinstance(field, (tuple, list)): field_names.add(field[0]) else: field_names.add(field) fields = [] if definitions: for definition in definitions: field_name = get_profile_field_name_from_definition(definition) if not field_name: # # Profile model may be extended without modifying the # mapping file if the attribute name is the same as the # definition # field_name = definition fields.append((field_name, definition)) else: fields = [ (field_name, definition) for definition in get_definitions_from_profile_field_name(field_name) for field_name in field_names ] logger.debug("retrieving fields %r from USER_PROFILE", fields) data = {} for field_name, definition in fields: try: value = attrgetter(field_name)(user) except AttributeError: logger.debug("field %r not found in USER_PROFILE", field_name) continue if value: if callable(value): value = value() logger.debug("field %r has value %r", field_name, value) old = data.get(definition, []) if not isinstance(value, basestring) and hasattr(value, "__iter__"): new = map(unicode, value) else: new = [unicode(value)] data[definition] = list(set(old) | set(new)) else: logger.debug("get_attributes: no value found") if hasattr(user, "get_attributes") and callable(user.get_attributes): attributes = user.get_attributes() if not definitions: definitions = [] for key in attributes: if is_definition(key): definitions.add(key) else: definition = get_def_name_from_alias(key) if definition: definitions.add(definition) for definition in definitions: new = [] for key in get_aliases(definition): if key in attributes: new.append(set(attributes[key])) if not new: continue new = reduce(__or__, new) old = data.get(definition, []) data[definition] = list(set(old) | set(new)) data = [{"definition": definition, "values": values} for definition, values in data.items()] return {SOURCE_NAME: data}
def load_by_dic(self, dictionnary): """ Dictionnary format: attributes = dict() data_from_source = list() a1 = dict() a1['oid'] = definition_name Or a1['definition'] = definition_name definition may be the definition name like 'gn' or an alias like 'givenName' Or a1['name'] = attribute_name_in_ns a1['namespace'] = ns_name a1['values'] = list_of_values data_from_source.append(a1) ... data_from_source.append(a2) attributes[source_name] = data_from_source First attempt on 'definition' key. Else, definition is searched by 'name' and 'namespece' keys. """ if not dictionnary: logger.info("load_by_dic: empty dictionnary provided") return -1 for source_name in dictionnary: logger.debug("load_by_dic: loading from source with name: %s" % source_name) source = get_source_from_name(source_name) if source: logger.debug("load_by_dic: attributes: %s" % str(dictionnary[source_name])) for attribute in dictionnary[source_name]: if ( not "oid" in attribute and not "definition" in attribute and not ("name" in attribute and "namespace" in attribute) ) or not "values" in attribute: logger.warn( "load_by_dic: \ missing data to treat %s" % str(attribute) ) else: definition = None if "oid" in attribute: definition = get_def_name_from_oid(attribute["oid"]) elif "definition" in attribute: if attribute["definition"] in ATTRIBUTE_MAPPING: definition = attribute["definition"] else: definition = get_def_name_from_alias(attribute["definition"]) else: definition = get_def_name_from_name_and_ns_of_attribute( attribute["name"], attribute["namespace"] ) if not definition: logger.warn( "load_by_dic: \ unable to find definition for %s" % str(attribute) ) else: logger.debug( "load_by_dic: \ definition %s found" % definition ) expiration_date = None if "expiration_date" in attribute: logger.debug("load_by_dic: expire at %s" % attribute["expiration_date"]) try: iso8601_to_datetime(attribute["expiration_date"]) expiration_date = attribute["expiration_date"] logger.debug( "load_by_dic: expiration \ date has the ISO8601 format" ) except: logger.warn( "load_by_dic: expiration \ date has not the ISO8601 format" ) if not expiration_date: expiration_date = datetime.datetime.now().isoformat() values = [value for value in attribute["values"] if convert_from_string(definition, value)] if ( self.add_data( AttributeData( definition, values=values, source=source, expiration_date=expiration_date ) ) == 0 ): logger.debug( "load_by_dic: \ attribute successfully added" ) else: logger.warn( "load_by_dic: \ error addind attribute" ) else: logger.critical( "load_by_dic: \ The source with name %s providing attributes %s \ is unknown of the system" % (str(source_name), str(dictionnary[source_name])) ) return 0
def load_by_dic(self, dictionnary): ''' Dictionnary format: attributes = dict() data_from_source = list() a1 = dict() a1['oid'] = definition_name Or a1['definition'] = definition_name definition may be the definition name like 'gn' or an alias like 'givenName' Or a1['name'] = attribute_name_in_ns a1['namespace'] = ns_name a1['values'] = list_of_values data_from_source.append(a1) ... data_from_source.append(a2) attributes[source_name] = data_from_source First attempt on 'definition' key. Else, definition is searched by 'name' and 'namespece' keys. ''' if not dictionnary: logger.error('load_by_dic: \ Missing profile or dictionnary') return -1 for source_name in dictionnary: logger.debug('load_by_dic: loading from source with name: %s' \ % source_name) source = get_source_from_name(source_name) if source: logger.debug('load_by_dic: attributes: %s' \ % str(dictionnary[source_name])) for attribute in dictionnary[source_name]: if (not 'oid' in attribute \ and not 'definition' in attribute \ and not('name' in attribute \ and 'namespace' in attribute)) \ or not 'values' in attribute: logger.warn('load_by_dic: \ missing data to treat %s' % str(attribute)) else: definition = None if 'oid' in attribute: definition = \ get_def_name_from_oid(attribute['oid']) elif 'definition' in attribute: if attribute['definition'] in ATTRIBUTE_MAPPING: definition = attribute['definition'] else: definition = \ get_def_name_from_alias(attribute['definition']) else: definition = \ get_def_name_from_name_and_ns_of_attribute(\ attribute['name'], attribute['namespace']) if not definition: logger.warn('load_by_dic: \ unable to find definition for %s' \ % str(attribute)) else: logger.debug('load_by_dic: \ definition %s found' % definition) expiration_date = None if 'expiration_date' in attribute: logger.debug('load_by_dic: expire at %s' \ % attribute['expiration_date']) try: iso8601_to_datetime(\ attribute['expiration_date']) expiration_date = \ attribute['expiration_date'] logger.debug('load_by_dic: expiration \ date has the ISO8601 format') except: logger.warn('load_by_dic: expiration \ date has not the ISO8601 format') if not expiration_date: expiration_date = \ datetime.datetime.now().isoformat() values = [value for value in attribute['values'] \ if convert_from_string(definition, value)] if self.add_data(AttributeData(\ definition, values=values, source=source, expiration_date=expiration_date)) == 0: logger.debug('load_by_dic: \ attribute successfully added') else: logger.warn('load_by_dic: \ error addind attribute') else: logger.critical('load_by_dic: \ The source with name %s providing attributes %s \ is unknown of the system' \ % (str(source_name), str(dictionnary[source_name]))) return 0