def createNewUser(config, name, passwd): htpasswdfile = os.path.join(config['etc_dir'], '.htpasswd') if os.path.exists(htpasswdfile): htpasswd = HtpasswdFile(htpasswdfile) htpasswd.update(name, passwd) htpasswd.save() return True return False
def checkHtpasswd(config): """XXX:set for backward compatiblity create a htpassword if etc/.users exist""" user = os.path.join(config['etc_dir'], '.users') htpasswdfile = os.path.join(config['etc_dir'], '.htpasswd') if os.path.exists(user) and not os.path.exists(htpasswdfile): data = open(user).read().strip().split(';') htpasswd = HtpasswdFile(htpasswdfile, create=True) htpasswd.update(data[0], data[1]) htpasswd.save() else: return
def saveSession(config, account): """ Save account information for the current user Args: config: Slaprunner configuration session: Flask session account: New session data to be save Returns: True if all goes well or str (error message) if fail """ # XXX Cedric LN hardcoded path for files user = os.path.join(config['etc_dir'], '.users') htpasswdfile = os.path.join(config['etc_dir'], '.htpasswd') backup = False try: if os.path.exists(user): #backup previous data data = open(user).read() open('%s.back' % user, 'w').write(data) backup = True if not account[1]: account[1] = data.split(';')[1] #save new account data open(user, 'w').write((';'.join(account)).encode("utf-8")) # Htpasswd file for cloud9 # XXX Cedric Le N order of account list values suppose to be fixed # Remove former file to avoid outdated accounts if os.path.exists(htpasswdfile): os.remove(htpasswdfile) passwd = HtpasswdFile(htpasswdfile, create=True) passwd.update(account[0], account[1]) passwd.save() return True except Exception as e: try: if backup: os.remove(user) os.rename('%s.back' % user, user) except: pass return str(e)