the beginning of a proper database

This commit is contained in:
Christoffer Müller Madsen 2017-06-04 19:07:21 +02:00
parent f3f02ab834
commit f4f8d1c316
3 changed files with 59 additions and 5 deletions

View File

@ -53,9 +53,9 @@ class User
end end
def refresh_keys def refresh_keys
File.open(@keypath,"r") do |f| #File.open(@keypath,"r") do |f|
@keys = f.read.split("\n").select{ |line| line[0..2] == "ssh" }.map{ |ks| k = ks.split(" "); SSHKey.new(k[0][4..6], k[1], k[2]) } # @keys = f.read.split("\n").select{ |line| line[0..2] == "ssh" }.map{ |ks| k = ks.split(" "); SSHKey.new(k[0][4..6], k[1], k[2]) }
end #end
end end
def flush def flush
@ -69,6 +69,7 @@ end
def add_user(id) def add_user(id)
user = User.new(id) user = User.new(id)
db.add_user(user)
$users << user $users << user
$user_id_map[id] = user $user_id_map[id] = user
end end
@ -295,8 +296,8 @@ end
################################################ ################################################
reload_users_from_dir #reload_users_from_dir
reload_projects_from_passwd #reload_projects_from_passwd
#remove_project 5 #remove_project 5
#create_project 3 #create_project 3

20
init_sql_db.sql Normal file
View File

@ -0,0 +1,20 @@
CREATE TABLE Users (
name VARCHAR (24) NOT NULL,
passwd VARCHAR (64),
salt VARCHAR (64),
keys VARCHAR (8192),
created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
last_seen DATETIME,
PRIMARY KEY (name)
);
CREATE TABLE Projects (
uid INT NOT NULL,
name VARCHAR (24) NOT NULL,
owner VARCHAR (24) NOT NULL,
users VARCHAR (8192),
PRIMARY KEY (uid),
FOREIGN KEY (owner) REFERENCES Users(name)
);

33
sqlite.rb Normal file
View File

@ -0,0 +1,33 @@
require 'sqlite3'
require 'json'
require 'securerandom'
require 'scrypt'
require_relative 'collab.rb'
$db = SQLite3::Database.new "data/test.db"
def get_user(id)
puts $db.execute("SELECT * FROM Users WHERE name = ?", [id])
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,password)
salt = SecureRandom.base64(32)
hash = SCrypt::Password.create(salt + password)
$db.execute("UPDATE Users SET passwd = ?, salt = ? WHERE name = ?",
[hash,salt,user.id])
end
#user1 = User.new("user1")
#add_user(user1)
#change_user_password(user1,"testtest")