Skip to content

Commit

Permalink
Adds route to switch roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Jan 27, 2024
1 parent 24e9237 commit 3475687
Show file tree
Hide file tree
Showing 4 changed files with 60 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 @@ -233,3 +233,44 @@ func HandleRemoveBanIdentity(c *gin.Context) {
"identity": id,
})
}

func HandleRoleSwitch(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.RoleSwitchFlowWrapper(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 @@ -42,6 +42,7 @@ func Start() {
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.PUT("/update-identity/switch-roles", middleware.OnlyAdmin, HandleRoleSwitch)

r.GET("/register", HandleGetRegistrationFlow)
r.POST("/register", HandlePostRegistrationFlow)
Expand Down
1 change: 1 addition & 0 deletions pkg/middleware/rolecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func GetSession(c *gin.Context) (*client.Session, error) {
func OnlyAdmin(c *gin.Context) {
session, err := GetSession(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
c.Abort()
return
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/wrapper/kratos/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ func RemoveBanIdentityFlowWrapper(identity *client.Identity) (*client.Identity,

return id, r, err
}

func RoleSwitchFlowWrapper(identity *client.Identity) (*client.Identity, *http.Response, error) {
traits := identity.GetTraits().(map[string]interface{})

if traits["role"] == "user" {
traits["role"] = "admin"
} else if traits["role"] == "admin" {
traits["role"] = "user"
}

submitDataBody := *client.NewAdminUpdateIdentityBody(identity.SchemaId, *identity.State, traits)

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 3475687

Please sign in to comment.