diff --git a/server/internal/core/app.go b/server/internal/core/app.go index 099e3007..22ba653a 100644 --- a/server/internal/core/app.go +++ b/server/internal/core/app.go @@ -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 diff --git a/server/internal/core/user_manager.go b/server/internal/core/user_manager.go index 62630e0f..8d09e5da 100644 --- a/server/internal/core/user_manager.go +++ b/server/internal/core/user_manager.go @@ -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) { diff --git a/server/internal/db/users.sql.go b/server/internal/db/users.sql.go index bd784305..40fe64a7 100644 --- a/server/internal/db/users.sql.go +++ b/server/internal/db/users.sql.go @@ -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 ` diff --git a/server/sql/queries/users.sql b/server/sql/queries/users.sql index 6955e58a..6f0642f6 100644 --- a/server/sql/queries/users.sql +++ b/server/sql/queries/users.sql @@ -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; \ No newline at end of file +WHERE id = $2; + +-- name: UpdateUserHome :exec +UPDATE users +SET + home = $1, + modified = NOW() +WHERE id = $2; \ No newline at end of file