Skip to content

Commit

Permalink
Adds route to lift ban
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Jan 27, 2024
1 parent 1284c2d commit 24e9237
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
41 changes: 41 additions & 0 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 24e9237

Please sign in to comment.