Пример #1
0
def user_search():

    q = get_value_from_all_arguments('q')
    results = []

    if q and len(q) > 1:
        l = Ldap()
        l.login_nonpriv()

        users = {}

        if db.session.bind.dialect.name == 'sqlite':
            query = User.query.filter(
                or_(
                    User.username.like(f'%{q}%'),
                    or_(
                        User.first_name.like(f'%{q}%'),
                        or_(
                            User.last_name.like(f'%{q}%'),
                            User.email.like(f'%{q}%'),
                        ))))
        elif db.session.bind.dialect.name == 'mysql':
            query = User.query.filter(
                or_(
                    User.username.like(f'%{q}%'),
                    or_(
                        func.concat(User.first_name, ' ',
                                    User.last_name).like(f'%{q}%'),
                        User.email.like(f'%{q}%'),
                    )))

        for u in query.all():
            users[u.username] = {
                'id': u.id,
                'username': u.username,
                'full_name': u.full_name,
                'first_name': u.first_name,
                'last_name': u.last_name,
            }

        for u in l.search_user(q):
            if u['username'] not in users:
                users[u['username']] = {
                    'id':
                    u['username'],
                    'username':
                    u['username'],
                    'full_name':
                    '{} {} ({})'.format(
                        u['given_name'],
                        u['surname'],
                        u['username'],
                    ),
                    'first_name':
                    u['given_name'],
                    'last_name':
                    u['surname'],
                }

        users = sorted(users.values(),
                       key=lambda u: (u['last_name'], u['first_name']))

        results = [{
            'id': u['id'],
            'text': u['full_name'],
        } for u in users]

        # for u in User.query.filter(or_(
        #     User.username.like(f'%{q}%'),
        #     or_(
        #         func.concat(User.first_name, ' ', User.last_name).like(f'%{q}%'),
        #         User.email.like(f'%{q}%'),
        #     )
        # )).all():
        #     results.append({
        #         'id': u.id,
        #         'text': u.full_name,
        #     })

    return {'results': results}
Пример #2
0
def test__ldap__is_enable__nouri(client, faker, ldap_initialize):
    current_app.config['LDAP_URI'] = ''

    ldap = Ldap()
    assert ldap.is_enabled() == False
Пример #3
0
def test__ldap__validate_password__bind_fails(client, faker, ldap_initialize):
    ldap = Ldap()
    ldap_initialize.return_value.simple_bind_s.side_effect = ldap_lib.LDAPError

    assert ldap.login(faker.pystr(min_chars=5, max_chars=10), faker.pystr(min_chars=5, max_chars=10)) == False
Пример #4
0
def test__ldap__validate_password__valid(client, faker, ldap_initialize):
    ldap = Ldap()
    assert ldap.login(faker.pystr(min_chars=5, max_chars=10), faker.pystr(min_chars=5, max_chars=10))
Пример #5
0
def test__ldap__validate_password__user_none(client, faker, ldap_initialize):
    ldap = Ldap()
    assert ldap.login(None, faker.pystr(min_chars=5, max_chars=10)) == False
Пример #6
0
def test__ldap__is_enable__withuri(client, faker, ldap_initialize):
    current_app.config['LDAP_URI'] = faker.pystr(min_chars=5, max_chars=10)

    ldap = Ldap()
    assert ldap.is_enabled()