def import_users_from_json(self, users): super(Office365ImportAPI, self).import_users_from_json(users) for user in users: # for key, value in user.items(): # log.debug("Office365ImportAPI::import_users_from_json -Key :: " + str(key) + " value :: " + str(value)) # check if we've already seen this person - assumes office365_id is unique if PersonIdentifier.objects.filter( identifier=user['id'], identifier_type=Identifier.OFFICE_ID).exists(): person = PersonIdentifier.objects.get( identifier=user['id'], identifier_type=Identifier.OFFICE_ID).belongs_to else: # create a person person, p_created = Person.objects.update_or_create( first_name=user['name']['givenName'], surname=user['name']['familyName'], office365_identity_data=json.dumps(user)) fullname = user['name']['fullName'] add_identifier(self.PERSON_MODEL, person, Identifier.NAME, fullname) if user.get('id'): add_identifier(self.PERSON_MODEL, person, Identifier.OFFICE_ID, user['id']) # customer id is the id for the company - weird edge case so we need to handle this here office365_group, id_created = Group.objects.update_or_create( name=user['customerId'], group_type=Group.OFFICE, description="Office365 Customer Group - Organisation", office365_group_data='') person.groups.add(office365_group) if user.get('emails'): email_addresses = user['emails'] for email_item in email_addresses: is_primary = 'primary' in email_item add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, email_item['address'], is_primary) if user.get('aliases'): aliases = user['aliases'] for alias in aliases: add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, alias) if user.get('nonEditableAliases'): aliases = user['nonEditableAliases'] for alias in aliases: add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, alias) # Invite user to system task_run_intro_email.apply_async((person.id, ), countdown=1)
def import_users_from_json(self, users): super(GoogleImportAPI, self).import_users_from_json(users) for user in users: # for key, value in user.items(): # log.debug("GoogleImportAPI::import_users_from_json -Key :: " + str(key) + " value :: " + str(value)) # check if we've already seen this person - assumes google_id is unique if PersonIdentifier.objects.filter(identifier=user['id'], identifier_type=Identifier.GOOGLE_ID).exists(): person = PersonIdentifier.objects.get(identifier=user['id'], identifier_type=Identifier.GOOGLE_ID).belongs_to else: # create a person person, p_created = Person.objects.update_or_create(first_name=user['name']['givenName'], surname=user['name']['familyName'], google_identity_data=json.dumps(user)) fullname = user['name']['fullName'] add_identifier(self.PERSON_MODEL, person, Identifier.NAME, fullname) if user.get('id'): add_identifier(self.PERSON_MODEL, person, Identifier.GOOGLE_ID, user['id']) # customer id is the id for the company - weird edge case so we need to handle this here google_group, id_created = Group.objects.update_or_create(name=user['customerId'], group_type=Group.GOOGLE, description="Google Customer Group - Organisation", google_group_data='') person.groups.add(google_group) if user.get('emails'): email_addresses = user['emails'] for email_item in email_addresses: is_primary = 'primary' in email_item add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, email_item['address'], is_primary) if user.get('aliases'): aliases = user['aliases'] for alias in aliases: add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, alias) if user.get('nonEditableAliases'): aliases = user['nonEditableAliases'] for alias in aliases: add_identifier(self.PERSON_MODEL, person, Identifier.EMAIL, alias) # Invite user to system task_run_intro_email.apply_async((person.id,), countdown=1)
def import_groups_from_json(self, groups): super(Office365ImportAPI, self).import_groups_from_json(groups) for group in groups: # check if we've already seen this group - assumes office365_id is unique if GroupIdentifier.objects.filter( identifier=group['id'], identifier_type=Identifier.OFFICE_ID).exists(): office365_group = GroupIdentifier.objects.get( identifier=group['id'], identifier_type=Identifier.OFFICE_ID).belongs_to else: # create a group office365_group, id_created = Group.objects.update_or_create( name=group['name'], group_type=Group.OFFICE, description="Imported from Office365 Apps", office365_group_data=json.dumps(group)) add_identifier(self.GROUP_MODEL, office365_group, Identifier.OFFICE_ID, group['id']) if group.get('aliases'): aliases = group['aliases'] for alias in aliases: add_identifier(self.GROUP_MODEL, office365_group, Identifier.EMAIL, alias) if group.get('nonEditableAliases'): aliases = group['nonEditableAliases'] for alias in aliases: add_identifier(self.GROUP_MODEL, office365_group, Identifier.EMAIL, alias)
def import_groups_from_json(self, groups): super(GoogleImportAPI, self).import_groups_from_json(groups) for group in groups: # check if we've already seen this group - assumes google_id is unique if GroupIdentifier.objects.filter(identifier=group['id'], identifier_type=Identifier.GOOGLE_ID).exists(): google_group = GroupIdentifier.objects.get(identifier=group['id'], identifier_type=Identifier.GOOGLE_ID).belongs_to else: # create a group google_group, id_created = Group.objects.update_or_create(name=group['name'], group_type=Group.GOOGLE, description="Imported from Google Apps", google_group_data=json.dumps(group)) add_identifier(self.GROUP_MODEL, google_group, Identifier.GOOGLE_ID, group['id']) if group.get('aliases'): aliases = group['aliases'] for alias in aliases: add_identifier(self.GROUP_MODEL, google_group, Identifier.EMAIL, alias) if group.get('nonEditableAliases'): aliases = group['nonEditableAliases'] for alias in aliases: add_identifier(self.GROUP_MODEL, google_group, Identifier.EMAIL, alias)
def import_users_from_json(self, users): super(LDAPImportAPI, self).import_users_from_json(users) ldap_json = json.loads(users) entries = ldap_json['entries'] for person in entries: log.debug("Handling groups '%s'", person.get('objectGUID')) attributes = person['attributes'] model_attributes = {} groups = [] gen_groups = [] email_addresses = [] for key, value in attributes.items(): # log.debug("Handling attributes for person key = %s, value = %s", key, value) if len(value) > 0: if key == 'memberOf': for cn in value: qs = Group.objects.filter(name=cn) for q in qs: groups.append(q) if q.groups: gen_groups.append(q.groups) elif key == 'proxyAddresses': for address in value: email_addresses.append(address[5:]) else: value_string = "" try: if isinstance(value, str): value_string = value value_string = value_string.decode('utf-8') else: for e in value: if isinstance(e, str): value_string = ''.join(e) else: value_string = e['encoded'] if key in ('accountExpires', 'badPasswordTime', 'lastLogoff', 'lastLogon', 'lastLogonTimestamp', 'pwdLastSet', 'uSNChanged', 'uSNCreated', 'whenChanged', 'whenCreated'): date = convert_date_time(self, value_string) if date: value_string = date.isoformat() if key in ('adminCount', 'badPwdCount', 'logonCount'): # print("WTF IS HAPPENING HERE") # print(value_string) if value_string is None or value_string is "": value_string = 0 else: value_string = int(value_string) log.debug("Adding to mode;_attributes for person key = %s, value = %s", ldap_field_to_user_model(self, key), value_string) model_attributes[ldap_field_to_user_model(self, key)] = value_string except UnicodeDecodeError: log.debug("Adding to mode;_attributes for person key = %s, value = %s", ldap_field_to_user_model(self, key), clean_hex(self, value_string)) model_attributes[ldap_field_to_user_model(self, key)] = clean_hex(self, value_string) attributes.pop('memberOf', None) attributes.pop('proxyAddresses', None) name = model_attributes['cn']; firstname = '' surname = name if " " in name: name_parts = name.split(" ") if len(name_parts) > 1: firstname = name_parts[0] surname = name_parts[1] curr_person, p_created = Person.objects.get_or_create(first_name=firstname, surname=surname, ldap_identity_data=json.dumps(model_attributes)) if 'object_guid' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.GUID, model_attributes['object_guid']) if 'object_sid' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.SID, model_attributes['object_sid']) if 'distinguished_name' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['distinguished_name']) if 'sam_account_name' in attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['sam_account_name']) if 'cn' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['cn']) # Import the email addresses. for email_address in email_addresses: add_identifier(self.PERSON_MODEL, curr_person, Identifier.EMAIL, email_address) for group in groups: if curr_person.groups.filter(id=group.id).count() == 0: curr_person.groups.add(group) # Invite user to system task_run_intro_email.apply_async((curr_person.id,), countdown=1)
def import_groups_from_json(self, groups): super(LDAPImportAPI, self).import_groups_from_json(groups) ldap_json = json.loads(groups) entries = ldap_json['entries'] for group in entries: log.debug("Handling groups '%s'", group.get('dn')) attributes = group['attributes'] model_attributes = {} log.debug("Cleaning and processing attributes for group") for key, value in attributes.items(): if len(value) > 0: value_string = "" try: if isinstance(value, str): value_string = value value_string = value_string.decode('utf-8') else: for e in value: if isinstance(e, str): value_string = ''.join(e) # value_string = value_string.decode('utf-8') else: value_string = e['encoded'] model_attributes[ldap_field_to_group_model(self, key)] = value_string except UnicodeDecodeError: model_attributes[ldap_field_to_group_model(self, key)] = clean_hex(self, value_string) # # If no matching groups currently exists then create one, otherwise # # update the existing groups. groups = Group.objects.filter(name=model_attributes['cn']) if groups.count() == 0: # log.debug("Attempting to create new Group object") # for k, v in model_attributes.items(): # log.debug("Model Attributes : %s = %s", k, v) gen_group, group_created = Group.objects.get_or_create(name=model_attributes['cn'], group_type=Group.AD, description="Imported group from LDAP") if 'object_guid' in model_attributes: log.debug("Adding group identifier (%s) as type guid to %s", model_attributes['object_guid'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.GUID, model_attributes['object_guid']) if 'object_sid' in model_attributes: log.debug("Adding group identifier (%s) as type sid to %s", model_attributes['object_sid'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.SID, model_attributes['object_sid']) if 'distinguished_name' in model_attributes: log.debug("Adding group identifier (%s) as type dist name to %s", model_attributes['distinguished_name'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.NAME, model_attributes['distinguished_name']) if 'sam_account_name' in model_attributes: log.debug("Adding group identifier (%s) as type sam account name to %s", model_attributes['sam_account_name'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.NAME, model_attributes['sam_account_name'])
def import_users_from_json(self, users): super(LDAPImportAPI, self).import_users_from_json(users) ldap_json = json.loads(users) entries = ldap_json['entries'] for person in entries: log.debug("Handling groups '%s'", person.get('objectGUID')) attributes = person['attributes'] model_attributes = {} groups = [] gen_groups = [] email_addresses = [] for key, value in attributes.items(): # log.debug("Handling attributes for person key = %s, value = %s", key, value) if len(value) > 0: if key == 'memberOf': for cn in value: qs = Group.objects.filter(name=cn) for q in qs: groups.append(q) if q.groups: gen_groups.append(q.groups) elif key == 'proxyAddresses': for address in value: email_addresses.append(address[5:]) else: value_string = "" try: if isinstance(value, str): value_string = value value_string = value_string.decode('utf-8') else: for e in value: if isinstance(e, str): value_string = ''.join(e) else: value_string = e['encoded'] if key in ('accountExpires', 'badPasswordTime', 'lastLogoff', 'lastLogon', 'lastLogonTimestamp', 'pwdLastSet', 'uSNChanged', 'uSNCreated', 'whenChanged', 'whenCreated'): date = convert_date_time(self, value_string) if date: value_string = date.isoformat() if key in ('adminCount', 'badPwdCount', 'logonCount'): # print("WTF IS HAPPENING HERE") # print(value_string) if value_string is None or value_string is "": value_string = 0 else: value_string = int(value_string) log.debug( "Adding to mode;_attributes for person key = %s, value = %s", ldap_field_to_user_model(self, key), value_string) model_attributes[ldap_field_to_user_model( self, key)] = value_string except UnicodeDecodeError: log.debug( "Adding to mode;_attributes for person key = %s, value = %s", ldap_field_to_user_model(self, key), clean_hex(self, value_string)) model_attributes[ldap_field_to_user_model( self, key)] = clean_hex(self, value_string) attributes.pop('memberOf', None) attributes.pop('proxyAddresses', None) name = model_attributes['cn'] firstname = '' surname = name if " " in name: name_parts = name.split(" ") if len(name_parts) > 1: firstname = name_parts[0] surname = name_parts[1] curr_person, p_created = Person.objects.get_or_create( first_name=firstname, surname=surname, ldap_identity_data=json.dumps(model_attributes)) if 'object_guid' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.GUID, model_attributes['object_guid']) if 'object_sid' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.SID, model_attributes['object_sid']) if 'distinguished_name' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['distinguished_name']) if 'sam_account_name' in attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['sam_account_name']) if 'cn' in model_attributes: add_identifier(self.PERSON_MODEL, curr_person, Identifier.NAME, model_attributes['cn']) # Import the email addresses. for email_address in email_addresses: add_identifier(self.PERSON_MODEL, curr_person, Identifier.EMAIL, email_address) for group in groups: if curr_person.groups.filter(id=group.id).count() == 0: curr_person.groups.add(group) # Invite user to system task_run_intro_email.apply_async((curr_person.id, ), countdown=1)
def import_groups_from_json(self, groups): super(LDAPImportAPI, self).import_groups_from_json(groups) ldap_json = json.loads(groups) entries = ldap_json['entries'] for group in entries: log.debug("Handling groups '%s'", group.get('dn')) attributes = group['attributes'] model_attributes = {} log.debug("Cleaning and processing attributes for group") for key, value in attributes.items(): if len(value) > 0: value_string = "" try: if isinstance(value, str): value_string = value value_string = value_string.decode('utf-8') else: for e in value: if isinstance(e, str): value_string = ''.join(e) # value_string = value_string.decode('utf-8') else: value_string = e['encoded'] model_attributes[ldap_field_to_group_model( self, key)] = value_string except UnicodeDecodeError: model_attributes[ldap_field_to_group_model( self, key)] = clean_hex(self, value_string) # # If no matching groups currently exists then create one, otherwise # # update the existing groups. groups = Group.objects.filter(name=model_attributes['cn']) if groups.count() == 0: # log.debug("Attempting to create new Group object") # for k, v in model_attributes.items(): # log.debug("Model Attributes : %s = %s", k, v) gen_group, group_created = Group.objects.get_or_create( name=model_attributes['cn'], group_type=Group.AD, description="Imported group from LDAP") if 'object_guid' in model_attributes: log.debug( "Adding group identifier (%s) as type guid to %s", model_attributes['object_guid'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.GUID, model_attributes['object_guid']) if 'object_sid' in model_attributes: log.debug("Adding group identifier (%s) as type sid to %s", model_attributes['object_sid'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.SID, model_attributes['object_sid']) if 'distinguished_name' in model_attributes: log.debug( "Adding group identifier (%s) as type dist name to %s", model_attributes['distinguished_name'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.NAME, model_attributes['distinguished_name']) if 'sam_account_name' in model_attributes: log.debug( "Adding group identifier (%s) as type sam account name to %s", model_attributes['sam_account_name'], gen_group.name) add_identifier(self.GROUP_MODEL, gen_group, Identifier.NAME, model_attributes['sam_account_name'])