Show
Ignore:
Timestamp:
06/16/03 13:59:09 (21 years ago)
Author:
jalet
Message:

More work on LDAP

Files:
1 modified

Legend:

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

    r1030 r1031  
    2121# 
    2222# $Log$ 
     23# Revision 1.9  2003/06/16 11:59:09  jalet 
     24# More work on LDAP 
     25# 
    2326# Revision 1.8  2003/06/15 22:26:52  jalet 
    2427# More work on LDAP 
     
    8689        except ldap.SERVER_DOWN :     
    8790            raise PyKotaStorageError, "LDAP backend for PyKota seems to be down !" # TODO : translate 
     91        except ldap.LDAPError :     
     92            raise PyKotaStorageError, "Unable to connect to LDAP server %s as %s." % (host, user) # TODO : translate 
    8893        else :     
    8994            self.closed = 0 
     
    116121            raise PyKotaStorageError, _("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope) 
    117122        else :      
     123            if self.debug : 
     124                self.tool.logger.log_message("QUERY : Result : %s" % result, "debug") 
    118125            return result 
    119126             
     
    155162    def getPrinterName(self, printerid) :         
    156163        """Returns a printerid given a printer id.""" 
    157         result = self.doSearch("objectClass=*", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE) 
     164        result = self.doSearch("objectClass=pykotaPrinter", ["pykotaPrinterName"], base=printerid, scope=ldap.SCOPE_BASE) 
    158165        if result : 
    159166            return result[0][1]["pykotaPrinterName"][0] 
     
    167174    def setPrinterPrices(self, printerid, perpage, perjob) : 
    168175        """Sets prices per job and per page for a given printer.""" 
    169         raise PyKotaStorageError, "Not implemented !" 
     176        fields = {  
     177                   "pykotaPricePerPage" : str(perpage), 
     178                   "pykotaPricePerJob" : str(perjob), 
     179                 }  
     180        return self.doModify(printerid, fields) 
    170181     
    171182    def getUserId(self, username) : 
     
    174185        if result : 
    175186            return result[0][0] 
     187             
     188    def getUserName(self, userid) : 
     189        """Returns a username given a userid.""" 
     190        result = self.doSearch("objectClass=pykotaAccount", ["pykotaUserName"], base=userid, scope=ldap.SCOPE_BASE) 
     191        if result : 
     192            return result[0][1]["pykotaUserName"][0] 
    176193             
    177194    def getGroupId(self, groupname) : 
     
    220237    def getUserGroupsNames(self, userid) :         
    221238        """Returns the list of groups' names the user is a member of.""" 
    222         raise PyKotaStorageError, "Not implemented !" 
     239        username = self.getUserName(userid) 
     240        if username : 
     241            result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], username), [self.info["grouprdn"]], base=self.info["groupbase"]) 
     242            if result : 
     243                return [v[self.info["grouprdn"]][0] for (k, v) in result] 
    223244         
    224245    def addPrinter(self, printername) :         
     
    269290                 }  
    270291        dn = "cn=%s,%s" % (uuid, self.info["userquotabase"]) 
    271         return self.doAdd(dn, fields) 
     292        self.doAdd(dn, fields) 
     293        return (dn, printerid) 
    272294         
    273295    def addGroupPQuota(self, groupname, printerid) : 
     
    283305                 }  
    284306        dn = "cn=%s,%s" % (uuid, self.info["groupquotabase"]) 
    285         return self.doAdd(dn, fields) 
    286          
    287     def increaseUserBalance(self, userid, amount) :     
     307        self.doAdd(dn, fields) 
     308        return (dn, printerid) 
     309         
     310    def increaseUserBalance(self, userquotaid, amount) :     
    288311        """Increases (or decreases) an user's account balance by a given amount.""" 
    289         raise PyKotaStorageError, "Not implemented !" 
     312        balance = self.getUserBalance(userquotaid) 
     313        if balance : 
     314            (newbal, newpaid) = [(float(v) + float(amount)) for v in balance] 
     315            result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName"], base=userquotaid, scope=ldap.SCOPE_BASE) 
     316            if result : 
     317                username = result[0][1]["pykotaUserName"][0] 
     318                if username : 
     319                    result = self.doSearch("(&(objectClass=pykotaAccountBalance)(pykotaUserName=%s))" % username , [ "pykotaBalance", "pykotaLifeTimePaid"]) 
     320                    fields = { 
     321                               "pykotaBalance" : str(newbal), 
     322                               "pykotaLifeTimePaid" : str(newpaid), 
     323                             } 
     324                    return self.doModify(result[0][0], fields)          
    290325         
    291326    def getUserBalance(self, userquotaid) :     
     
    349384                return result[0][1]["pykotaLimitBy"][0] 
    350385         
    351     def setUserBalance(self, userid, balance) :     
     386    def setUserBalance(self, userquotaid, balance) :     
    352387        """Sets the account balance for a given user to a fixed value.""" 
    353         raise PyKotaStorageError, "Not implemented !" 
    354          
    355     def limitUserBy(self, userid, limitby) :     
     388        oldbalance = self.getUserBalance(userquotaid) 
     389        if oldbalance : 
     390            (oldbal, oldpaid) = oldbalance 
     391            difference = balance - oldbal 
     392            return self.increaseUserBalance(userquotaid, difference) 
     393         
     394    def limitUserBy(self, userquotaid, limitby) :     
    356395        """Limits a given user based either on print quota or on account balance.""" 
    357         raise PyKotaStorageError, "Not implemented !" 
    358          
    359     def limitGroupBy(self, groupid, limitby) :     
     396        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaUserName"], base=userquotaid, scope=ldap.SCOPE_BASE) 
     397        if result : 
     398            username = result[0][1]["pykotaUserName"][0] 
     399            fields = { 
     400                       "pykotaLimitBy" : limitby, 
     401                     } 
     402            self.doModify(self.getUserId(username), fields) 
     403         
     404    def limitGroupBy(self, groupquotaid, limitby) :     
    360405        """Limits a given group based either on print quota or on sum of its users' account balances.""" 
    361         raise PyKotaStorageError, "Not implemented !" 
     406        result = self.doSearch("objectClass=pykotaGroupPQuota", ["pykotaGroupName"], base=groupquotaid, scope=ldap.SCOPE_BASE) 
     407        if result : 
     408            groupname = result[0][1]["pykotaGroupName"][0] 
     409            fields = { 
     410                       "pykotaLimitBy" : limitby, 
     411                     } 
     412            self.doModify(self.getGroupId(groupname), fields) 
    362413         
    363414    def setUserPQuota(self, userquotaid, printerid, softlimit, hardlimit) : 
     
    377428        return self.doModify(groupquotaid, fields) 
    378429         
    379     def resetUserPQuota(self, userid, printerid) :     
     430    def resetUserPQuota(self, userquotaid, printerid) :     
    380431        """Resets the page counter to zero for a user on a printer. Life time page counter is kept unchanged.""" 
    381         raise PyKotaStorageError, "Not implemented !" 
    382          
    383     def resetGroupPQuota(self, groupid, printerid) :     
     432        fields = { 
     433                   "pykotaPageCounter" : "0", 
     434                   "pykotaDateLimit" : "None", 
     435                 } 
     436        return self.doModify(userquotaid, fields)       
     437         
     438    def resetGroupPQuota(self, groupquotaid, printerid) :     
    384439        """Resets the page counter to zero for a group on a printer. Life time page counter is kept unchanged.""" 
    385         raise PyKotaStorageError, "Not implemented !" 
    386          
    387     def updateUserPQuota(self, userid, printerid, pagecount) : 
     440        fields = { 
     441                   "pykotaPageCounter" : "0", 
     442                   "pykotaDateLimit" : "None", 
     443                 } 
     444        return self.doModify(groupquotaid, fields)       
     445         
     446    def updateUserPQuota(self, userquotaid, printerid, pagecount) : 
    388447        """Updates the used user Quota information given (userid, printerid) and a job size in pages.""" 
    389         raise PyKotaStorageError, "Not implemented !" 
     448        jobprice = self.computePrinterJobPrice(printerid, pagecount) 
     449        result = self.doSearch("objectClass=pykotaUserPQuota", ["pykotaPageCounter", "pykotaLifePageCounter"], base=userquotaid, scope=ldap.SCOPE_BASE) 
     450        if result : 
     451            oldfields = result[0][1] 
     452            fields = { 
     453                       "pykotaPageCounter" : str(pagecount + int(oldfields["pykotaPageCounter"][0])), 
     454                       "pykotaLifePageCounter" : str(pagecount + int(oldfields["pykotaLifePageCounter"][0])), 
     455                     }   
     456            return self.doModify(userquotaid, fields)          
    390457         
    391458    def getUserPQuota(self, userquotaid, printerid) : 
     
    432499            return quota 
    433500         
    434     def setUserDateLimit(self, userid, printerid, datelimit) : 
     501    def setUserDateLimit(self, userquotaid, printerid, datelimit) : 
    435502        """Sets the limit date for a soft limit to become an hard one given (userid, printerid).""" 
    436         raise PyKotaStorageError, "Not implemented !" 
    437          
    438     def setGroupDateLimit(self, groupid, printerid, datelimit) : 
     503        fields = { 
     504                   "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), 
     505                 } 
     506        return self.doModify(userquotaid, fields) 
     507         
     508    def setGroupDateLimit(self, groupquotaid, printerid, datelimit) : 
    439509        """Sets the limit date for a soft limit to become an hard one given (groupid, printerid).""" 
    440         raise PyKotaStorageError, "Not implemented !" 
     510        fields = { 
     511                   "pykotaDateLimit" : "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second), 
     512                 } 
     513        return self.doModify(groupquotaid, fields) 
    441514         
    442515    def addJobToHistory(self, jobid, userid, printerid, pagecounter, action) :