114 lines
3.2 KiB
Ruby
114 lines
3.2 KiB
Ruby
require_relative '../user'
|
|
require_relative '../project'
|
|
require_relative '../sshkey'
|
|
|
|
require 'sqlite3'
|
|
|
|
module EmacsCollab
|
|
module Database
|
|
|
|
class SQLite
|
|
def initialize(db_name)
|
|
@db = SQLite3::Database.new "#{DATA_PATH}/#{db_name}.db"
|
|
end
|
|
|
|
def get_user(id)
|
|
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 ? Array.new : Marshal.load(row[3])
|
|
|
|
user
|
|
end
|
|
|
|
def get_users
|
|
rows = @db.execute("SELECT * FROM Users")
|
|
|
|
rows.map do |row|
|
|
user = User.new(row[0])
|
|
user.pw_hash = row[1]
|
|
user.salt = row[2]
|
|
user.keys = row[3] == nil ? Array.new : Marshal.load(row[3])
|
|
user
|
|
end
|
|
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 = ?",
|
|
[Marshal.dump(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
|
|
|
|
def get_project(name)
|
|
row = @db.execute("SELECT * FROM Projects WHERE name = ?", [name])[0]
|
|
unless row
|
|
raise EmacsCollab::ProjectNotFoundError, "Project with name #{name} could not be found in database"
|
|
end
|
|
|
|
proj = Project.new()
|
|
proj.uid = row[0]
|
|
proj.id = row[1]
|
|
proj.path = row[2]
|
|
proj.posixname = row[3]
|
|
proj.owner = row[4]
|
|
users = (row[5] == nil ? Array.new : Marshal.load(row[5]))
|
|
users.each do |u|
|
|
EmacsCollab.get_user_by_id(u.id).keys.each do |k|
|
|
proj.add_key(k)
|
|
end
|
|
end
|
|
proj
|
|
end
|
|
|
|
def get_projects
|
|
rows = @db.execute("SELECT * FROM Projects")
|
|
|
|
rows.map do |row|
|
|
proj = Project.new()
|
|
proj.uid = row[0]
|
|
proj.id = row[1]
|
|
proj.path = row[2]
|
|
proj.posixname = row[3]
|
|
proj.owner = row[4]
|
|
users = (row[5] == nil ? Array.new : Marshal.load(row[5]))
|
|
users.each do |u|
|
|
EmacsCollab.get_user_by_id(u).keys.each do |k|
|
|
proj.add_key(k)
|
|
end
|
|
end
|
|
proj.users = row[5]
|
|
proj
|
|
end
|
|
end
|
|
|
|
def add_project(proj)
|
|
@db.execute("INSERT INTO Projects (uid,name,path,posixname,owner,users) VALUES (?,?,?,?,?,?)",
|
|
[ proj.uid, proj.id, proj.path, proj.posixname, "", Marshal.dump(proj.users) ])
|
|
end
|
|
|
|
def update_project(proj)
|
|
@db.execute("UPDATE Projects SET name = ?, path = ?, posixname = ?, owner = ?, users = ? WHERE uid = ?",
|
|
[ proj.id, proj.path, proj.posixname, "", Marshal.dump(proj.users), proj.uid ])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
#user1 = User.new("user1")
|
|
#add_user(user1)
|
|
#change_user_password(user1,"testtest")
|