Show
Ignore:
Timestamp:
06/16/03 00:26:52 (21 years ago)
Author:
jalet
Message:

More work on LDAP

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/ldapstorage.py

    r1029 r1030  
    2121# 
    2222# $Log$ 
     23# Revision 1.8  2003/06/15 22:26:52  jalet 
     24# More work on LDAP 
     25# 
    2326# Revision 1.7  2003/06/14 22:44:21  jalet 
    2427# More work on LDAP storage backend. 
     
    5558# 
    5659 
     60import time 
     61import md5 
    5762import fnmatch 
    5863 
     
    6166try : 
    6267    import ldap 
     68    from ldap import modlist 
    6369except ImportError :     
    6470    import sys 
     
    9399                self.tool.logger.log_message("Database closed.", "debug") 
    94100         
    95     def doSearch(self, key, fields, base="", scope=ldap.SCOPE_SUBTREE) : 
     101    def genUUID(self) :     
     102        """Generates an unique identifier. 
     103         
     104           TODO : this one is not unique accross several print servers, but should be sufficient for testing. 
     105        """ 
     106        return md5.md5("%s" % time.time()).hexdigest() 
     107         
     108    def doSearch(self, key, fields=None, base="", scope=ldap.SCOPE_SUBTREE) : 
    96109        """Does an LDAP search query.""" 
    97110        try : 
    98             # prepends something more restrictive at the beginning of the base dn 
     111            base = base or self.basedn 
    99112            if self.debug : 
    100                 self.tool.logger.log_message("QUERY : BaseDN : %s, Scope : %s, Filter : %s, Attributes : %s" % ((base or self.basedn), scope, key, fields), "debug") 
     113                self.tool.logger.log_message("QUERY : BaseDN : %s, Scope : %s, Filter : %s, Attributes : %s" % (base, scope, key, fields), "debug") 
    101114            result = self.database.search_s(base or self.basedn, scope, key, fields) 
    102         except ldap.NO_SUCH_OBJECT :     
    103             return 
     115        except ldap.LDAPError :     
     116            raise PyKotaStorageError, _("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope) 
    104117        else :      
    105118            return result 
     119             
     120    def doAdd(self, dn, fields) : 
     121        """Adds an entry in the LDAP directory.""" 
     122        try : 
     123            if self.debug : 
     124                self.tool.logger.log_message("QUERY : ADD(%s, %s)" % (dn, str(fields)), "debug") 
     125            self.database.add_s(dn, modlist.addModlist(fields)) 
     126        except ldap.LDAPError : 
     127            raise PyKotaStorageError, _("Problem adding LDAP entry (%s, %s)") % (dn, str(fields)) 
     128        else : 
     129            return dn 
     130             
     131    def doModify(self, dn, fields) : 
     132        """Modifies an entry in the LDAP directory.""" 
     133        try : 
     134            oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE) 
     135            if self.debug : 
     136                self.tool.logger.log_message("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry[0][1], fields), "debug") 
     137            self.database.modify_s(dn, modlist.modifyModlist(oldentry[0][1], fields, ignore_oldexistent=1)) 
     138        except ldap.LDAPError : 
     139            raise PyKotaStorageError, _("Problem modifying LDAP entry (%s, %s)") % (dn, fields) 
     140        else : 
     141            return dn 
    106142         
    107143    def getMatchingPrinters(self, printerpattern) : 
     
    116152        if result : 
    117153            return result[0][0] 
     154             
     155    def getPrinterName(self, printerid) :         
     156        """Returns a printerid given a printer id.""" 
     157        result = self.doSearch("objectClass=*", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE) 
     158        if result : 
     159            return result[0][1]["pykotaPrinterName"][0] 
    118160             
    119161    def getPrinterPrices(self, printerid) :         
     
    182224    def addPrinter(self, printername) :         
    183225        """Adds a printer to the quota storage, returns its id.""" 
    184         raise PyKotaStorageError, "Not implemented !" 
     226        fields = { self.info["printerrdn"] : printername, 
     227                   "objectClass" : ["pykotaObject", "pykotaPrinter"], 
     228                   "pykotaPrinterName" : printername, 
     229                   "pykotaPricePerPage" : "0.0", 
     230                   "pykotaPricePerJob" : "0.0", 
     231                 }  
     232        dn = "%s=%s,%s" % (self.info["printerrdn"], printername, self.info["printerbase"]) 
     233        return self.doAdd(dn, fields) 
    185234         
    186235    def addUser(self, username) :         
    187236        """Adds a user to the quota storage, returns its id.""" 
    188         raise PyKotaStorageError, "Not implemented !" 
     237        fields = { self.info["userrdn"] : username, 
     238                   "objectClass" : ["pykotaObject", "pykotaAccount", "pykotaAccountBalance"], 
     239                   "pykotaUserName" : username, 
     240                   "pykotaLimitBy" : "quota", 
     241                   "pykotaBalance" : "0.0", 
     242                   "pykotaLifeTimePaid" : "0.0", 
     243                 }  
     244        dn = "%s=%s,%s" % (self.info["userrdn"], username, self.info["userbase"]) 
     245        return self.doAdd(dn, fields) 
    189246         
    190247    def addGroup(self, groupname) :         
    191248        """Adds a group to the quota storage, returns its id.""" 
    192         raise PyKotaStorageError, "Not implemented !" 
     249        fields = { self.info["grouprdn"] : groupname, 
     250                   "objectClass" : ["pykotaObject", "pykotaGroup"], 
     251                   "pykotaGroupName" : groupname, 
     252                   "pykotaLimitBy" : "quota", 
     253                 }  
     254        dn = "%s=%s,%s" % (self.info["grouprdn"], groupname, self.info["groupbase"]) 
     255        return self.doAdd(dn, fields) 
    193256         
    194257    def addUserPQuota(self, username, printerid) : 
    195258        """Initializes a user print quota on a printer, adds the user to the quota storage if needed.""" 
    196         raise PyKotaStorageError, "Not implemented !" 
     259        uuid = self.genUUID() 
     260        fields = { "objectClass" : ["pykotaObject", "pykotaUserPQuota"], 
     261                   "cn" : uuid, 
     262                   "pykotaUserName" : username, 
     263                   "pykotaPrinterName" : self.getPrinterName(printerid),  
     264                   "pykotaPageCounter" : "0", 
     265                   "pykotaLifePageCounter" : "0", 
     266                   "pykotaSoftLimit" : "0", 
     267                   "pykotaHardLimit" : "0", 
     268                   "pykotaDateLimit" : "None", 
     269                 }  
     270        dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) 
     271        return self.doAdd(dn, fields) 
    197272         
    198273    def addGroupPQuota(self, groupname, printerid) : 
    199274        """Initializes a group print quota on a printer, adds the group to the quota storage if needed.""" 
    200         raise PyKotaStorageError, "Not implemented !" 
     275        uuid = self.genUUID() 
     276        fields = { "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], 
     277                   "cn" : uuid, 
     278                   "pykotaGroupName" : groupname, 
     279                   "pykotaPrinterName" : self.getPrinterName(printerid),  
     280                   "pykotaSoftLimit" : "0", 
     281                   "pykotaHardLimit" : "0", 
     282                   "pykotaDateLimit" : "None", 
     283                 }  
     284        dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) 
     285        return self.doAdd(dn, fields) 
    201286         
    202287    def increaseUserBalance(self, userid, amount) :     
     
    276361        raise PyKotaStorageError, "Not implemented !" 
    277362         
    278     def setUserPQuota(self, userid, printerid, softlimit, hardlimit) : 
     363    def setUserPQuota(self, userquotaid, printerid, softlimit, hardlimit) : 
    279364        """Sets soft and hard limits for a user quota on a specific printer given (userid, printerid).""" 
    280         raise PyKotaStorageError, "Not implemented !" 
    281          
    282     def setGroupPQuota(self, groupid, printerid, softlimit, hardlimit) : 
     365        fields = {  
     366                   "pykotaSoftLimit" : str(softlimit), 
     367                   "pykotaHardLimit" : str(hardlimit), 
     368                 }  
     369        return self.doModify(userquotaid, fields) 
     370         
     371    def setGroupPQuota(self, groupquotaid, printerid, softlimit, hardlimit) : 
    283372        """Sets soft and hard limits for a group quota on a specific printer given (groupid, printerid).""" 
    284         raise PyKotaStorageError, "Not implemented !" 
     373        fields = {  
     374                   "pykotaSoftLimit" : str(softlimit), 
     375                   "pykotaHardLimit" : str(hardlimit), 
     376                 }  
     377        return self.doModify(groupquotaid, fields) 
    285378         
    286379    def resetUserPQuota(self, userid, printerid) :