From d81224a3a1fdeccebe035ed17dd52580d4daed0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaque=20V=C3=A9ras?= Date: Sat, 11 Feb 2023 18:42:00 -0300 Subject: [PATCH 1/2] feat: adding route to disable user --- internal/application/auth/user/business.go | 34 ++++++++++++++++ internal/domain/auth/user/interface.go | 3 ++ internal/infrastructure/auth/user/postgres.go | 19 +++++++++ .../infrastructure/auth/user/repository.go | 6 +++ internal/interface/http/auth/router.go | 9 ++++- internal/interface/http/auth/user/handler.go | 39 +++++++++++++++++++ internal/interface/http/auth/user/router.go | 12 ++++++ ...andling_grpc.go => error_handling_grpc.go} | 0 pkg/oops/helpers.go | 2 - 9 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 internal/application/auth/user/business.go create mode 100644 internal/interface/http/auth/user/handler.go create mode 100644 internal/interface/http/auth/user/router.go rename pkg/oops/{erro_handling_grpc.go => error_handling_grpc.go} (100%) diff --git a/internal/application/auth/user/business.go b/internal/application/auth/user/business.go new file mode 100644 index 0000000..e528887 --- /dev/null +++ b/internal/application/auth/user/business.go @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Isaque Veras +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package user + +import ( + "context" + + "github.com/google/uuid" + "github.com/isaqueveras/power-sso/internal/infrastructure/auth/user" + "github.com/isaqueveras/power-sso/pkg/database/postgres" + "github.com/isaqueveras/power-sso/pkg/oops" +) + +// Disable is the business logic for disable user +func Disable(ctx context.Context, userUUID *uuid.UUID) (err error) { + var transaction *postgres.DBTransaction + if transaction, err = postgres.NewTransaction(ctx, false); err != nil { + return oops.Err(err) + } + defer transaction.Rollback() + + repository := user.New(transaction) + if err = repository.DisableUser(userUUID); err != nil { + return oops.Err(err) + } + + if err = transaction.Commit(); err != nil { + return oops.Err(err) + } + + return +} diff --git a/internal/domain/auth/user/interface.go b/internal/domain/auth/user/interface.go index 0859341..9eed6b9 100644 --- a/internal/domain/auth/user/interface.go +++ b/internal/domain/auth/user/interface.go @@ -4,8 +4,11 @@ package user +import "github.com/google/uuid" + // IUser define an interface for data layer access methods type IUser interface { GetUser(data *User) error FindByEmailUserExists(email *string) (exists bool, err error) + DisableUser(userUUID *uuid.UUID) error } diff --git a/internal/infrastructure/auth/user/postgres.go b/internal/infrastructure/auth/user/postgres.go index d3ce029..0624d2e 100644 --- a/internal/infrastructure/auth/user/postgres.go +++ b/internal/infrastructure/auth/user/postgres.go @@ -8,6 +8,7 @@ import ( "database/sql" "github.com/Masterminds/squirrel" + "github.com/google/uuid" "github.com/isaqueveras/power-sso/internal/domain/auth/user" "github.com/isaqueveras/power-sso/pkg/database/postgres" @@ -78,3 +79,21 @@ func (pg *pgUser) getUser(data *user.User) (err error) { } return nil } + +// disableUser disable user in database +func (pg *pgUser) disableUser(userUUID *uuid.UUID) (err error) { + if err = pg.DB.Builder. + Update("users"). + Set("is_active", false). + Set("updated_at", squirrel.Expr("NOW()")). + Where(squirrel.Eq{ + "id": userUUID, + "is_active": true, + }). + Suffix("RETURNING id"). + Scan(new(string)); err != nil { + return oops.Err(err) + } + + return +} diff --git a/internal/infrastructure/auth/user/repository.go b/internal/infrastructure/auth/user/repository.go index 3670078..6c134e8 100644 --- a/internal/infrastructure/auth/user/repository.go +++ b/internal/infrastructure/auth/user/repository.go @@ -5,6 +5,7 @@ package user import ( + "github.com/google/uuid" "github.com/isaqueveras/power-sso/internal/domain/auth/user" "github.com/isaqueveras/power-sso/pkg/database/postgres" ) @@ -30,3 +31,8 @@ func (r *repository) FindByEmailUserExists(email *string) (bool, error) { func (r *repository) GetUser(data *user.User) error { return r.pg.getUser(data) } + +// DisableUser contains the flow for disable user +func (r *repository) DisableUser(userUUID *uuid.UUID) error { + return r.pg.disableUser(userUUID) +} diff --git a/internal/interface/http/auth/router.go b/internal/interface/http/auth/router.go index be9619a..2dbe086 100644 --- a/internal/interface/http/auth/router.go +++ b/internal/interface/http/auth/router.go @@ -4,16 +4,21 @@ package auth -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "github.com/isaqueveras/power-sso/internal/interface/http/auth/user" +) // Router is the router for the auth module. func Router(r *gin.RouterGroup) { r.POST("activation/:token", activation) + r.POST("register", register) r.POST("login", login) } // RouterAuthorization is the router for the auth module. func RouterAuthorization(r *gin.RouterGroup) { - r.POST("register", register) r.DELETE("logout", logout) + + user.RouterWithUUID(r.Group("user/:user_uuid")) } diff --git a/internal/interface/http/auth/user/handler.go b/internal/interface/http/auth/user/handler.go new file mode 100644 index 0000000..d145639 --- /dev/null +++ b/internal/interface/http/auth/user/handler.go @@ -0,0 +1,39 @@ +// Copyright (c) 2023 Isaque Veras +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package user + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/isaqueveras/power-sso/internal/application/auth/user" + "github.com/isaqueveras/power-sso/internal/utils" + "github.com/isaqueveras/power-sso/pkg/oops" +) + +// disable godoc +// @Summary Disable user +// @Description Route to disable a user +// @Tags Http/Auth/User +// @Param user_uuid path string true "UUID of the user" +// @Accept json +// @Produce json +// @Success 201 {object} utils.NoContent{} +// @Router /v1/auth/user/{user_uuid}/disable [put] +func disable(ctx *gin.Context) { + userUUID, err := uuid.Parse(ctx.Param("user_uuid")) + if err != nil { + oops.Handling(ctx, err) + return + } + + if err = user.Disable(ctx, &userUUID); err != nil { + oops.Handling(ctx, err) + return + } + + ctx.JSON(http.StatusCreated, utils.NoContent{}) +} diff --git a/internal/interface/http/auth/user/router.go b/internal/interface/http/auth/user/router.go new file mode 100644 index 0000000..1d360c1 --- /dev/null +++ b/internal/interface/http/auth/user/router.go @@ -0,0 +1,12 @@ +// Copyright (c) 2023 Isaque Veras +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package user + +import "github.com/gin-gonic/gin" + +// RouterWithUUID is the router for the user module. +func RouterWithUUID(r *gin.RouterGroup) { + r.PUT("disable", disable) +} diff --git a/pkg/oops/erro_handling_grpc.go b/pkg/oops/error_handling_grpc.go similarity index 100% rename from pkg/oops/erro_handling_grpc.go rename to pkg/oops/error_handling_grpc.go diff --git a/pkg/oops/helpers.go b/pkg/oops/helpers.go index 3ca3e4a..9313c4a 100644 --- a/pkg/oops/helpers.go +++ b/pkg/oops/helpers.go @@ -62,8 +62,6 @@ func handling(rawError error) error { case strconv.ErrSyntax: message, code = i18n.Value("errors.handling.error.strconv_err_syntax"), defaultCode+3 - default: - message, code = err.Error(), defaultCode+4 } case nil: return nil From 625d548885443a4e436468714a4bbd0a50d67e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaque=20V=C3=A9ras?= Date: Sat, 11 Feb 2023 18:46:00 -0300 Subject: [PATCH 2/2] fix: fix handler test create user --- internal/interface/http/auth/handler_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/interface/http/auth/handler_test.go b/internal/interface/http/auth/handler_test.go index b87d8ff..083f585 100644 --- a/internal/interface/http/auth/handler_test.go +++ b/internal/interface/http/auth/handler_test.go @@ -35,6 +35,7 @@ func (a *authHandlerSuite) SetupSuite() { config.LoadConfig("../../../../") a.router = gin.New() + Router(a.router.Group("v1/auth")) RouterAuthorization(a.router.Group("v1/auth")) }