Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 437

Show
Ignore:
Timestamp:
04/27/07 08:33:02
Author:
umaxx
Message:

implement connect() and disconnect() to ldap server (tls with certs will be possible too) and simplify things: remove own exceptions, just raise ldap.* errors for now

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/ldap/storage/storeldap.py

    r436 r437  
    2323from dejavu import storage, logic, errors 
    2424 
    25 ##class ConnectionError(Exception): 
    26 ##    """Indicates an error in ldap connection.""" 
    27 ##    def __init__(self, message, details=""): 
    28 ##        DatabaseError.__init__(self) 
    29 ##        self.message = message 
    30 ##        self.details = details 
    31 ## 
    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 = message 
    41         self.details = details 
    42         self.query = query 
    43         self.query_parameters = query_parameters 
    44  
    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  
    4925class StorageManagerLDAP(storage.StorageManager): 
    5026    """StoreManager to save and retrieve Units in LDAP Directory. 
     
    5430    def __init__(self, arena, allOptions={}): 
    5531        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 
    5638        self.basedn = allOptions['base_dn'] 
    5739 
    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) 
    5966 
    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() 
    7870 
    7971    def _search(self, cls): 
     
    9183        ## no idea from where to get it 
    9284        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*') 
    9886 
    9987        return result, lock 
     
    10694                for unitdict in data.itervalues(): 
    10795                    unit = cls() 
    108                     # Set the attribute directly to avoid __set__ overhead. 
     96                    ## set the attribute directly to avoid __set__ overhead. 
    10997                    unit._properties = unitdict 
    11098                    if expr is None or expr(unit): 
     
    170158        query_parameters = [("objectclass", [clstypename]), 
    171159                            (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) 
    176161 
    177162## todo: implement has_storage functions 
     
    181166    def drop_storage(self, cls): 
    182167        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) 
    187169 
    188170## todo: implement drop_property functions 
     
    198180        for key, value in columns.items(): 
    199181            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) 
    204183 
    205184    def rename_property(self, cls, oldname, newname): 
     
    209188        query_oldname = "%s, %s=%s" % (self.basedn, clstype, clsname) 
    210189        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)