diff options
Diffstat (limited to 'pkg/service')
-rw-r--r-- | pkg/service/auth.go | 58 | ||||
-rw-r--r-- | pkg/service/main_test.go | 13 |
2 files changed, 67 insertions, 4 deletions
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 +} |