mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-05 19:21:23 -06:00
Run in tx
This commit is contained in:
@@ -96,7 +96,7 @@ func (a *App) populateData(ctx context.Context) (user db.User, e error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := q.UpdateUserDirs(ctx, db.UpdateUserDirsParams{ID: user.ID, Root: root.ID, Home: userHome.ID}); err != nil {
|
||||
if err := q.UpdateUserHome(ctx, db.UpdateUserHomeParams{ID: user.ID, Home: userHome.ID}); err != nil {
|
||||
return err
|
||||
}
|
||||
user.Root = root.ID
|
||||
|
||||
@@ -17,25 +17,27 @@ func (a App) CreateUser(ctx context.Context, username, displayName, password str
|
||||
if root != nil {
|
||||
rootID = *root
|
||||
}
|
||||
if hash, err := cryptutil.GenerateArgon2EncodedHash(password, cryptutil.DefaultArgon2Params()); err != nil {
|
||||
return err
|
||||
} else if u, err := a.db.Queries().CreateUser(ctx, db.CreateUserParams{
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
PasswordHash: hash,
|
||||
Root: rootID,
|
||||
Home: rootID,
|
||||
}); err != nil {
|
||||
return err
|
||||
} else if home, err := a.rootfs.ResourceByPath("/home"); err != nil {
|
||||
return err
|
||||
} else if home, err := a.rootfs.CreateMemberResource(home, uuid.New(), username, true); err != nil {
|
||||
return err
|
||||
} else if err := a.rootfs.UpdateOwner(home, u.ID); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return a.db.Queries().UpdateUserDirs(ctx, db.UpdateUserDirsParams{ID: u.ID, Root: rootID, Home: home.ID()})
|
||||
}
|
||||
return a.db.WithTx(ctx, func(q *db.Queries) error {
|
||||
if hash, err := cryptutil.GenerateArgon2EncodedHash(password, cryptutil.DefaultArgon2Params()); err != nil {
|
||||
return err
|
||||
} else if u, err := q.CreateUser(ctx, db.CreateUserParams{
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
PasswordHash: hash,
|
||||
Root: rootID,
|
||||
Home: rootID,
|
||||
}); err != nil {
|
||||
return err
|
||||
} else if home, err := a.rootfs.ResourceByPath("/home"); err != nil {
|
||||
return err
|
||||
} else if userHome, err := a.rootfs.CreateMemberResource(home, uuid.New(), username, true); err != nil {
|
||||
return err
|
||||
} else if err := a.rootfs.UpdateOwner(userHome, u.ID); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return q.UpdateUserHome(ctx, db.UpdateUserHomeParams{ID: u.ID, Home: userHome.ID()})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (a App) ListUsers(ctx context.Context) ([]User, error) {
|
||||
|
||||
@@ -84,26 +84,6 @@ func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateUserDirs = `-- name: UpdateUserDirs :exec
|
||||
UPDATE users
|
||||
SET
|
||||
root = $1,
|
||||
home = $2,
|
||||
modified = NOW()
|
||||
WHERE id = $3
|
||||
`
|
||||
|
||||
type UpdateUserDirsParams struct {
|
||||
Root uuid.UUID
|
||||
Home uuid.UUID
|
||||
ID int32
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserDirs(ctx context.Context, arg UpdateUserDirsParams) error {
|
||||
_, err := q.db.Exec(ctx, updateUserDirs, arg.Root, arg.Home, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserDisplayName = `-- name: UpdateUserDisplayName :exec
|
||||
UPDATE users
|
||||
SET
|
||||
@@ -122,6 +102,24 @@ func (q *Queries) UpdateUserDisplayName(ctx context.Context, arg UpdateUserDispl
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserHome = `-- name: UpdateUserHome :exec
|
||||
UPDATE users
|
||||
SET
|
||||
home = $1,
|
||||
modified = NOW()
|
||||
WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateUserHomeParams struct {
|
||||
Home uuid.UUID
|
||||
ID int32
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserHome(ctx context.Context, arg UpdateUserHomeParams) error {
|
||||
_, err := q.db.Exec(ctx, updateUserHome, arg.Home, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserPasswordHash = `-- name: UpdateUserPasswordHash :exec
|
||||
UPDATE users
|
||||
SET
|
||||
@@ -140,6 +138,24 @@ func (q *Queries) UpdateUserPasswordHash(ctx context.Context, arg UpdateUserPass
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserRoot = `-- name: UpdateUserRoot :exec
|
||||
UPDATE users
|
||||
SET
|
||||
root = $1,
|
||||
modified = NOW()
|
||||
WHERE id = $2
|
||||
`
|
||||
|
||||
type UpdateUserRootParams struct {
|
||||
Root uuid.UUID
|
||||
ID int32
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserRoot(ctx context.Context, arg UpdateUserRootParams) error {
|
||||
_, err := q.db.Exec(ctx, updateUserRoot, arg.Root, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const userByID = `-- name: UserByID :one
|
||||
SELECT id, username, created, modified, display_name, password_hash, deleted, root, home from users WHERE id = $1
|
||||
`
|
||||
|
||||
@@ -29,10 +29,16 @@ SET
|
||||
WHERE id = $2;
|
||||
|
||||
|
||||
-- name: UpdateUserDirs :exec
|
||||
-- name: UpdateUserRoot :exec
|
||||
UPDATE users
|
||||
SET
|
||||
root = $1,
|
||||
home = $2,
|
||||
modified = NOW()
|
||||
WHERE id = $3;
|
||||
WHERE id = $2;
|
||||
|
||||
-- name: UpdateUserHome :exec
|
||||
UPDATE users
|
||||
SET
|
||||
home = $1,
|
||||
modified = NOW()
|
||||
WHERE id = $2;
|
||||
Reference in New Issue
Block a user