def authenticate_user(self, name, password): """Return the tuple of the user name, description, and blob if the user was successfully authenticated.""" users = self._readonly_copy() try: description, blob, pword = users[name] except KeyError: raise UserStorageError("The name or password is invalid.") if password != pword: raise UserStorageError("The name or password is invalid.") return name, description, blob
def _readonly_copy(self): """Return the user database (which should not be modified).""" try: self._db.lock() try: data = self._db.read() finally: self._db.unlock() except PersistentError, e: raise UserStorageError(str(e))
def add_user(self, name, description, password): """Add a new user.""" self._db.lock() try: users = self._db.read() if users.has_key(name): raise UserStorageError("The user \"%s\" already exists." % name) users[name] = (description, {}, password) self._db.write(users) finally: self._db.unlock()
def delete_user(self, name): """Delete a user.""" self._db.lock() try: users = self._db.read() try: del users[name] except KeyError: raise UserStorageError("The user \"%s\" does not exist." % name) self._db.write(users) finally: self._db.unlock()
def update_password(self, name, password): """Update the password for the given user.""" self._db.lock() try: users = self._db.read() try: description, blob, _ = users[name] except KeyError: raise UserStorageError("The user \"%s\" does not exist." % name) users[name] = (description, blob, password) self._db.write(users) finally: self._db.unlock()