Use usernames directly

This commit is contained in:
Abhishek Shroff
2024-04-19 12:53:57 +05:30
parent dca85cc4d9
commit 09a7ba4fef
10 changed files with 75 additions and 56 deletions

View File

@@ -23,25 +23,26 @@ func setupSiloCommand() *cobra.Command {
func setupSiloCreateCommand() *cobra.Command {
return &cobra.Command{
Use: "create storage-backend owner display-name",
Use: "create owner storage name",
Short: "Create Silo",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
id := uuid.New()
storageName := args[0]
username := args[0]
user, err := userManager.FindUser(context.Background(), username)
if err != nil {
logrus.Fatal("User not found: " + username)
}
storageName := args[1]
storage := storageManager.Find(storageName)
if storage == nil {
logrus.Fatal("Storage not found: " + storageName)
}
username := args[1]
user, err := userManager.FindUser(context.Background(), username)
if err != nil {
logrus.Fatal("User not found: " + username)
}
name := args[2]
if err := fs.CreateSilo(context.Background(), id, user.ID, storageName, name); err != nil {
if err := fs.CreateSilo(context.Background(), id, user.Username, storageName, name); err != nil {
logrus.Fatal(err)
}
logrus.Info("Created " + id.String())
@@ -63,11 +64,8 @@ func setupSiloListCommand() *cobra.Command {
for _, silo := range silos {
logrus.Infof("%-16s: %s\n", silo.Name, silo.ID.String())
logrus.Infof(" storage: %s\n", silo.Storage)
if owner, err := userManager.UserById(ctx, silo.Owner); err == nil {
logrus.Infof(" owner: %5d - %s\n", silo.Owner, owner.Username)
} else {
logrus.Infof(" owner not found: %5d\n", silo.Owner)
}
logrus.Infof(" owner: %s\n", silo.Owner)
logrus.Info()
}
},
}

View File

@@ -20,6 +20,7 @@ func setupUserCommand() *cobra.Command {
}
cmd.AddCommand([]*cobra.Command{
setupUserCreateCommand(),
setupUserListCommand(),
setupUserLoginCommand(),
}...)
return cmd
@@ -76,8 +77,24 @@ func setupUserCreateCommand() *cobra.Command {
return cmd
}
func setupUserListCommand() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List Users",
Run: func(cmd *cobra.Command, args []string) {
users, err := userManager.ListUsers(context.Background())
if err != nil {
logrus.Fatal(err)
}
for _, user := range users {
logrus.Infof("%16s: %s", user.Username, user.DisplayName)
}
},
}
}
func setupUserLoginCommand() *cobra.Command {
cmd := &cobra.Command{
return &cobra.Command{
Use: "auth",
Short: "Authenticate user",
Run: func(cmd *cobra.Command, args []string) {
@@ -113,5 +130,4 @@ func setupUserLoginCommand() *cobra.Command {
}
},
}
return cmd
}

View File

@@ -18,7 +18,7 @@ func OpenFileSystem(db *sql.DbHandler, storageManager *storage.Manager) *FileSys
return &FileSystem{db: db, storageManager: storageManager}
}
func (fs FileSystem) CreateSilo(ctx context.Context, id uuid.UUID, owner int32, storage string, name string) error {
func (fs FileSystem) CreateSilo(ctx context.Context, id uuid.UUID, owner, storage, name string) error {
return fs.db.RunInTx(ctx, func(q *sql.Queries) error {
if err := q.CreateSilo(ctx, sql.CreateSiloParams{
ID: id,

View File

@@ -26,7 +26,7 @@ type Silo struct {
Created pgtype.Timestamp
Modified pgtype.Timestamp
Deleted pgtype.Timestamp
Owner int32
Owner string
Name string
Storage string
}
@@ -38,9 +38,8 @@ type StorageBackend struct {
}
type User struct {
ID int32
DisplayName string
Username string
DisplayName string
PasswordHash string
Deleted pgtype.Timestamp
}

View File

@@ -21,7 +21,7 @@ INSERT INTO silos(
type CreateSiloParams struct {
ID uuid.UUID
Owner int32
Owner string
Name string
Storage string
}

View File

@@ -11,60 +11,70 @@ import (
const createUser = `-- name: CreateUser :one
INSERT INTO users(
display_name, username, password_hash
username, display_name, password_hash
) VALUES (
$1, $2, $3
)
RETURNING id, display_name, username, password_hash, deleted
RETURNING username, display_name, password_hash, deleted
`
type CreateUserParams struct {
DisplayName string
Username string
DisplayName string
PasswordHash string
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser, arg.DisplayName, arg.Username, arg.PasswordHash)
row := q.db.QueryRow(ctx, createUser, arg.Username, arg.DisplayName, arg.PasswordHash)
var i User
err := row.Scan(
&i.ID,
&i.DisplayName,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.Deleted,
)
return i, err
}
const userById = `-- name: UserById :one
SELECT id, display_name, username, password_hash, deleted from users WHERE id = $1
const listUsers = `-- name: ListUsers :many
SELECT username, display_name, password_hash, deleted from users WHERE deleted IS NULL
`
func (q *Queries) UserById(ctx context.Context, id int32) (User, error) {
row := q.db.QueryRow(ctx, userById, id)
var i User
err := row.Scan(
&i.ID,
&i.DisplayName,
&i.Username,
&i.PasswordHash,
&i.Deleted,
)
return i, err
func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
rows, err := q.db.Query(ctx, listUsers)
if err != nil {
return nil, err
}
defer rows.Close()
var items []User
for rows.Next() {
var i User
if err := rows.Scan(
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.Deleted,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const userByUsername = `-- name: UserByUsername :one
SELECT id, display_name, username, password_hash, deleted from users WHERE username = $1
SELECT username, display_name, password_hash, deleted from users WHERE username = $1
`
func (q *Queries) UserByUsername(ctx context.Context, username string) (User, error) {
row := q.db.QueryRow(ctx, userByUsername, username)
var i User
err := row.Scan(
&i.ID,
&i.DisplayName,
&i.Username,
&i.DisplayName,
&i.PasswordHash,
&i.Deleted,
)

View File

@@ -22,10 +22,10 @@ func (m Manager) CreateUser(ctx context.Context, username, displayName, password
return nil
}
func (m Manager) ListUsers(ctx context.Context) ([]sql.User, error) {
return m.db.Queries().ListUsers(ctx)
}
func (m Manager) FindUser(ctx context.Context, username string) (sql.User, error) {
return m.db.Queries().UserByUsername(ctx, username)
}
func (m Manager) UserById(ctx context.Context, id int32) (sql.User, error) {
return m.db.Queries().UserById(ctx, id)
}

View File

@@ -1,9 +1,6 @@
CREATE TABLE users(
id SERIAL PRIMARY KEY,
username TEXT NOT NULL PRIMARY KEY,
display_name TEXT NOT NULL,
username TEXT NOT NULL,
password_hash TEXT NOT NULL,
deleted TIMESTAMP
);
CREATE UNIQUE INDEX unique_username ON users(username);
);

View File

@@ -3,7 +3,7 @@ CREATE TABLE silos(
created TIMESTAMP NOT NULL,
modified TIMESTAMP NOT NULL,
deleted TIMESTAMP,
owner SERIAL NOT NULL REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
owner TEXT NOT NULL REFERENCES users(username) ON UPDATE CASCADE ON DELETE CASCADE,
name TEXT NOT NULL,
storage TEXT NOT NULL REFERENCES storage_backends(name) ON UPDATE CASCADE ON DELETE CASCADE
);

View File

@@ -1,14 +1,13 @@
-- name: CreateUser :one
INSERT INTO users(
display_name, username, password_hash
username, display_name, password_hash
) VALUES (
$1, $2, $3
)
RETURNING *;
-- name: UserByUsername :one
SELECT * from users WHERE username = $1;
-- name: UserById :one
SELECT * from users WHERE id = $1;
-- name: ListUsers :many
SELECT * from users WHERE deleted IS NULL;