From 24e9237f144d4e76b6ce238dd587d174f36eebd9 Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sat, 27 Jan 2024 16:58:14 +0530 Subject: [PATCH] Adds route to lift ban --- api/admin.go | 41 +++++++++++++++++++++++++++++++ api/main.go | 1 + pkg/wrapper/kratos/admin/admin.go | 14 +++++++++++ 3 files changed, 56 insertions(+) diff --git a/api/admin.go b/api/admin.go index 8031d6c..2181555 100644 --- a/api/admin.go +++ b/api/admin.go @@ -192,3 +192,44 @@ func HandleBanIdentity(c *gin.Context) { "identity": id, }) } + +func HandleRemoveBanIdentity(c *gin.Context) { + var t IdentityBody + err := c.BindJSON(&t) + + if err != nil { + log.ErrorLogger("Unable to process JSON body", err) + + errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0]) + c.JSON(errCode, gin.H{ + "error": err.Error(), + "message": "Unable to process JSON body", + }) + return + } + + identityResult, r, err := admin.GetIdentityFlowWrapper(t.Identity) + + if err != nil { + log.ErrorLogger("Error while fetching Identity details", err) + fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Internal server error", + }) + return + } + + id, r, err := admin.RemoveBanIdentityFlowWrapper(identityResult) + + if err != nil { + log.ErrorLogger("Error while calling `AdminPatchIdentities`", err) + fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "identity": id, + }) +} diff --git a/api/main.go b/api/main.go index 5d92d38..f743256 100644 --- a/api/main.go +++ b/api/main.go @@ -41,6 +41,7 @@ func Start() { r.POST("/delete-identity", middleware.OnlyAdmin, HandleDeleteIdentityFlow) r.GET("/list-identity", middleware.OnlyAdmin, HandleListIdentity) r.PUT("/update-identity/ban", middleware.OnlyAdmin, HandleBanIdentity) + r.PUT("/update-identity/remove-ban", middleware.OnlyAdmin, HandleRemoveBanIdentity) r.GET("/register", HandleGetRegistrationFlow) r.POST("/register", HandlePostRegistrationFlow) diff --git a/pkg/wrapper/kratos/admin/admin.go b/pkg/wrapper/kratos/admin/admin.go index f4f3e87..1ae8213 100644 --- a/pkg/wrapper/kratos/admin/admin.go +++ b/pkg/wrapper/kratos/admin/admin.go @@ -72,3 +72,17 @@ func BanIdentityFlowWrapper(identity *client.Identity) (*client.Identity, *http. return id, r, err } + +func RemoveBanIdentityFlowWrapper(identity *client.Identity) (*client.Identity, *http.Response, error) { + newState, err := client.NewIdentityStateFromValue("active") + if err != nil { + return nil, nil, err + } + + submitDataBody := *client.NewAdminUpdateIdentityBody(identity.SchemaId, *newState, identity.Traits.(map[string]interface{})) + + apiClient := client.NewAPIClient(config.KratosClientConfigAdmin) + id, r, err := apiClient.V0alpha2Api.AdminUpdateIdentity(context.Background(), identity.Id).AdminUpdateIdentityBody(submitDataBody).Execute() + + return id, r, err +}