diff --git a/collab.rb b/collab.rb index ecc7201..0be9a54 100644 --- a/collab.rb +++ b/collab.rb @@ -53,9 +53,9 @@ class User end def refresh_keys - 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]) } - end + #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]) } + #end end def flush @@ -69,6 +69,7 @@ end def add_user(id) user = User.new(id) + db.add_user(user) $users << user $user_id_map[id] = user end @@ -295,8 +296,8 @@ end ################################################ -reload_users_from_dir -reload_projects_from_passwd +#reload_users_from_dir +#reload_projects_from_passwd #remove_project 5 #create_project 3 diff --git a/init_sql_db.sql b/init_sql_db.sql new file mode 100644 index 0000000..7035e57 --- /dev/null +++ b/init_sql_db.sql @@ -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) +); diff --git a/sqlite.rb b/sqlite.rb new file mode 100644 index 0000000..575427f --- /dev/null +++ b/sqlite.rb @@ -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")