Changeset 1152

Show
Ignore:
Timestamp:
10/08/03 23:41:38 (21 years ago)
Author:
jalet
Message:

External policies for printers works !
We can now auto-add users on first print, and do other useful things if needed.

Location:
pykota/trunk
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pykota

    r1144 r1152  
    2424# 
    2525# $Log$ 
     26# Revision 1.42  2003/10/08 21:41:38  jalet 
     27# External policies for printers works ! 
     28# We can now auto-add users on first print, and do other useful things if needed. 
     29# 
    2630# Revision 1.41  2003/10/07 09:07:27  jalet 
    2731# Character encoding added to please latest version of Python 
     
    252256            return -1 
    253257             
     258def format_commandline(prt, usr, cmdline) :             
     259    """Passes printer and user names on the command line.""" 
     260    printer = prt.Name 
     261    user = usr.Name 
     262    # we don't want the external command's standard  
     263    # output to break the print job's data, but we 
     264    # want to keep its standard error 
     265    return "%s >/dev/null" % (cmdline % locals()) 
     266 
    254267def main(thefilter) :     
    255268    """Do it, and do it right !""" 
     
    268281        thefilter.logger.log_message(_("Printer %s not registered in the PyKota system") % thefilter.printername, "warn") 
    269282    else :     
    270         user = thefilter.storage.getUser(thefilter.username) 
    271         if not user.Exists : 
    272             # The user is unknown from the Quota Storage perspective 
    273             # Depending on the default policy for this printer, we 
    274             # either let the job pass through or reject it, but we 
    275             # log a message in any case. 
    276             policy = thefilter.config.getPrinterPolicy(thefilter.printername) 
    277             if policy == "ALLOW" : 
    278                 action = "POLICY_ALLOW" 
     283        for compteur in range(2) : 
     284            user = thefilter.storage.getUser(thefilter.username) 
     285            if user.Exists : 
     286                break 
    279287            else :     
    280                 action = "POLICY_DENY" 
    281             thefilter.logger.log_message(_("User %s not registered in the PyKota system, applying default policy (%s) for printer %s") % (thefilter.username, action, thefilter.printername), "warn") 
    282             if action == "POLICY_DENY" : 
    283                 return thefilter.removeJob() 
    284         else : 
     288                # The user is unknown from the Quota Storage perspective 
     289                # Depending on the default policy for this printer, we 
     290                # either let the job pass through or reject it, but we 
     291                # log a message in any case. 
     292                (policy, args) = thefilter.config.getPrinterPolicy(thefilter.printername) 
     293                if policy == "ALLOW" : 
     294                    action = "POLICY_ALLOW" 
     295                elif policy == "EXTERNAL" :     
     296                    commandline = format_commandline(printer, user, args) 
     297                    thefilter.logger.log_message(_("User %s not registered in the PyKota system, applying external policy (%s) for printer %s") % (thefilter.username, commandline, thefilter.printername), "info") 
     298                    if os.system(commandline) : 
     299                        thefilter.logger.log_message(_("External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files.") % (commandline, thefilter.printername), "error") 
     300                        return thefilter.removeJob() 
     301                    else :     
     302                        # here we try a second time, because the goal 
     303                        # of the external action was to add the user 
     304                        # in the database. 
     305                        continue  
     306                else :     
     307                    action = "POLICY_DENY" 
     308                thefilter.logger.log_message(_("User %s not registered in the PyKota system, applying default policy (%s) for printer %s") % (thefilter.username, action, thefilter.printername), "warn") 
     309                if action == "POLICY_DENY" : 
     310                    return thefilter.removeJob() 
     311                # when we get there, the printer policy allows the job to pass  
     312                break     
     313                     
     314        # if user exists, do accounting 
     315        if user.Exists : 
    285316            # Now does the accounting and act depending on the result 
    286317            action = thefilter.accounter.doAccounting(printer, user) 
    287              
     318         
    288319            # if not allowed to print then die, else proceed. 
    289320            if action == "DENY" : 
    290321                # No, just die cleanly 
    291322                return thefilter.removeJob() 
     323        elif policy == "EXTERNAL" :                 
     324            thefilter.logger.log_message(_("External policy %s for printer %s couldn't add user %s. Job rejected.") % (commandline, thefilter.printername, thefilter.username), "error") 
     325            return thefilter.removeJob() 
    292326         
    293327    # pass the job untouched to the underlying layer 
  • pykota/trunk/conf/pykota.conf.sample

    r1149 r1152  
    300300 
    301301# Default policy for inexistant users (e.g. root) 
    302 # either allow or deny 
     302# either allow or deny or external(some command here) 
    303303# This value can be set either globally or per printer or both. 
    304304# If both are defined, the printer option has priority. 
     
    309309#     charged to the next person who prints on the same printer. 
    310310# There's no policy wrt inexistant groups, they are ignored. 
     311# 
     312# external policy can be used to launch any external command of your choice, 
     313# for example to automatically add the user to the quota storage  
     314# if he is unknown. Example : 
     315#  
     316#       policy: external(/usr/bin/edpykota --add --printer %(printer)s --softlimit 50 --hardlimit 60 %(user)s)  
     317# 
     318#       NB : '%(user)s' and '%(printer)s' will be automatically replaced  
     319#            by the user and printer names.  
     320# 
     321# Of course you can launch any command of your choice with this, e.g. : 
     322# 
     323#       policy: external(/usr/local/bin/myadminscript.sh %(user)s) 
     324# 
    311325policy: deny 
    312326 
  • pykota/trunk/NEWS

    r1149 r1152  
    2222PyKota NEWS : 
    2323 
     24    - 1.15alpha9 : 
     25     
     26        - External policy can be used to auto-add unknown users  
     27          and any other interesting things you may want to do.  
     28          WARNING : You can't auto-add printers for now.  
     29          They have to be created manually before. 
     30          Look at sample configuration file for examples. 
     31           
    2432    - 1.15alpha8 : 
    2533     
  • pykota/trunk/po/en/pykota.po

    r1116 r1152  
    2121# 
    2222# $Log$ 
     23# Revision 1.35  2003/10/08 21:41:38  jalet 
     24# External policies for printers works ! 
     25# We can now auto-add users on first print, and do other useful things if needed. 
     26# 
    2327# Revision 1.34  2003/08/18 16:20:59  jalet 
    2428# Improvement of the printing system detection code. 
     
    427431msgstr "" 
    428432 
     433msgid "Invalid policy %s for printer %s" 
     434msgstr "" 
     435 
     436msgid "User %s not registered in the PyKota system, applying external policy (%s) for printer %s" 
     437msgstr "" 
     438 
     439msgid "External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files." 
     440msgstr "" 
     441 
     442msgid "External policy %s for printer %s couldn't add user %s. Job rejected." 
     443msgstr "" 
     444 
  • pykota/trunk/po/fr/pykota.po

    r1116 r1152  
    2121# 
    2222# $Log$ 
     23# Revision 1.34  2003/10/08 21:41:38  jalet 
     24# External policies for printers works ! 
     25# We can now auto-add users on first print, and do other useful things if needed. 
     26# 
    2327# Revision 1.33  2003/08/18 16:20:59  jalet 
    2428# Improvement of the printing system detection code. 
     
    437441msgstr "Syst� d'impression inconnu, args=%s" 
    438442 
     443msgid "Invalid policy %s for printer %s" 
     444msgstr "Option policy %s invalide pour l'imprimante %s" 
     445 
     446msgid "User %s not registered in the PyKota system, applying external policy (%s) for printer %s" 
     447msgstr "Utilisateur %s non enregistr�ans le syst� PyKota, action externe (%s) appliqu�sur l'imprimante %s" 
     448 
     449msgid "External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files." 
     450msgstr "L'action externe %s sur l'imprimante %s a produit une erreur. Job rejet�Merci de v�fier les fichiers de configuration de PyKota." 
     451 
     452msgid "External policy %s for printer %s couldn't add user %s. Job rejected." 
     453msgstr "L'action externe %s sur l'imprimante %s n'a pas pu ajouter l'utilisateur %s. Job rejet� 
     454 
  • pykota/trunk/po/pykota.pot

    r1116 r1152  
    2121# 
    2222# $Log$ 
     23# Revision 1.35  2003/10/08 21:41:38  jalet 
     24# External policies for printers works ! 
     25# We can now auto-add users on first print, and do other useful things if needed. 
     26# 
    2327# Revision 1.34  2003/08/18 16:20:59  jalet 
    2428# Improvement of the printing system detection code. 
     
    427431msgstr "" 
    428432 
     433msgid "Invalid policy %s for printer %s" 
     434msgstr "" 
     435 
     436msgid "User %s not registered in the PyKota system, applying external policy (%s) for printer %s" 
     437msgstr "" 
     438 
     439msgid "External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files." 
     440msgstr "" 
     441 
     442msgid "External policy %s for printer %s couldn't add user %s. Job rejected." 
     443msgstr "" 
     444 
  • pykota/trunk/pykota/config.py

    r1148 r1152  
    2222# 
    2323# $Log$ 
     24# Revision 1.39  2003/10/08 21:41:38  jalet 
     25# External policies for printers works ! 
     26# We can now auto-add users on first print, and do other useful things if needed. 
     27# 
    2428# Revision 1.38  2003/10/07 22:06:05  jalet 
    2529# Preliminary code to disable job history 
     
    320324    def getPrinterPolicy(self, printer) :     
    321325        """Returns the default policy for the current printer.""" 
    322         validpolicies = [ "ALLOW", "DENY" ]      
    323         try : 
    324             policy = self.getPrinterOption(printer, "policy").upper() 
    325         except PyKotaConfigError :     
    326             policy = "DENY" 
    327         if policy not in validpolicies : 
    328             raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) 
    329         return policy 
     326        validpolicies = [ "ALLOW", "DENY", "EXTERNAL" ]      
     327        try : 
     328            fullpolicy = self.getPrinterOption(printer, "policy") 
     329        except PyKotaConfigError :     
     330            return ("DENY", None) 
     331        else :     
     332            try : 
     333                policy = [x.strip() for x in fullpolicy.split('(', 1)] 
     334            except ValueError :     
     335                raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printer) 
     336            if len(policy) == 1 :     
     337                policy.append("") 
     338            (policy, args) = policy     
     339            if args.endswith(')') : 
     340                args = args[:-1] 
     341            policy = policy.upper()     
     342            if (policy == "EXTERNAL") and not args : 
     343                raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printer) 
     344            if policy not in validpolicies : 
     345                raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) 
     346            return (policy, args) 
    330347         
    331348    def getSMTPServer(self) :     
  • pykota/trunk/pykota/tool.py

    r1144 r1152  
    2222# 
    2323# $Log$ 
     24# Revision 1.53  2003/10/08 21:41:38  jalet 
     25# External policies for printers works ! 
     26# We can now auto-add users on first print, and do other useful things if needed. 
     27# 
    2428# Revision 1.52  2003/10/07 09:07:28  jalet 
    2529# Character encoding added to please latest version of Python 
     
    435439                 
    436440        # then we check the user's own quota 
    437         policy = self.config.getPrinterPolicy(printer.Name) 
     441        # if we get there we are sure that policy is not EXTERNAL 
     442        (policy, dummy) = self.config.getPrinterPolicy(printer.Name) 
    438443        if user.LimitBy and (user.LimitBy.lower() == "balance") :  
    439444            if user.AccountBalance is None : 
  • pykota/trunk/pykota/version.py

    r1149 r1152  
    2222# 
    2323 
    24 __version__ = "1.15alpha8_unofficial" 
     24__version__ = "1.15alpha9_unofficial" 
    2525 
    2626__doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng."""