def get_updatedby(usr): """Get username of who is performing the action.""" if form.getfirst('updatedby'): usr.updatedby = form.getfirst('updatedby') else: raise RBFatalError('Updated by must be given') if usr.updatedby == 'root': raise RBFatalError('root not allowed for updatedby') udb.check_updatedby(usr.updatedby)
def cmd(self, cmd): """Run given command and raise a RBError exception returning the command output if command exit status is non zero.""" output, status = self.runcmd(cmd) if status: raise RBFatalError("Command '%s' failed.\n%s" % (cmd, output))
def check_accountfree(cls, usr): """Raise RBFatalError if given account name is not free i.e. has a home directory.""" if os.path.exists(usr.homeDirectory): raise RBFatalError( "Account '%s' already exists (has a home directory)" % usr.uid)
def get_cardid(usr): """Set usr.id to DCU ID number in cardid field. The ID will either be the 8 digit number when entered manually or the 13 digit code produced by barcode and magnetic readers of the form xxIDNUMBERnnn with possible start and/or end sentinel characters such as ';' or '?'. Some readers will output a code number at the start, (to indicate the type of barcode or something) so we assume the 13 digit number is at the end (i.e. right-hand side) of the string. If invalid input is given, raises RBFatalError. NOTE: It is up to the caller to check if usr.id has been set, get_cardid does not require it to be set. """ usr.id = form.getfirst('cardid') if usr.id != None: res = re.search(r'\d{2}(\d{8})\d{3}\D*$', usr.id) if res: usr.id = int(res.group(1)) return res = re.search(r'^(\d{8})$', usr.id) if res: usr.id = int(usr.id) return raise RBFatalError('Invalid ID number/card reader input')
def get_birthday(usr): """Get (optional) birthday.""" if form.getfirst('byear') or form.getfirst('bmonth') or form.getfirst( 'bday'): if not (form.getfirst('byear') and form.getfirst('bmonth') and form.getfirst('bday')): raise RBFatalError('Incomplete birthday given') try: usr.birthday = '%.4d-%0.2d-%0.2d' % (int( form.getfirst('byear')), int( form.getfirst('bmonth')), int(form.getfirst('bday'))) except ValueError: raise RBFatalError('Invalid birthday given') udb.check_birthday(usr)
def get_year(usr): """Get DCU year.""" if not usr.usertype in ('member', 'committe'): return if form.getfirst('year'): usr.year = form.getfirst('year') else: raise RBFatalError('Year must be given')
def get_name(usr): """Get name.""" if form.getfirst('cn'): usr.cn = form.getfirst('cn') else: raise RBFatalError('Name must be given') udb.check_name(usr)
def get_course(usr): """Get DCU course.""" if not usr.usertype in ('member', 'committe'): return if form.getfirst('course'): usr.course = form.getfirst('course') else: raise RBFatalError('Course must be given')
def get_id(usr): """Get DCU ID.""" if usr.usertype in rbconfig.usertypes_dcu: if form.getfirst('id'): usr.id = int(form.getfirst('id')) else: raise RBFatalError('ID must be given') udb.check_id(usr)
def get_username(usr): """Get an existing username.""" if form.getfirst('uid'): usr.uid = form.getfirst('uid') else: raise RBFatalError('Username must be given') udb.check_username(usr.uid) udb.check_user_byname(usr.uid)
def get_email(usr): """Get alternative email address.""" if form.getfirst('altmail'): usr.altmail = form.getfirst('altmail') else: raise RBFatalError('Email must be given') try: udb.check_email(usr) except RBWarningError as e: error(e)
def get_years_paid(usr): """Get years paid.""" if not usr.usertype in rbconfig.usertypes_paying: return if form.getfirst('yearsPaid'): usr.yearsPaid = int(form.getfirst('yearsPaid')) else: raise RBFatalError('Years paid must be given') udb.check_years_paid(usr)
def get_usertype(usr): """Get usertype.""" usr.oldusertype = usr.usertype if form.getfirst('usertype'): usr.usertype = form.getfirst('usertype') else: raise RBFatalError('Usertype must be given') udb.check_usertype(usr.usertype)
def card(): """Process input from card reader form. Mode will be switched to add or renew as appropriate if there were no problems with user input.""" get_updatedby(usr) get_cardid(usr) newmode = None # We have an ID, is it a newbie or a renewal? # if usr.id != None: try: udb.check_user_byid(usr.id) except RBError: # Doesn't exist, must be new user. newmode = 'add' else: # Exists, must be renewal. newmode = 'renew' elif form.getfirst('dummyid'): get_dummyid(usr) newmode = 'add' elif form.getfirst('uid'): usr.uid = form.getfirst('uid') udb.check_username(usr.uid) try: udb.check_user_byname(usr.uid) except RBError: # Doesn't exist, must be new user. newmode = 'add' else: # Exists, must be renewal. newmode = 'renew' else: raise RBFatalError("DCU Card ID, username or dummy ID must be given") if newmode == 'add': if usr.id != None: udb.get_userinfo_new(usr) udb.get_userdefaults_new(usr) elif newmode == 'renew': curusr = RBUser() udb.get_userinfo_renew(usr, curusr, override=1) udb.check_unpaid(curusr) udb.get_userdefaults_renew(usr) if newmode: opt.mode = newmode
def search(): """Search user and/or DCU databases.""" global okay if form.getfirst('uid'): uid = form.getfirst('uid') res = udb.search_users_byusername(uid) print( "<p align=center>User database search for username '%s' - %d match%s</p>" % (uid, len(res), len(res) != 1 and 'es' or '')) show_search_results(res) okay = 1 elif 'id' in form or 'cn' in form: id = form.getfirst('id') cn = form.getfirst('cn') if id != None: res = udb.search_users_byid(id) print( "<p align=center>User database search for ID '%s' - %d match%s</p>" % (id, len(res), len(res) != 1 and 'es' or '')) else: res = udb.search_users_byname(cn) print( "<p align=center>User database search for name '%s' - %d match%s</p>" % (cn, len(res), len(res) != 1 and 'es' or '')) show_search_results(res) if id != None: res = udb.search_dcu_byid(id) print( "<p align=center>DCU database search for ID '%s' - %d match%s</p>" % (id, len(res), len(res) != 1 and 'es' or '')) else: res = udb.search_dcu_byname(cn) print( "<p align=center>DCU database search for name '%s' - %d match%s</p>" % (cn, len(res), len(res) != 1 and 'es' or '')) show_search_results(res) okay = 1 else: raise RBFatalError('No search term given!')
def get_newusername(usr): """Get a new (free) username.""" if opt.mode == 'add': if form.getfirst('uid'): usr.uid = form.getfirst('uid') else: if form.getfirst('newuid'): usr.uid = form.getfirst('newuid') # New username is optional for renewals but compulsory for all other # modes that require it (add, rename, freename). # if opt.mode == 'renew' and not usr.uid: return if not usr.uid: raise RBFatalError('New username must be given') try: udb.check_username(usr.uid) udb.check_userfree(usr.uid) except RBWarningError as e: error(e)
def rename(self, oldusr, newusr): """Rename an account. Requires: oldusr.uid, oldusr.homeDirectory, newusr.uid, newusr.homeDirectory. """ # fixme Should check this before we rename user in ldap, have a # rbaccount.check_userfree? There should never be a file or # directory in /home or /webtree that doesn't belong to an # existing user. if os.path.exists(newusr.homeDirectory): if not os.path.isdir(newusr.homeDirectory): try: self.wrapper(os.unlink, newusr.homeDirectory) except OSError: raise RBFatalError( ("New home directory '%s' already exists," " could not unlink existing file.") % newusr.homeDirectory) else: raise RBFatalError("New home directory '%s' already exists." % newusr.homeDirectory) oldwebtree = rbconfig.gen_webtree(oldusr.uid) newwebtree = rbconfig.gen_webtree(newusr.uid) try: self.wrapper(os.rename, oldusr.homeDirectory, newusr.homeDirectory) except OSError as err: raise RBFatalError("Could not rename home directory [%s]" % err) try: self.wrapper(os.rename, oldwebtree, newwebtree) except OSError as err: raise RBFatalError("Could not rename webtree directory [%s]" % err) # Remove and then attempt to rename webtree symlink. # webtreelink = os.path.join(newusr.homeDirectory, 'public_html') try: self.wrapper(os.unlink, webtreelink) except OSError: pass if not os.path.exists(webtreelink): self.wrapper(os.symlink, newwebtree, webtreelink) # Rename any extra files that may belong to a user. # oldfiles = rbconfig.gen_extra_user_files(oldusr.uid) newfiles = rbconfig.gen_extra_user_files(newusr.uid) for i, _ in enumerate(oldfiles): oldf = oldfiles[i] newf = newfiles[i] try: if os.path.isfile(oldf): self.wrapper(os.rename, oldf, newf) except OSError as err: raise RBFatalError("Could not rename '%s' to '%s' [%s]" % (oldf, newf, err)) # fixme # Rename their subscription to announce lists in case an email # alias isn't put in for them or is later removed. # self.list_delete('announce-redbrick', "*****@*****.**" % oldusr.uid) self.list_delete('redbrick-newsletter', "*****@*****.**" % oldusr.uid) self.list_add('announce-redbrick', "*****@*****.**" % newusr.uid) self.list_add('redbrick-newsletter', "*****@*****.**" % newusr.uid)
def check_account_byname(cls, usr): """Raise RBFatalError if given account does not exist.""" if not os.path.exists(usr.homeDirectory): raise RBFatalError( "Account '%s' does not exist (no home directory)" % usr.uid)
def convert(self, oldusr, newusr): """Convert account to a new usertype (Unix group).""" if oldusr.usertype == newusr.usertype: return # Do supplementary group shit in rbuserdb. # # if rbconfig.convert_primary_groups.has_key(usertype): # group = rbconfig.convert_primary_groups[usertype] # else: # group = usertype # if rbconfig.convert_extra_groups.has_key(usertype): # groups = '-G ' + rbconfig.convert_extra_groups[usertype] # else: # groups = '' if newusr.usertype == 'committe' and oldusr.usertype not in ( 'member', 'staff', 'committe'): raise RBFatalError( "Non-members cannot be converted to committee group") if os.path.exists(newusr.homeDirectory): if not os.path.isdir(newusr.homeDirectory): try: self.wrapper(os.unlink, newusr.homeDirectory) except OSError: raise RBFatalError( ("New home directory '%s' already exists, " "could not unlink existing file.") % newusr.homeDirectory) else: raise RBFatalError("New home directory '%s' already exists." % newusr.homeDirectory) # Rename home directory. # try: self.wrapper(os.rename, oldusr.homeDirectory, newusr.homeDirectory) except BaseException: raise RBFatalError("Could not rename home directory") # Change the home directory and webtree ownership to the new # group. -h on Solaris chgrp makes sure to change the symbolic # links themselves not the files they point to - very # important!! # self.cmd("%s -Rh %s %s %s" % (rbconfig.command_chgrp, newusr.gidNumber, self.shquote(newusr.homeDirectory), self.shquote(rbconfig.gen_webtree(oldusr.uid)))) # Add/remove from committee mailing list as appropriate. # if newusr.usertype == 'committe': self.list_add('committee', "*****@*****.**" % oldusr.uid) elif oldusr.usertype == 'committe': self.list_delete('committee', "*****@*****.**" % oldusr.uid) # Add to admin list. Most admins stay in the root group for a while # after leaving committee, so removal can be done manually later. # if newusr.usertype == 'admin': self.list_add('rb-admins', "*****@*****.**" % oldusr.uid)