conn_pool.py 2.82 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# CONN_POOL.PY-- 
#
# Copyright (C) 2010 Antoine Durand-Gasselin
# Author: Antoine Durand-Gasselin <adg@crans.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
22 23
import time
import sys
24 25
import ldap
import settings
26
from ldap import SERVER_DOWN
27

28
import lc_ldap.shortcuts as shortcuts
29 30 31

CONNS = {}
OBJECTS = {}
32
LDAP_USERS = {}
33
LDAP_USER_LAST_UPDATE = {}
34

35 36
LDAP_USER_TIMEOUT = 600

37
def get_conn(user, force_new=False):
38 39 40 41
    if settings.BASE_LDAP_TEST:
        make_admin_conn = shortcuts.lc_ldap_test
    else:
        make_admin_conn = shortcuts.lc_ldap_admin
42
    if force_new or not user.username in CONNS.keys():
43
        CONNS[user.username]= make_admin_conn(user=user.username)
44 45 46 47 48 49 50 51
    return CONNS[user.username]

def get_user(user, mode='ro', refresh=False):
    if not refresh and user.username in LDAP_USER_LAST_UPDATE.keys() and \
    time.time() - LDAP_USER_LAST_UPDATE[user.username] < LDAP_USER_TIMEOUT and \
    user.username in LDAP_USERS.keys() and \
    (mode == 'ro' or LDAP_USERS[user.username].mode == mode):
        return LDAP_USERS[user.username]
52
    elif user.username:
53 54 55 56
        try:
            [luser] = get_conn(user).search(u'uid=%s' % user.username, mode=mode, scope=ldap.SCOPE_ONELEVEL)
        except SERVER_DOWN:
            [luser] = get_conn(user, force_new=True).search(u'uid=%s' % user.username, mode=mode, scope=ldap.SCOPE_ONELEVEL)
57 58 59
        LDAP_USERS[user.username] = luser
        LDAP_USER_LAST_UPDATE[user.username] = time.time()
        return luser
60 61
    else:
        return None
62 63

def get_machine(user, mid, mode='ro', refresh=False):
64 65 66 67 68
    # Si on est connecté, on cherche dans les machines de l'user ldap
    if user.username:
        luser =  get_user(user, mode=mode, refresh=False)
        machine = [ m for m in get_user(user).machines() if m['mid'][0].value == int(mid) ]

69 70 71
    if mode == 'rw' and machine and machine[0].mode == 'ro':
        machine = []

72 73
    # Sinon ou si on n'a rien trouvé, on fait une requête ldap
    if not user.username or not machine:
74 75 76 77
        try:
            machine = get_conn(user).search(u'mid=%s' % mid, mode=mode)
        except SERVER_DOWN:
            machine = get_conn(user, force_new=True).search(u'mid=%s' % mid, mode=mode)
78

79 80 81 82
    if machine:
        return machine[0]
    else:
        return None