Update User @Tags users @Summary Update user by name @Description Update user by name @Accept json @Produce json @Param name path string true "用户名称" @Success 200 {object} v1User.User @Security ApiKeyAuth @Router /users/{name} [put]
()
| 332 | // @Security ApiKeyAuth |
| 333 | // @Router /users/{name} [put] |
| 334 | func (h *Handler) UpdateUser() iris.Handler { |
| 335 | return func(ctx *context.Context) { |
| 336 | userName := ctx.Params().GetString("name") |
| 337 | var req User |
| 338 | if err := ctx.ReadJSON(&req); err != nil { |
| 339 | ctx.StatusCode(iris.StatusBadRequest) |
| 340 | ctx.Values().Set("message", err.Error()) |
| 341 | return |
| 342 | } |
| 343 | if req.Password != "" { |
| 344 | if err := h.userService.UpdatePassword(userName, req.OldPassword, req.Password, common.DBOptions{}); err != nil { |
| 345 | ctx.StatusCode(iris.StatusInternalServerError) |
| 346 | ctx.Values().Set("message", "can not match original password") |
| 347 | return |
| 348 | } |
| 349 | ctx.Values().Set("data", "ok") |
| 350 | return |
| 351 | } |
| 352 | u := ctx.Values().Get("profile") |
| 353 | profile := u.(session.UserProfile) |
| 354 | tx, err := server.DB().Begin(true) |
| 355 | if err != nil { |
| 356 | ctx.StatusCode(iris.StatusInternalServerError) |
| 357 | ctx.Values().Set("message", err.Error()) |
| 358 | return |
| 359 | } |
| 360 | if err := h.userService.Update(userName, &req.User, common.DBOptions{DB: tx}); err != nil { |
| 361 | _ = tx.Rollback() |
| 362 | ctx.StatusCode(iris.StatusInternalServerError) |
| 363 | ctx.Values().Set("message", err.Error()) |
| 364 | return |
| 365 | } |
| 366 | bindings, err := h.roleBindingService.GetRoleBindingBySubject(v1Role.Subject{Kind: "User", Name: userName}, common.DBOptions{}) |
| 367 | if err != nil && !errors.Is(err, storm.ErrNotFound) { |
| 368 | ctx.StatusCode(iris.StatusInternalServerError) |
| 369 | ctx.Values().Set("message", err.Error()) |
| 370 | return |
| 371 | } |
| 372 | currentRoles := collections.NewStringSet() |
| 373 | for i := range bindings { |
| 374 | currentRoles.Add(bindings[i].RoleRef) |
| 375 | } |
| 376 | for i := range req.Roles { |
| 377 | r := req.Roles[i] |
| 378 | if currentRoles.Exists(r) { |
| 379 | continue |
| 380 | } |
| 381 | binding := v1Role.Binding{ |
| 382 | BaseModel: v1.BaseModel{ |
| 383 | Kind: "RoleBind", |
| 384 | ApiVersion: "v1", |
| 385 | CreatedBy: profile.Name, |
| 386 | }, |
| 387 | Metadata: v1.Metadata{ |
| 388 | Name: fmt.Sprintf("role-binding-%s-%s", r, req.Name), |
| 389 | }, |
| 390 | Subject: v1Role.Subject{ |
| 391 | Kind: "User", |
no test coverage detected