From 3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 22 Jul 2023 18:45:59 +0200 Subject: feat: Add user management As many things it is on crude state. The settings.go has become a big mess, but I have achieve MVP, so from now one things shall improve as I'll spent more time on refactoring. --- pkg/service/auth.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'pkg/service/auth.go') diff --git a/pkg/service/auth.go b/pkg/service/auth.go index 1966e70..f27cf88 100644 --- a/pkg/service/auth.go +++ b/pkg/service/auth.go @@ -82,6 +82,64 @@ func (c *AuthController) InitialRegister(ctx context.Context, username, password return err } +func (u *AuthController) List(ctx context.Context) ([]*repository.User, error) { + return u.userRepository.List(ctx) +} + +func (u *AuthController) Get(ctx context.Context, id uint) (*repository.User, error) { + return u.userRepository.Get(ctx, id) +} + +func (u *AuthController) Delete(ctx context.Context, id uint) error { + return u.userRepository.Delete(ctx, id) +} + +func (u *AuthController) Upsert( + ctx context.Context, + id *uint, + username string, + name string, + password []byte, + isAdmin bool, + path string, +) error { + if id != nil { + if err := u.userRepository.Update(ctx, *id, &repository.UpdateUser{ + Username: string(username), + Name: name, + IsAdmin: isAdmin, + Path: path, + }); err != nil { + return err + } + + if len(password) > 0 { + hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) + if err != nil { + return err + } + + return u.userRepository.UpdatePassword(ctx, *id, hash) + } + return nil + } + + hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) + if err != nil { + return err + } + + _, err = u.userRepository.Create(ctx, &repository.CreateUser{ + Username: username, + Name: name, + Password: hash, + IsAdmin: isAdmin, + Path: path, + }) + + return err +} + type Token struct { UserID uint Username string -- cgit v1.2.3