emacs-collab/lib/emacscollab/database/sqlite.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

require_relative '../user'
require_relative '../project'
require_relative '../sshkey'
require 'sqlite3'
require 'json'
module EmacsCollab
module Database
class SQLite
def initialize(db_name)
@db = SQLite3::Database.new "#{DATA_PATH}/#{db_name}.db"
end
def get_user(id)
# TODO
row = @db.execute("SELECT * FROM Users WHERE name = ?", [id])[0]
unless row
raise UserNotFoundError, "User with id #{id} could not be found in database"
end
user = User.new(row[0])
user.pw_hash = row[1]
user.salt = row[2]
user.keys = row[3] == nil ? nil : JSON.parse(row[3])
user
end
def add_user(user)
@db.execute("INSERT INTO Users (name) VALUES (?)",
[user.id])
end
def update_user(user)
@db.execute("UPDATE Users SET keys = ? WHERE name = ?",
[JSON.generate(user.keys),user.id])
end
def change_user_password(user,hash,salt)
@db.execute("UPDATE Users SET passwd = ?, salt = ? WHERE name = ?",
[hash,salt,user.id])
end
end
end
end
#user1 = User.new("user1")
#add_user(user1)
#change_user_password(user1,"testtest")