Changeset 713

Show
Ignore:
Timestamp:
02/06/03 10:19:02 (22 years ago)
Author:
jalet
Message:

More robust behavior (hopefully) when the user or printer is not managed
correctly by the Quota System : e.g. cupsFilter added in ppd file, but
printer and/or user not 'yet?' in storage.

Location:
pykota/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/conf/pykota.conf.sample

    r707 r713  
    6060# 
    6161# Default policy for inexistant users (e.g. root) 
    62 # either accept or deny 
    63 policy: accept 
     62# either allow or deny 
     63policy: allow 
    6464 
  • pykota/trunk/pykota/config.py

    r708 r713  
    1515# 
    1616# $Log$ 
     17# Revision 1.4  2003/02/06 09:19:02  jalet 
     18# More robust behavior (hopefully) when the user or printer is not managed 
     19# correctly by the Quota System : e.g. cupsFilter added in ppd file, but 
     20# printer and/or user not 'yet?' in storage. 
     21# 
    1722# Revision 1.3  2003/02/05 23:26:22  jalet 
    1823# Incorrect handling of grace delay 
     
    7782                    raise PyKotaConfigError, "Option %s not found in section %s of %s" % (option, printer, self.filename) 
    7883                     
    79             validpolicies = [ "accept", "deny" ]      
    80             if self.config.get(printer, "policy").lower() not in validpolicies : 
     84            validpolicies = [ "ALLOW", "DENY" ]      
     85            if self.config.get(printer, "policy").upper() not in validpolicies : 
    8186                raise PyKotaConfigError, "Option policy in section %s only supports values in %s" % (printer, str(validrequesters)) 
    8287             
     
    119124    def getPrinterPolicy(self, printer) :     
    120125        """Returns the default policy for the current printer.""" 
    121         return self.config.get(printer, "policy").lower() 
     126        return self.config.get(printer, "policy").upper() 
    122127         
    123128    def getSMTPServer(self) :     
  • pykota/trunk/pykota/storages/sql.py

    r708 r713  
    1515# 
    1616# $Log$ 
     17# Revision 1.6  2003/02/06 09:19:02  jalet 
     18# More robust behavior (hopefully) when the user or printer is not managed 
     19# correctly by the Quota System : e.g. cupsFilter added in ppd file, but 
     20# printer and/or user not 'yet?' in storage. 
     21# 
    1722# Revision 1.5  2003/02/05 23:26:22  jalet 
    1823# Incorrect handling of grace delay 
     
    6065         
    6166    def addUserPQuota(self, username, printername) : 
    62         printerid = self.getPrinterId(printername) 
     67        (userid, printerid) = self.getUPIds(username, printername) 
    6368        if printerid is None :     
    6469            self.doQuery("INSERT INTO printers (printername) VALUES (%s);" % self.doQuote(printername)) 
    65             printerid = self.getPrinterId(printername) 
    66         userid = self.getUserId(username) 
    6770        if userid is None :     
    6871            self.doQuery("INSERT INTO users (username) VALUES (%s);" % self.doQuote(username)) 
    69             userid = self.getUserId(username) 
    70         if (printerid is not None) and (userid is not None) :     
     72        (userid, printerid) = self.getUPIds(username, printername) 
     73        if (userid is not None) and (printerid is not None) : 
    7174            return self.doQuery("INSERT INTO userpquota (userid, printerid) VALUES (%s, %s);" % (self.doQuote(userid), self.doQuote(printerid))) 
    7275         
     76    def getUPIds(self, username, printername) :     
     77        return (self.getUserId(username), self.getPrinterId(printername)) 
     78         
    7379    def getUserPQuota(self, username, printername) : 
    74         result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) 
    75         try : 
    76             return self.doParseResult(result)[0] 
    77         except TypeError :      # Not found     
    78             return 
     80        (userid, printerid) = self.getUPIds(username, printername) 
     81        if (userid is not None) and (printerid is not None) : 
     82            result = self.doQuery("SELECT pagecounter, softlimit, hardlimit, datelimit FROM userpquota WHERE userid=%s AND printerid=%s;" % (self.doQuote(userid), self.doQuote(printerid))) 
     83            try : 
     84                return self.doParseResult(result)[0] 
     85            except TypeError :      # Not found     
     86                pass 
    7987         
    8088    def setUserPQuota(self, username, printername, softlimit, hardlimit) : 
    81         self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) 
     89        (userid, printerid) = self.getUPIds(username, printername) 
     90        if (userid is not None) and (printerid is not None) : 
     91            self.doQuery("UPDATE userpquota SET softlimit=%s, hardlimit=%s, datelimit=NULL WHERE userid=%s AND printerid=%s;" % (self.doQuote(softlimit), self.doQuote(hardlimit), self.doQuote(userid), self.doQuote(printerid))) 
    8292         
    8393    def updateUserPQuota(self, username, printername, pagecount) : 
    84         self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) 
     94        (userid, printerid) = self.getUPIds(username, printername) 
     95        if (userid is not None) and (printerid is not None) : 
     96            self.doQuery("UPDATE userpquota SET pagecounter=pagecounter+(%s) WHERE userid=%s AND printerid=%s;" % (self.doQuote(pagecount), self.doQuote(userid), self.doQuote(printerid))) 
    8597         
    8698    def buyUserPQuota(self, username, printername, pagebought) : 
  • pykota/trunk/pykota/tool.py

    r712 r713  
    1515# 
    1616# $Log$ 
     17# Revision 1.8  2003/02/06 09:19:02  jalet 
     18# More robust behavior (hopefully) when the user or printer is not managed 
     19# correctly by the Quota System : e.g. cupsFilter added in ppd file, but 
     20# printer and/or user not 'yet?' in storage. 
     21# 
    1722# Revision 1.7  2003/02/06 00:00:45  jalet 
    1823# Now includes the printer name in email messages 
     
    9196        now = DateTime.now() 
    9297        quota = self.storage.getUserPQuota(username, printername) 
    93         pagecounter = quota["pagecounter"] 
    94         softlimit = quota["softlimit"] 
    95         hardlimit = quota["hardlimit"] 
    96         datelimit = quota["datelimit"] 
    97         if datelimit is not None : 
    98             datelimit = DateTime.ISO.ParseDateTime(datelimit) 
    99         if softlimit is not None : 
    100             if pagecounter < softlimit : 
     98        if quota is None : 
     99            # Unknown user or printer or combination 
     100            policy = self.config.getPrinterPolicy(printername) 
     101            if policy in [None, "ALLOW"] : 
    101102                action = "ALLOW" 
    102             elif hardlimit is not None : 
    103                  gracedelay = self.config.getGraceDelay() 
    104                  if softlimit <= pagecounter < hardlimit :     
    105                      if datelimit is None : 
    106                          datelimit = now + gracedelay 
    107                          self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) 
    108                      if (now + gracedelay) < datelimit : 
    109                          action = "WARN" 
    110                      else :     
     103            else :     
     104                action = "DENY" 
     105            self.logger.log_message("Unable to match user %s on printer %s, applying default policy (%s)" % (username, printername, action), "warn") 
     106            return (action, None, None) 
     107        else :     
     108            pagecounter = quota["pagecounter"] 
     109            softlimit = quota["softlimit"] 
     110            hardlimit = quota["hardlimit"] 
     111            datelimit = quota["datelimit"] 
     112            if datelimit is not None : 
     113                datelimit = DateTime.ISO.ParseDateTime(datelimit) 
     114            if softlimit is not None : 
     115                if pagecounter < softlimit : 
     116                    action = "ALLOW" 
     117                elif hardlimit is not None : 
     118                     gracedelay = self.config.getGraceDelay() 
     119                     if softlimit <= pagecounter < hardlimit :     
     120                         if datelimit is None : 
     121                             datelimit = now + gracedelay 
     122                             self.storage.doQuery("UPDATE userpquota SET datelimit=%s::DATETIME WHERE userid=%s AND printerid=%s;" % (self.doQuote("%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second)), self.doQuote(self.getUserId(username)), self.doQuote(self.getPrinterId(printername)))) 
     123                         if (now + gracedelay) < datelimit : 
     124                             action = "WARN" 
     125                         else :     
     126                             action = "DENY" 
     127                     else :          
    111128                         action = "DENY" 
    112                  else :          
    113                      action = "DENY" 
     129                else :         
     130                    action = "DENY" 
    114131            else :         
    115                 action = "DENY" 
    116         else :         
    117             action = "ALLOW" 
    118         return (action, (hardlimit - pagecounter), datelimit) 
     132                action = "ALLOW" 
     133            return (action, (hardlimit - pagecounter), datelimit) 
    119134     
    120135    def warnQuotaPrinter(self, username) : 
     
    122137        (action, grace, gracedate) = self.checkUserPQuota(username, self.printername) 
    123138        if action == "DENY" : 
    124             adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) 
    125             self.logger.log_message(adminmessage) 
    126             self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername) 
    127             self.sendMessageToAdmin("Print Quota", adminmessage) 
     139            if (grace is not None) and (gracedate is not None) : 
     140                # only when both user and printer are known 
     141                adminmessage = "Print Quota exceeded for user %s on printer %s" % (username, self.printername) 
     142                self.logger.log_message(adminmessage) 
     143                self.sendMessageToUser(username, "Print Quota Exceeded", "You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s." % self.printername) 
     144                self.sendMessageToAdmin("Print Quota", adminmessage) 
    128145        elif action == "WARN" :     
    129146            adminmessage = "Print Quota soft limit exceeded for user %s on printer %s" % (username, self.printername)