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 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/service/main_test.go | 13 +++++++---- 2 files changed, 67 insertions(+), 4 deletions(-) (limited to 'pkg/service') 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 diff --git a/pkg/service/main_test.go b/pkg/service/main_test.go index e1214dc..8cef985 100644 --- a/pkg/service/main_test.go +++ b/pkg/service/main_test.go @@ -28,6 +28,7 @@ type ( ) var _ repository.UserRepository = &UserRepository{} + var _ repository.AuthRepository = &UserRepository{} func NewUserRepository() *UserRepository { @@ -86,10 +87,6 @@ func (u *UserRepository) Update(_ context.Context, id uint, updateUser *reposito user.Name = updateUser.Name user.Username = updateUser.Username - if updateUser.Password != "" { - user.Password = []byte(updateUser.Password) - } - return nil } @@ -127,3 +124,11 @@ func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string return "", errors.New("Not Found") } + +func (u *UserRepository) UpdatePassword(ctx context.Context, id uint, password []byte) error { + panic("not implemented") // TODO: Implement +} + +func (u *UserRepository) Delete(ctx context.Context, id uint) error { + panic("not implemented") // TODO: Implement +} -- cgit v1.2.3