Show
Ignore:
Timestamp:
02/26/04 15:18:07 (20 years ago)
Author:
jalet
Message:

Should fix the remaining bugs wrt printers groups and users groups.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r1353 r1365  
    2222# 
    2323# $Log$ 
     24# Revision 1.74  2004/02/26 14:18:07  jalet 
     25# Should fix the remaining bugs wrt printers groups and users groups. 
     26# 
    2427# Revision 1.73  2004/02/19 14:20:21  jalet 
    2528# maildomain pykota.conf directive added. 
     
    448451        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) 
    449452         
     453    def _checkUserPQuota(self, userpquota) :             
     454        """Checks the user quota on a printer and deny or accept the job.""" 
     455        # then we check the user's own quota 
     456        # if we get there we are sure that policy is not EXTERNAL 
     457        user = userpquota.User 
     458        printer = userpquota.Printer 
     459        self.logdebug("Checking user %s's quota on printer %s" % (user.Name, printer.Name)) 
     460        (policy, dummy) = self.config.getPrinterPolicy(userpquota.Printer.Name) 
     461        if not userpquota.Exists : 
     462            # Unknown userquota  
     463            if policy == "ALLOW" : 
     464                action = "POLICY_ALLOW" 
     465            else :     
     466                action = "POLICY_DENY" 
     467            self.logger.log_message(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) 
     468        else :     
     469            pagecounter = int(userpquota.PageCounter or 0) 
     470            if userpquota.SoftLimit is not None : 
     471                softlimit = int(userpquota.SoftLimit) 
     472                if pagecounter < softlimit : 
     473                    action = "ALLOW" 
     474                else :     
     475                    if userpquota.HardLimit is None : 
     476                        # only a soft limit, this is equivalent to having only a hard limit 
     477                        action = "DENY" 
     478                    else :     
     479                        hardlimit = int(userpquota.HardLimit) 
     480                        if softlimit <= pagecounter < hardlimit :     
     481                            now = DateTime.now() 
     482                            if userpquota.DateLimit is not None : 
     483                                datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit) 
     484                            else : 
     485                                datelimit = now + self.config.getGraceDelay(printer.Name) 
     486                                userpquota.setDateLimit(datelimit) 
     487                            if now < datelimit : 
     488                                action = "WARN" 
     489                            else :     
     490                                action = "DENY" 
     491                        else :          
     492                            action = "DENY" 
     493            else :         
     494                if userpquota.HardLimit is not None : 
     495                    # no soft limit, only a hard one. 
     496                    hardlimit = int(userpquota.HardLimit) 
     497                    if pagecounter < hardlimit : 
     498                        action = "ALLOW" 
     499                    else :       
     500                        action = "DENY" 
     501                else : 
     502                    # Both are unset, no quota, i.e. accounting only 
     503                    action = "ALLOW" 
     504        return action 
     505     
    450506    def checkGroupPQuota(self, grouppquota) :     
    451507        """Checks the group quota on a printer and deny or accept the job.""" 
    452508        group = grouppquota.Group 
    453509        printer = grouppquota.Printer 
     510        self.logdebug("Checking group %s's quota on printer %s" % (group.Name, printer.Name)) 
    454511        if group.LimitBy and (group.LimitBy.lower() == "balance") :  
    455512            if group.AccountBalance <= 0.0 : 
     
    497554     
    498555    def checkUserPQuota(self, userpquota) : 
    499         """Checks the user quota on a printer and deny or accept the job.""" 
     556        """Checks the user quota on a printer and all its parents and deny or accept the job.""" 
    500557        user = userpquota.User 
    501558        printer = userpquota.Printer 
     559         
     560        # indicates that a warning needs to be sent 
     561        warned = 0                 
    502562         
    503563        # first we check any group the user is a member of 
    504564        for group in self.storage.getUserGroups(user) : 
    505565            grouppquota = self.storage.getGroupPQuota(group, printer) 
    506             if grouppquota.Exists : 
    507                 action = self.checkGroupPQuota(grouppquota) 
    508                 if action == "DENY" : 
    509                     return action 
    510                  
    511         # then we check the user's own quota 
     566            # for the printer and all its parents 
     567            for gpquota in [ grouppquota ] + grouppquota.ParentPrintersGroupPQuota : 
     568                if gpquota.Exists : 
     569                    action = self.checkGroupPQuota(gpquota) 
     570                    if action == "DENY" : 
     571                        return action 
     572                    elif action == "WARN" :     
     573                        warned = 1 
     574                         
     575        # Then we check the user's account balance 
    512576        # if we get there we are sure that policy is not EXTERNAL 
    513577        (policy, dummy) = self.config.getPrinterPolicy(printer.Name) 
    514578        if user.LimitBy and (user.LimitBy.lower() == "balance") :  
     579            self.logdebug("Checking account balance for user %s" % user.Name) 
    515580            if user.AccountBalance is None : 
    516581                if policy == "ALLOW" : 
     
    519584                    action = "POLICY_DENY" 
    520585                self.logger.log_message(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name)) 
     586                return action         
    521587            else :     
    522588                val = float(user.AccountBalance or 0.0) 
    523589                if val <= 0.0 : 
    524                     action = "DENY" 
     590                    return "DENY" 
    525591                elif val <= self.config.getPoorMan() :     
    526                     action = "WARN" 
     592                    return "WARN" 
    527593                else :     
    528                     action = "ALLOW" 
     594                    return "ALLOW" 
    529595        else : 
    530             if not userpquota.Exists : 
    531                 # Unknown userquota  
    532                 if policy == "ALLOW" : 
    533                     action = "POLICY_ALLOW" 
    534                 else :     
    535                     action = "POLICY_DENY" 
    536                 self.logger.log_message(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) 
     596            # Then check the user quota on current printer and all its parents.                 
     597            policyallowed = 0 
     598            for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota :                
     599                action = self._checkUserPQuota(upquota) 
     600                if action in ("DENY", "POLICY_DENY") : 
     601                    return action 
     602                elif action == "WARN" :     
     603                    warned = 1 
     604                elif action == "POLICY_ALLOW" :     
     605                    policyallowed = 1 
     606            if warned :         
     607                return "WARN" 
     608            elif policyallowed :     
     609                return "POLICY_ALLOW"  
    537610            else :     
    538                 pagecounter = int(userpquota.PageCounter or 0) 
    539                 if userpquota.SoftLimit is not None : 
    540                     softlimit = int(userpquota.SoftLimit) 
    541                     if pagecounter < softlimit : 
    542                         action = "ALLOW" 
    543                     else :     
    544                         if userpquota.HardLimit is None : 
    545                             # only a soft limit, this is equivalent to having only a hard limit 
    546                             action = "DENY" 
    547                         else :     
    548                             hardlimit = int(userpquota.HardLimit) 
    549                             if softlimit <= pagecounter < hardlimit :     
    550                                 now = DateTime.now() 
    551                                 if userpquota.DateLimit is not None : 
    552                                     datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit) 
    553                                 else : 
    554                                     datelimit = now + self.config.getGraceDelay(printer.Name) 
    555                                     userpquota.setDateLimit(datelimit) 
    556                                 if now < datelimit : 
    557                                     action = "WARN" 
    558                                 else :     
    559                                     action = "DENY" 
    560                             else :          
    561                                 action = "DENY" 
    562                 else :         
    563                     if userpquota.HardLimit is not None : 
    564                         # no soft limit, only a hard one. 
    565                         hardlimit = int(userpquota.HardLimit) 
    566                         if pagecounter < hardlimit : 
    567                             action = "ALLOW" 
    568                         else :       
    569                             action = "DENY" 
    570                     else : 
    571                         # Both are unset, no quota, i.e. accounting only 
    572                         action = "ALLOW" 
    573         return action 
    574      
     611                return "ALLOW" 
     612                 
    575613    def externalMailTo(self, cmd, action, user, printer, message) : 
    576614        """Warns the user with an external command."""