Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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)