Changeset 437
- Timestamp:
- 04/27/07 03:33:02
- Files:
-
- branches/ldap/storage/storeldap.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ldap/storage/storeldap.py
r436 r437 23 23 from dejavu import storage, logic, errors 24 24 25 ##class ConnectionError(Exception):26 ## """Indicates an error in ldap connection."""27 ## def __init__(self, message, details=""):28 ## DatabaseError.__init__(self)29 ## self.message = message30 ## self.details = details31 ##32 ## def __str__(self):33 ## """Representation of exception."""34 ## return "%s %s" % (self.message, self.details)35 36 class QueryError(Exception):37 """Indicates an error on ldap query."""38 def __init__(self, message, details, query, query_parameters=""):39 Exception.__init__(self)40 self.message = message41 self.details = details42 self.query = query43 self.query_parameters = query_parameters44 45 def __str__(self):46 """Representation of exception."""47 return "%s (%s) - query: %s %s" % (self.message, self.details, self.query, self.query_parameters)48 49 25 class StorageManagerLDAP(storage.StorageManager): 50 26 """StoreManager to save and retrieve Units in LDAP Directory. … … 54 30 def __init__(self, arena, allOptions={}): 55 31 storage.StorageManager.__init__(self, arena, allOptions) 32 self.connection = None 33 ## url is something like: ldap(s)://localhost:1636 or ldapi://%2ftmp%2fopenldap2 34 ## bind_dn is the ldap user for authentification on the server 35 self._connect(allOptions['url'], allOptions['bind_dn'], allOptions['password']) 36 ## every request will use this as base, this means 37 ## everything happens under this (base) leaf of the tree 56 38 self.basedn = allOptions['base_dn'] 57 39 58 ## connection stuff does not belong here? 40 def _connect(self, url, username=None, password=None, version=ldap.VERSION3, tls_cert=''): 41 """Connect and bind to ldap server.""" 42 ## setting debug level 43 #ldap.set_option(ldap.OPT_DEBUG_LEVEL, 255) 44 ## setting sizelimit 45 #ldap.set_option(ldap.OPT_SIZELIMIT, 10) 46 ## set path name of file containing all CA certificates 47 ## needed to validate server certificates 48 #ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, tls_cert) 49 ## create ldap object instance (connect to url) 50 self.connection = ldap.initialize(url, 1) 51 ## set ldap protocol version used 52 self.connection.protocol_version = version 53 #if not tls_cert == '': 54 # ## try tls extended operation 55 # self.connection.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND) 56 # self.connection.start_tls_s() 57 if username == None and password == None: 58 ## anonymous bind 59 self.connection.bind_s('', '', ldap.AUTH_SIMPLE) 60 else: 61 self.connection.simple_bind_s(username, password) 62 ## set connections size limit to 20 63 #self.connection.set_option(ldap.OPT_SIZELIMIT,20) 64 ## set timelimit to 60 seconds 65 #self.connection.set_option(ldap.OPT_TIMELIMIT,60) 59 66 60 ## def __connect_ldap(self): 61 ## """Connect to ldap backend.""" 62 ## ## setting debug level 63 ## #ldap.set_option(ldap.OPT_DEBUG_LEVEL,255) 64 ## ## setting sizelimit 65 ## #ldap.set_option(ldap.OPT_SIZELIMIT,10) 66 ## try: 67 ## ## conect to host 68 ## self.connection = ldap.initialize(binddn, trace_level=1) 69 ## self.connection.simple_bind_s(username, password) 70 ## except ldap.INVALID_CREDENTIALS, error: 71 ## raise ConnectionError("can not connect to database", error) 72 ## else: 73 ## ## set connections size limit to 20 74 ## #self.connection.set_option(ldap.OPT_SIZELIMIT,20) 75 ## ## set timelimit to 60 seconds 76 ## #self.connection.set_option(ldap.OPT_TIMELIMIT,60) 77 ## pass 67 def _shutdown(self): 68 """Unbind from ldap server.""" 69 self.connection.unbind_s() 78 70 79 71 def _search(self, cls): … … 91 83 ## no idea from where to get it 92 84 query = "%s, %s=%s" % (self.basedn, clstype, clsname) 93 94 try: 95 result = self.connection.search_s(query,ldap.SCOPE_SUBTREE,'sn='+'a*') 96 except ldap.LDAPError, error: 97 raise QueryError("ldap select error", error, query) 85 result = self.connection.search_s(query,ldap.SCOPE_SUBTREE,'sn='+'a*') 98 86 99 87 return result, lock … … 106 94 for unitdict in data.itervalues(): 107 95 unit = cls() 108 # Set the attribute directly to avoid __set__ overhead.96 ## set the attribute directly to avoid __set__ overhead. 109 97 unit._properties = unitdict 110 98 if expr is None or expr(unit): … … 170 158 query_parameters = [("objectclass", [clstypename]), 171 159 (clstyp, [clsname])] 172 try: 173 self.connection.add_s(query, query_parameters) 174 except ldap.LDAPError, error: 175 raise QueryError("ldap create error", error, query, query_parameters) 160 self.connection.add_s(query, query_parameters) 176 161 177 162 ## todo: implement has_storage functions … … 181 166 def drop_storage(self, cls): 182 167 query = "%s=%s, %s" % (clstype, clsname, self.basedn) 183 try: 184 self.connection.delete_s(query) 185 except ldap.LDAPError, error: 186 raise QueryError("ldap drop error", error, query) 168 self.connection.delete_s(query) 187 169 188 170 ## todo: implement drop_property functions … … 198 180 for key, value in columns.items(): 199 181 columns_list.append((key, value)) 200 try: 201 return self.connection.add_s(query, columns_list) 202 except ldap.LDAPError, error: 203 raise QueryError("ldap insert error", error, query) 182 self.connection.add_s(query, columns_list) 204 183 205 184 def rename_property(self, cls, oldname, newname): … … 209 188 query_oldname = "%s, %s=%s" % (self.basedn, clstype, clsname) 210 189 query_newname = "%s, %s=%s" % (self.basedn, clstype, newname) 211 try: 212 self.connection.rename_s(oldname, newname) 213 except ldap.LDAPError, error: 214 raise QueryError("ldap create error", error, query, query_parameters) 215 190 self.connection.rename_s(oldname, newname)
