|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
|
# Copyright 2008-2010 Mir Calculate Ltd. http://www.calculate-linux.org
|
|
|
#
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
# You may obtain a copy of the License at
|
|
|
#
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
#
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
# See the License for the specific language governing permissions and
|
|
|
# limitations under the License.
|
|
|
|
|
|
import ldap
|
|
|
from cl_utils import _error
|
|
|
|
|
|
class ldapFun(_error):
|
|
|
'''Объект для работы с LDAP сервером
|
|
|
|
|
|
подключение к серверу и поиск данных
|
|
|
'''
|
|
|
def __init__(self, dnUser, password, host="localhost"):
|
|
|
self.conLdap = False
|
|
|
# Получаем соединение с LDAP
|
|
|
try:
|
|
|
self.conLdap = self.__ldapConnect(dnUser, password, host)
|
|
|
except ldap.LDAPError, e:
|
|
|
self.setError(e[0]['desc'])
|
|
|
|
|
|
def __ldapConnect(self, dnUser, password, host):
|
|
|
"""Соединение с LDAP сервером"""
|
|
|
conLdap = ldap.initialize('ldap://%s'%host)
|
|
|
conLdap.simple_bind_s(dnUser, password)
|
|
|
return conLdap
|
|
|
|
|
|
def ldapSearch(self,baseDN, searchScope, searchFilter, retrieveAttributes):
|
|
|
try:
|
|
|
ldap_result_id = self.conLdap.search(baseDN, searchScope,
|
|
|
searchFilter,
|
|
|
retrieveAttributes)
|
|
|
result_set = []
|
|
|
while 1:
|
|
|
result_type, result_data = self.conLdap.result(ldap_result_id,
|
|
|
0)
|
|
|
if (result_data == []):
|
|
|
break
|
|
|
else:
|
|
|
if result_type == ldap.RES_SEARCH_ENTRY:
|
|
|
result_set.append(result_data)
|
|
|
except ldap.NO_SUCH_OBJECT:
|
|
|
return []
|
|
|
except:
|
|
|
return False
|
|
|
return result_set
|
|
|
|