import subprocess import pwd import time IOTA_DAY=60*60*24 IOTA_WEEK=IOTA_DAY*7 IOTA_MONTH=60*60*24*30 class PrintaServer: def __init__(self): self.users={} self.printers={} self.saving=False self.var={} self.var["users"]=self.users self.var["printers"]=self.printers #def init def startup(self,options): global master_password if "PRINTASERVER" in objects["VariablesManager"].variables: var=objects["VariablesManager"].get_variable("PRINTASERVER",False,False,master_password) self.users=var["users"] self.printers=var["printers"] self.var["users"]=self.users self.var["printers"]=self.printers else: self.save_variable() #def startup # ###### PRIVATE FUNCTIONS ############################################################################ def save_variable(self): '''private function''' while self.saving: time.sleep(2) self.saving=True try: if "PRINTASERVER" in objects["VariablesManager"].variables: self.var["users"]=self.users self.var["printers"]=self.printers objects["VariablesManager"].set_variable("PRINTASERVER",self.var) else: objects["VariablesManager"].add_variable("PRINTASERVER",self.var,"","PrintaServer accounts database",["Printa"],False,True) except Exception as e: print e self.saving=False #def save_variable def add_printer(self,printer): '''private function''' try: if printer["name"] not in self.printers: if "device_uri" not in printer: return False name=printer["name"] printer.pop("name") printer["quota"]=200 printer["quota_enabled"]=False printer["free_pass"]=True printer["allowed_users"]=["*"] printer["added_date"]=time.time() printer["quota_configured_date"]=None printer["autoload_each"]=IOTA_MONTH printer["autoload_amount"]=200 printer["password"]=None printer["jobs"]=[] printer["locked"]=False self.printers[name]=printer return True else: return False except: return False #def add_printer def add_user(self,uid,user): '''private function''' uid=str(uid) if uid not in self.users: #print "Adding user..." self.users[uid]={} self.users[uid]["user"]=user self.users[uid]["quota"]=20 self.users[uid]["free_pass"]=False self.users[uid]["first_print"]=None self.users[uid]["autoload_each"]=IOTA_MONTH self.users[uid]["autoload_amount"]=20 self.users[uid]["password"]=None self.users[uid]["use_password"]=False self.users[uid]["jobs"]=[] self.users[uid]["locked"]=False self.save_variable() return True return False #def add_user def update_user_quota(self,uid,value): '''Private function''' uid=str(uid) if uid in self.users: self.users[uid]["quota"]+=value #print self.users[uid]["quota"] self.save_variable() else: pass #print ":(" return True #def update_user_quota def get_uid(self,name): '''private_function''' try: for user in pwd.getpwall(): if name==user.pw_name: return user.pw_uid except Exception as e: return None return None #def get_uid def get_name(self,uid): '''private_function''' try: for user in pwd.getpwall(): if uid==user.pw_uid: return user.pw_name except Exception as e: return None return None #def get_name def is_valid_user(self,uid): '''private function''' try: uid=int(uid) for user in pwd.getpwall(): if uid==user.pw_uid: return [True,user.pw_name] except Exception as e: return [False,str(e)] return [False,None] #def is_valid_user # ###### END OF PRIVATE FUNCTIONS ######################################################################### def add_job_history(self,user,job): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid in self.users: self.users[uid]["jobs"].insert(0,job) self.save_variable() return True return False #def add_job def get_user_history(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid in self.users: return self.users[uid]["jobs"] return [] #def get_user_history def get_user_quota(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid in self.users: return self.users[uid]["quota"] ret=self.is_valid_user(uid) if ret[0]: self.add_user(uid,ret[1]) return self.get_user_quota(uid) return 0 #def get_user_quota def set_user_quota(self,user,value): if type(value)==type(1): uid=self.get_uid(user) if uid!=None: uid=str(uid) ret,name=self.is_valid_user(uid) if ret: if uid not in self.users: self.add_user(uid,name) self.users[uid]["quota"]=value self.save_variable() return True return False def add_user_quota(self,user,value): uid=self.get_uid(user) if uid!=None: uid=str(uid) return self.update_user_quota(uid,value) return False #def add_user_quota def substract_user_quota(self,user,value): uid=self.get_uid(user) if uid!=None: uid=str(uid) return self.update_user_quota(uid,value*-1) return False #def substract_user_quota def check_if_printable(self,job): # # user locked # user quota # printer locked # printer quota # user_name=job["user"] uid=self.get_uid(user_name) if uid!=None: uid=str(uid) # if it already exists, it does nothing self.add_user(uid,user_name) if self.users[uid]["locked"]: return [False,-2] if self.users[uid]["free_pass"]: return [True,0] user_quota=self.get_user_quota(user_name) if job["pages"] > user_quota: return [False,-3] return [True,0] else: return [False,-1] #def check_if_printable def is_password_needed(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) ret,name=self.is_valid_user(uid) if ret: if uid not in self.users: self.add_user(uid,name) return self.users[uid]["use_password"] return False #def is_password_needed def validate_password(self,user,password): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid in self.users: if self.users[uid]["password"]==password: return True return False #def validate_password def get_user_list(self): user_list=[] for user in pwd.getpwall(): if user.pw_uid >= 1000 and user.pw_uid < 65534: user_list.append((user.pw_name,user.pw_gecos)) return user_list #def get_user_list def get_user_info(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid not in self.users: self.add_user(uid,user) return (True,uid,self.users[uid]) return (False,None,None) #def get_user_info def set_locked(self,user,status): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid not in self.users: self.add_user(uid,user) self.users[uid]["locked"]=status self.save_variable() return True return False def is_locked(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid not in self.users: self.add_user(uid,user) return self.users[uid]["locked"] return True #def is_locked def has_freepass(self,user): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid not in self.users: self.add_user(uid,user) return self.users[uid]["free_pass"] return False #def has_freepass def set_freepass(self,user,status): uid=self.get_uid(user) if uid!=None: uid=str(uid) if uid not in self.users: self.add_user(uid,user) self.users[uid]["free_pass"]=status self.save_variable() return True return False