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.

Files:
1 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