2023-12-11 20:56:09 +00:00
|
|
|
require 'sqlite3'
|
|
|
|
|
2023-12-14 17:30:29 +00:00
|
|
|
DATA_LOCATION = './data'
|
|
|
|
DATABASE_SUBPATH = '/database'
|
|
|
|
DATABASE_NAME = '/auth.db'
|
|
|
|
FILE_STORAGE_LOCATION = '/files'
|
|
|
|
|
2023-12-13 16:02:09 +00:00
|
|
|
def create_new_user(username, password, is_admin)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-12 22:52:36 +00:00
|
|
|
|
|
|
|
db.execute('
|
2023-12-13 16:02:09 +00:00
|
|
|
INSERT INTO users (username, hashed_password, is_admin)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
', [username.downcase, create_password_for_user(password), is_admin])
|
2023-12-12 22:52:36 +00:00
|
|
|
end
|
|
|
|
|
2023-12-13 09:06:34 +00:00
|
|
|
def check_if_user_exists(username)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-13 09:06:34 +00:00
|
|
|
|
2023-12-13 10:02:55 +00:00
|
|
|
result = db.get_first_value('SELECT COUNT(*) FROM users WHERE username = ?', username.downcase)
|
2023-12-12 22:52:36 +00:00
|
|
|
|
2023-12-13 09:06:34 +00:00
|
|
|
if result > 0
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2023-12-12 22:52:36 +00:00
|
|
|
|
2023-12-13 16:02:09 +00:00
|
|
|
def check_if_user_is_admin(user_id)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-13 16:02:09 +00:00
|
|
|
|
|
|
|
result = db.get_first_value('SELECT is_admin FROM users WHERE id = ?', user_id)
|
|
|
|
|
|
|
|
if result == 1
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-12-11 20:56:09 +00:00
|
|
|
def get_user_id(username)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-11 20:56:09 +00:00
|
|
|
|
|
|
|
user_id = db.get_first_value('
|
|
|
|
SELECT id
|
|
|
|
FROM users U
|
|
|
|
WHERE U.username = ?
|
|
|
|
|
2023-12-13 10:02:55 +00:00
|
|
|
', [username.downcase])
|
2023-12-11 20:56:09 +00:00
|
|
|
|
|
|
|
return user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_hashed_password(user_id)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-11 20:56:09 +00:00
|
|
|
|
|
|
|
password_hash = db.get_first_value('
|
|
|
|
SELECT hashed_password
|
|
|
|
FROM users U
|
|
|
|
WHERE U.id = ?
|
|
|
|
|
|
|
|
', [user_id])
|
|
|
|
|
|
|
|
return password_hash
|
|
|
|
end
|
|
|
|
|
2023-12-12 22:52:36 +00:00
|
|
|
def update_user_password(user_id, password)
|
2023-12-14 17:30:29 +00:00
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
2023-12-11 20:56:09 +00:00
|
|
|
|
2023-12-12 22:52:36 +00:00
|
|
|
db.execute('
|
|
|
|
UPDATE users
|
|
|
|
SET hashed_password = ?, salt = ?
|
|
|
|
WHERE id = ?
|
|
|
|
', [hash_password, salt, user_id])
|
2023-12-14 17:30:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_claims (user_id)
|
|
|
|
db = SQLite3::Database.new( DATA_LOCATION + DATABASE_SUBPATH + DATABASE_NAME )
|
|
|
|
results = db.execute('
|
|
|
|
SELECT C.claim
|
|
|
|
FROM users U
|
|
|
|
INNER JOIN user_claims UC ON U.id = UC.user_id
|
|
|
|
INNER JOIN claims C ON UC.claim_id = U.id
|
|
|
|
WHERE u.id = ?
|
|
|
|
', user_id)
|
|
|
|
|
|
|
|
|
|
|
|
claims_list = []
|
|
|
|
|
|
|
|
if results.empty?
|
|
|
|
puts 'No claims found.'
|
|
|
|
else
|
|
|
|
results.each do |column|
|
|
|
|
claim = column[0]
|
|
|
|
claims_list << claim
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return claims_list
|
|
|
|
end
|