mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 17:58:50 +02:00
store; account
This commit is contained in:
parent
a0a6f49131
commit
629d2fe0dc
37
controller/store/account.go
Normal file
37
controller/store/account.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Account struct {
|
||||||
|
Model
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) {
|
||||||
|
stmt, err := tx.Prepare("insert into accounts (username, password, token) values (?, ?, ?)")
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error preparing accounts insert statement")
|
||||||
|
}
|
||||||
|
res, err := stmt.Exec(a.Username, a.Password, a.Token)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error executing accounts insert statement")
|
||||||
|
}
|
||||||
|
id, err := res.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error retrieving last accounts insert id")
|
||||||
|
}
|
||||||
|
return int(id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Store) FindAccountWithToken(token string, tx *sqlx.Tx) (*Account, error) {
|
||||||
|
a := &Account{}
|
||||||
|
if err := tx.QueryRowx("select * from accounts where token = ?", token).StructScan(a); err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error selecting account by token")
|
||||||
|
}
|
||||||
|
return a, nil
|
||||||
|
}
|
@ -1,13 +1,13 @@
|
|||||||
-- +migrate Up
|
-- +migrate Up
|
||||||
|
|
||||||
--
|
--
|
||||||
-- credentials
|
-- accounts
|
||||||
--
|
--
|
||||||
create table credentials (
|
create table accounts (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
username string not null,
|
username string not null unique,
|
||||||
password string not null,
|
password string not null,
|
||||||
token string not null,
|
token string not null unique,
|
||||||
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||||
|
|
||||||
|
@ -9,8 +9,15 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
Id int
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user