Exemplo n.º 1
0
    def populate_db(self):
        con = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
        con.simple_bind_s(settings.AUTH_LDAP_BIND_DN,
                          settings.AUTH_LDAP_BIND_PASSWORD)
        filter_ = '(&(uid=*))'  # customize this if necessary
        ldap_user = con.search_s(settings.BASE_DN, ldap.SCOPE_SUBTREE, filter_)
        con.unbind()
        for u in ldap_user:
            username = u[1]['uid'][0].decode('UTF-8')
            if not User.objects.filter(username=username).exists():
                logger.info("Add new user '%s' from LDAP" % username)

            user = LDAPBackend().populate_user(u[1]['uid'][0].decode('UTF-8'))
            user.is_active = True

            # add a single group (wimi, stud, prof) to a user
            # has to be rewritten if group information is not stored per user
            # but instead each group in ldap stores its member!
            try:
                groups = u[1]['group']  # customize this
            except KeyError:
                logger.info(
                    "User could not be added to a group and won't be able to purchase anything."
                )
                continue

            groups = [g.decode('UTF-8') for g in groups]
            self.add_user_to_group(user, groups)
            user.save()
Exemplo n.º 2
0
    def populate_db(self):
        connection = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
        connection.simple_bind_s(settings.AUTH_LDAP_BIND_DN,
                                 settings.AUTH_LDAP_BIND_PASSWORD)
        filter_ = '(&(uid=*))'  # Customize this if necessary.
        ldap_users = connection.search_s(settings.BASE_DN, ldap.SCOPE_SUBTREE,
                                         filter_)
        connection.unbind()

        for ldap_user in ldap_users:
            username = ldap_user[1]['uid'][0].decode('UTF-8')
            if not User.objects.filter(username=username).exists():
                logger.info('Adding new user %s...' % username)

            user = LDAPBackend().populate_user(
                ldap_user[1]['uid'][0].decode('UTF-8'))
            user.is_active = True

            # Add a single group to the user.
            # When group information is not stored as part of the user info,
            # code needs to be modified.
            try:
                groups = ldap_user[1]['group']
            except KeyError:
                logger.info(
                    'User could not be added to a group and won\'t be able to '
                    'purchase anything.')
                continue

            groups = [g.decode('UTF-8') for g in groups]
            self.add_user_to_group(user, groups)
            user.save()
Exemplo n.º 3
0
    def _sync_users():
        connection = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
        connection.simple_bind_s(settings.AUTH_LDAP_BIND_DN,
                                 settings.AUTH_LDAP_BIND_PASSWORD)
        ldap_users = connection.search_s(settings.BASE_DN, ldap.SCOPE_SUBTREE,
                                         '(objectClass=posixAccount)')
        connection.unbind()

        for ldap_user in ldap_users:
            user_id = ldap_user[1]['uid'][0].decode('UTF-8')

            if not User.objects.filter(username=user_id).exists():
                logger.info(f'Adding new user {user_id}...')

            user = LDAPBackend().populate_user(user_id)
            user.is_active = True

            if len(user.groups.all()) == 0:
                logger.info(f'No groups were found for {user_id}.')

            user.save()