Skip to content

Commit

Permalink
Add basic account level management
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed Aug 12, 2019
1 parent 326f04d commit 0c4d75f
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 117 deletions.
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DB interface {
GetBookmark(id int, url string) (model.Bookmark, bool)

// SaveAccount saves new account in database
SaveAccount(username, password string) error
SaveAccount(model.Account) error

// GetAccounts fetch list of account (without its password) with matching keyword.
GetAccounts(keyword string) ([]model.Account, error)
Expand Down
22 changes: 12 additions & 10 deletions internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func OpenMySQLDatabase(username, password, dbName string) (mysqlDB *MySQLDatabas

// Create tables
tx.MustExec(`CREATE TABLE IF NOT EXISTS account(
id INT(11) NOT NULL,
username VARCHAR(250) NOT NULL,
password BINARY(80) NOT NULL,
id INT(11) NOT NULL,
username VARCHAR(250) NOT NULL,
password BINARY(80) NOT NULL,
owner TINYINT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
UNIQUE KEY account_username_UNIQUE (username))`)

Expand Down Expand Up @@ -509,19 +510,20 @@ func (db *MySQLDatabase) GetBookmark(id int, url string) (model.Bookmark, bool)
}

// SaveAccount saves new account to database. Returns error if any happened.
func (db *MySQLDatabase) SaveAccount(username, password string) (err error) {
func (db *MySQLDatabase) SaveAccount(account model.Account) (err error) {
// Hash password with bcrypt
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 10)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(account.Password), 10)
if err != nil {
return err
}

// Insert account to database
_, err = db.Exec(`INSERT INTO account
(username, password) VALUES (?, ?)
(username, password, owner) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
password = VALUES(password)`,
username, hashedPassword)
password = VALUES(password),
owner = VALUES(owner)`,
account.Username, hashedPassword, account.Owner)

return err
}
Expand All @@ -530,7 +532,7 @@ func (db *MySQLDatabase) SaveAccount(username, password string) (err error) {
func (db *MySQLDatabase) GetAccounts(keyword string) ([]model.Account, error) {
// Create query
args := []interface{}{}
query := `SELECT id, username FROM account WHERE 1`
query := `SELECT id, username, owner FROM account WHERE 1`

if keyword != "" {
query += " AND username LIKE ?"
Expand All @@ -554,7 +556,7 @@ func (db *MySQLDatabase) GetAccounts(keyword string) ([]model.Account, error) {
func (db *MySQLDatabase) GetAccount(username string) (model.Account, bool) {
account := model.Account{}
db.Get(&account, `SELECT
id, username, password FROM account WHERE username = ?`,
id, username, password, owner FROM account WHERE username = ?`,
username)

return account, account.ID != 0
Expand Down
16 changes: 9 additions & 7 deletions internal/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func OpenSQLiteDatabase(databasePath string) (sqliteDB *SQLiteDatabase, err erro
id INTEGER NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL,
owner INTEGER NOT NULL DEFAULT 0,
CONSTRAINT account_PK PRIMARY KEY(id),
CONSTRAINT account_username_UNIQUE UNIQUE(username))`)

Expand Down Expand Up @@ -521,19 +522,20 @@ func (db *SQLiteDatabase) GetBookmark(id int, url string) (model.Bookmark, bool)
}

// SaveAccount saves new account to database. Returns error if any happened.
func (db *SQLiteDatabase) SaveAccount(username, password string) (err error) {
func (db *SQLiteDatabase) SaveAccount(account model.Account) (err error) {
// Hash password with bcrypt
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 10)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(account.Password), 10)
if err != nil {
return err
}

// Insert account to database
_, err = db.Exec(`INSERT INTO account
(username, password) VALUES (?, ?)
(username, password, owner) VALUES (?, ?, ?)
ON CONFLICT(username) DO UPDATE SET
password = ?`,
username, hashedPassword, hashedPassword)
password = ?, owner = ?`,
account.Username, hashedPassword, account.Owner,
hashedPassword, account.Owner)

return err
}
Expand All @@ -542,7 +544,7 @@ func (db *SQLiteDatabase) SaveAccount(username, password string) (err error) {
func (db *SQLiteDatabase) GetAccounts(keyword string) ([]model.Account, error) {
// Create query
args := []interface{}{}
query := `SELECT id, username FROM account WHERE 1`
query := `SELECT id, username, owner FROM account WHERE 1`

if keyword != "" {
query += " AND username LIKE ?"
Expand All @@ -566,7 +568,7 @@ func (db *SQLiteDatabase) GetAccounts(keyword string) ([]model.Account, error) {
func (db *SQLiteDatabase) GetAccount(username string) (model.Account, bool) {
account := model.Account{}
db.Get(&account, `SELECT
id, username, password FROM account WHERE username = ?`,
id, username, password, owner FROM account WHERE username = ?`,
username)

return account, account.ID != 0
Expand Down
8 changes: 1 addition & 7 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,5 @@ type Account struct {
ID int `db:"id" json:"id"`
Username string `db:"username" json:"username"`
Password string `db:"password" json:"password,omitempty"`
}

// LoginRequest is request from user to access web interface.
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Remember int `json:"remember"`
Owner bool `db:"owner" json:"owner"`
}
4 changes: 2 additions & 2 deletions internal/view/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</head>

<body class="night">
<div id="content-scene" :class="{night: displayOptions.nightMode}">
<div id="content-scene" :class="{night: appOptions.nightMode}">
<div id="header">
<p id="metadata" v-cloak>Added {{localtime()}}</p>
<p id="title">$$.Title$$</p>
Expand Down Expand Up @@ -61,7 +61,7 @@
nightMode = (typeof opts.nightMode === "boolean") ? opts.nightMode : false,
useArchive = (typeof opts.useArchive === "boolean") ? opts.useArchive : false;

this.displayOptions = {
this.appOptions = {
showId: showId,
listMode: listMode,
nightMode: nightMode,
Expand Down
2 changes: 1 addition & 1 deletion internal/view/css/custom-dialog.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0c4d75f

Please sign in to comment.