Add basic user creation
This commit is contained in:
parent
361d3ebbcb
commit
d7f7f547e4
|
@ -1,7 +1,7 @@
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
displayname TEXT NOT NULL,
|
|
||||||
username TEXT UNIQUE NOT NULL,
|
username TEXT UNIQUE NOT NULL,
|
||||||
|
displayname TEXT NOT NULL,
|
||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
import db
|
||||||
|
import formatter
|
||||||
|
|
||||||
|
proc addUser(username: string, displayname: string, password: string): bool =
|
||||||
|
# Hopefully hashing and stuff will be implemented here at some point
|
||||||
|
return createUser(username=username, displayname=displayname, password=password)
|
||||||
|
|
||||||
|
# Extremely rudimentary, must be fixed later.
|
||||||
|
proc addUserInteractive(): bool =
|
||||||
|
var userConsent: bool = false
|
||||||
|
while userConsent != true:
|
||||||
|
stdout.write "Did not find a user. Create one now? (y/n) "
|
||||||
|
stdout.flushFile()
|
||||||
|
var userInput = readChar(stdin)
|
||||||
|
stdin.flushFile()
|
||||||
|
case userInput
|
||||||
|
of 'y', 'Y':
|
||||||
|
userConsent = true
|
||||||
|
of 'n', 'N':
|
||||||
|
return false
|
||||||
|
else:
|
||||||
|
discard
|
||||||
|
|
||||||
|
stdout.write "Enter a username: "
|
||||||
|
stdout.flushFile()
|
||||||
|
var username = readLine(stdin)
|
||||||
|
stdin.flushFile()
|
||||||
|
|
||||||
|
stdout.write "Enter a display name: "
|
||||||
|
stdout.flushFile()
|
||||||
|
var displayname = readLine(stdin)
|
||||||
|
stdin.flushFile()
|
||||||
|
|
||||||
|
stdout.write "Enter a password: "
|
||||||
|
stdout.flushFile()
|
||||||
|
var password = readLine(stdin)
|
||||||
|
stdin.flushFile()
|
||||||
|
|
||||||
|
if addUser(username=username, displayname=displayname, password=password):
|
||||||
|
logPrint("User " & username & " with displayname " & displayname & " has been created", "Main")
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
|
export addUserInteractive
|
|
@ -3,7 +3,7 @@ import prologue/middlewares/staticfile
|
||||||
import views
|
import views
|
||||||
import formatter
|
import formatter
|
||||||
|
|
||||||
proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) =
|
proc runWebsite(settings: Table[string, bool]) =
|
||||||
let prologueSettings = newSettings(
|
let prologueSettings = newSettings(
|
||||||
debug = settings["debug"]
|
debug = settings["debug"]
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,8 +2,30 @@ import tables
|
||||||
import std/[os, db_sqlite, strutils, streams]
|
import std/[os, db_sqlite, strutils, streams]
|
||||||
import formatter
|
import formatter
|
||||||
|
|
||||||
|
proc connectDb(dbPath: string = "data/blog.db"): DbConn =
|
||||||
|
var db = open(dbPath, "", "", "")
|
||||||
|
return db
|
||||||
|
|
||||||
|
proc createUser(username: string, displayname: string, password: string): bool =
|
||||||
|
try:
|
||||||
|
var db = connectDb()
|
||||||
|
db.exec(sql"INSERT INTO users (username,displayname,password) VALUES (?, ?, ?)", username, displayname, password)
|
||||||
|
db.close()
|
||||||
|
return true
|
||||||
|
except:
|
||||||
|
return false
|
||||||
|
|
||||||
|
proc checkUser(): bool =
|
||||||
|
var db = connectDb()
|
||||||
|
var response = db.getRow(sql"SELECT * FROM users")
|
||||||
|
db.close()
|
||||||
|
if response[0] != "":
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
proc createDb(dbSettings: Table): bool =
|
proc createDb(dbSettings: Table): bool =
|
||||||
if not fileExists(dbSettings["path"]):
|
if not fileExists("data/blog.db"):
|
||||||
var userConsent: bool = false
|
var userConsent: bool = false
|
||||||
while userConsent != true:
|
while userConsent != true:
|
||||||
stdout.write "Did not find database. Create one now? (y/n) "
|
stdout.write "Did not find database. Create one now? (y/n) "
|
||||||
|
@ -20,7 +42,7 @@ proc createDb(dbSettings: Table): bool =
|
||||||
|
|
||||||
let
|
let
|
||||||
schema = readFile(dbSettings["schema_path"])
|
schema = readFile(dbSettings["schema_path"])
|
||||||
db = open(dbSettings["path"], "", "", "")
|
db = connectDb()
|
||||||
for command in schema.split(";"):
|
for command in schema.split(";"):
|
||||||
# Skip "command" if it's just a blank line
|
# Skip "command" if it's just a blank line
|
||||||
if command == "\c\n" or command == "\n":
|
if command == "\c\n" or command == "\n":
|
||||||
|
@ -30,4 +52,4 @@ proc createDb(dbSettings: Table): bool =
|
||||||
db.close()
|
db.close()
|
||||||
return true
|
return true
|
||||||
|
|
||||||
export createDb
|
export createDb, checkUser, createUser
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import tables
|
import tables
|
||||||
import os
|
import os
|
||||||
import modules/[blog, env, db, formatter]
|
import modules/[blog, env, db, formatter, auth]
|
||||||
|
|
||||||
let dataDir: string = "data"
|
let dataDir: string = "data"
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ when isMainModule:
|
||||||
logPrint("Debug mode is enabled", "Warning")
|
logPrint("Debug mode is enabled", "Warning")
|
||||||
|
|
||||||
let dbSettings = {
|
let dbSettings = {
|
||||||
"path": dataDir & "/" & "blog.db",
|
|
||||||
"schema_path": "schema.sql"
|
"schema_path": "schema.sql"
|
||||||
}.toTable()
|
}.toTable()
|
||||||
|
|
||||||
|
@ -25,10 +24,16 @@ when isMainModule:
|
||||||
createDir(dataDir)
|
createDir(dataDir)
|
||||||
|
|
||||||
if not createDb(dbSettings):
|
if not createDb(dbSettings):
|
||||||
logPrint("Failed to prepare database", "Error")
|
logPrint("A database must be created before use", "Error")
|
||||||
quit(QuitFailure)
|
quit(QuitFailure)
|
||||||
|
|
||||||
|
if not checkUser():
|
||||||
|
logPrint("No user in database", "Warning")
|
||||||
|
if not addUserInteractive():
|
||||||
|
logPrint("An initial user must be created before use", "Error")
|
||||||
|
quit(QuitFailure)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
runWebsite(settings=settings, dbSettings=dbSettings)
|
runWebsite(settings=settings)
|
||||||
except:
|
except:
|
||||||
logPrint("Could not run website", "Error")
|
logPrint("Could not run website", "Error")
|
||||||
|
|
Loading…
Reference in New Issue