swagger:route PATCH /admin/identities/{id} identity patchIdentity # Patch an Identity Partially updates an [identity's](https://www.ory.sh/docs/kratos/concepts/identity-user-model) field using [JSON Patch](https://jsonpatch.com/). The fields `id`, `stateChangedAt` and `credentials` can not be upda
(w http.ResponseWriter, r *http.Request)
| 1095 | // Extensions: |
| 1096 | // x-ory-ratelimit-bucket: kratos-admin-high |
| 1097 | func (h *Handler) patch(w http.ResponseWriter, r *http.Request) { |
| 1098 | requestBody, err := io.ReadAll(r.Body) |
| 1099 | if err != nil { |
| 1100 | h.r.Writer().WriteError(w, r, err) |
| 1101 | return |
| 1102 | } |
| 1103 | |
| 1104 | id := x.ParseUUID(r.PathValue("id")) |
| 1105 | identity, err := h.r.PrivilegedIdentityPool().GetIdentityConfidential(r.Context(), id) |
| 1106 | if err != nil { |
| 1107 | h.r.Writer().WriteError(w, r, err) |
| 1108 | return |
| 1109 | } |
| 1110 | |
| 1111 | oldState := identity.State |
| 1112 | |
| 1113 | patchedIdentity, err := jsonx.ApplyJSONPatch(requestBody, WithCredentialsAndAdminMetadataInJSON(*identity), "/id", "/stateChangedAt", "/credentials", "/credentials/oidc/**") |
| 1114 | if err != nil { |
| 1115 | h.r.Writer().WriteError(w, r, errors.WithStack( |
| 1116 | herodot. |
| 1117 | ErrBadRequest. |
| 1118 | WithReasonf("An error occured when applying the JSON patch"). |
| 1119 | WithErrorf("%v", err). |
| 1120 | WithWrap(err), |
| 1121 | )) |
| 1122 | return |
| 1123 | } |
| 1124 | |
| 1125 | if oldState != patchedIdentity.State { |
| 1126 | // Check if the changed state was actually valid |
| 1127 | if err := patchedIdentity.State.IsValid(); err != nil { |
| 1128 | h.r.Writer().WriteError(w, r, errors.WithStack( |
| 1129 | herodot. |
| 1130 | ErrBadRequest. |
| 1131 | WithReasonf("The supplied state ('%s') was not valid. Valid states are ('%s', '%s').", string(patchedIdentity.State), StateActive, StateInactive). |
| 1132 | WithErrorf("%v", err). |
| 1133 | WithWrap(err), |
| 1134 | )) |
| 1135 | return |
| 1136 | } |
| 1137 | |
| 1138 | // If the state changed, we need to update the timestamp of it |
| 1139 | stateChangedAt := sqlxx.NullTime(time.Now()) |
| 1140 | patchedIdentity.StateChangedAt = &stateChangedAt |
| 1141 | } |
| 1142 | |
| 1143 | updatedIdentity := Identity(patchedIdentity) |
| 1144 | |
| 1145 | if err := h.r.IdentityManager().Update( |
| 1146 | r.Context(), |
| 1147 | &updatedIdentity, |
| 1148 | ManagerAllowWriteProtectedTraits, |
| 1149 | ); err != nil { |
| 1150 | h.r.Writer().WriteError(w, r, err) |
| 1151 | return |
| 1152 | } |
| 1153 | |
| 1154 | h.r.Writer().Write(w, r, WithCredentialsNoConfigAndAdminMetadataInJSON(updatedIdentity)) |
nothing calls this directly
no test coverage detected