diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-22 18:45:59 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-22 19:35:33 +0200 |
commit | 3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae (patch) | |
tree | 0f43f30d824b8b805e4a72b66a0ee1bee7397803 /pkg/service | |
parent | 1e4613aa1113b373a8d841c28e222599237a33c5 (diff) | |
download | lens-3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae.tar.gz lens-3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae.tar.bz2 lens-3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae.zip |
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.
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 +} |