Exemplo n.º 1
0
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
Exemplo n.º 2
0
 def test_updateAccount(self):
   """test Update accound, this needs the user to log in"""
   self.setAccount()
   htpasswd = os.path.join(self.app.config['etc_dir'], '.htpasswd')
   assert self.users[0] in open(htpasswd).read()
   response = loadJson(self.updateAccount(self.updateUser, self.rcode))
   self.assertEqual(response['code'], 1)
   encode = HtpasswdFile(htpasswd, False)
   encode.update(self.updateUser[0], self.updateUser[1])
   assert self.updateUser[0] in open(htpasswd).read()
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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)