aboutsummaryrefslogtreecommitdiff
path: root/pkg/service/auth.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/service/auth.go')
-rw-r--r--pkg/service/auth.go58
1 files changed, 58 insertions, 0 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