feat: add update and delete operations for users and policies in admin interface
Add updateUser and deleteUser API client methods with PATCH and DELETE endpoints. Add updatePolicy and deletePolicy API client methods. Add email field to User type. Add Actions column to users and policies tables with Edit and Delete buttons. Implement inline edit forms for users and policies with state management for editing mode. Add update and delete mutations with query invalidation on success. Add error notices
This commit is contained in:
@@ -86,6 +86,61 @@ func (h *Handler) Disable(w http.ResponseWriter, r *http.Request) {
|
||||
apiutil.JSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
targetID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
apiutil.Error(w, http.StatusBadRequest, "invalid_user_id", "invalid user id")
|
||||
return
|
||||
}
|
||||
|
||||
var input UpdateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
||||
apiutil.Error(w, http.StatusBadRequest, "invalid_json", "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := h.service.Update(r.Context(), targetID.String(), input)
|
||||
if err != nil {
|
||||
apiutil.Error(w, http.StatusInternalServerError, "user_update_failed", "unable to update user")
|
||||
return
|
||||
}
|
||||
|
||||
if claims, ok := requestctx.ClaimsFromContext(r.Context()); ok {
|
||||
_ = h.audit.Record(r.Context(), audit.Entry{
|
||||
ActorUserID: &claims.UserID,
|
||||
EntityType: "user",
|
||||
EntityID: &targetID,
|
||||
EventType: "admin.user.updated",
|
||||
Status: "success",
|
||||
Message: "admin updated user",
|
||||
})
|
||||
}
|
||||
apiutil.JSON(w, http.StatusOK, updated)
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
targetID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
apiutil.Error(w, http.StatusBadRequest, "invalid_user_id", "invalid user id")
|
||||
return
|
||||
}
|
||||
if err := h.service.Delete(r.Context(), targetID.String()); err != nil {
|
||||
apiutil.Error(w, http.StatusInternalServerError, "user_delete_failed", "unable to delete user")
|
||||
return
|
||||
}
|
||||
if claims, ok := requestctx.ClaimsFromContext(r.Context()); ok {
|
||||
_ = h.audit.Record(r.Context(), audit.Entry{
|
||||
ActorUserID: &claims.UserID,
|
||||
EntityType: "user",
|
||||
EntityID: &targetID,
|
||||
EventType: "admin.user.deleted",
|
||||
Status: "success",
|
||||
Message: "admin deleted user",
|
||||
})
|
||||
}
|
||||
apiutil.JSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (h *Handler) Enable(w http.ResponseWriter, r *http.Request) {
|
||||
targetID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
type Repository interface {
|
||||
List(ctx context.Context) ([]User, error)
|
||||
Create(ctx context.Context, input CreateRequest, passwordHash string) (User, error)
|
||||
Update(ctx context.Context, userID uuid.UUID, input UpdateRequest, passwordHash *string) (User, error)
|
||||
Delete(ctx context.Context, userID uuid.UUID) error
|
||||
SetActive(ctx context.Context, userID uuid.UUID, active bool) error
|
||||
}
|
||||
|
||||
@@ -69,3 +71,35 @@ func (r *PGRepository) SetActive(ctx context.Context, userID uuid.UUID, active b
|
||||
_, err := r.db.Exec(ctx, `update users set is_active = $2, updated_at = now() where id = $1`, userID, active)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PGRepository) Update(ctx context.Context, userID uuid.UUID, input UpdateRequest, passwordHash *string) (User, error) {
|
||||
const query = `
|
||||
update users u
|
||||
set
|
||||
role_id = coalesce((select id from roles where name = $2), u.role_id),
|
||||
display_name = coalesce($3, u.display_name),
|
||||
email = case when $4 is null then u.email else nullif($4, '')::citext end,
|
||||
password_hash = coalesce($5, u.password_hash),
|
||||
is_active = coalesce($6, u.is_active),
|
||||
updated_at = now()
|
||||
where u.id = $1 and u.deleted_at is null
|
||||
returning
|
||||
u.id,
|
||||
u.role_id,
|
||||
(select name from roles where id = u.role_id),
|
||||
u.username::text,
|
||||
u.display_name,
|
||||
coalesce(u.email::text, ''),
|
||||
u.is_active
|
||||
`
|
||||
|
||||
var item User
|
||||
err := r.db.QueryRow(ctx, query, userID, input.Role, input.DisplayName, input.Email, passwordHash, input.IsActive).
|
||||
Scan(&item.ID, &item.RoleID, &item.RoleName, &item.Username, &item.DisplayName, &item.Email, &item.IsActive)
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *PGRepository) Delete(ctx context.Context, userID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `update users set deleted_at = now(), updated_at = now() where id = $1 and deleted_at is null`, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -28,6 +28,32 @@ func (s *Service) Create(ctx context.Context, input CreateRequest) (User, error)
|
||||
return s.repo.Create(ctx, input, passwordHash)
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, userID string, input UpdateRequest) (User, error) {
|
||||
id, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
var passwordHash *string
|
||||
if input.Password != nil && *input.Password != "" {
|
||||
hashed, err := auth.HashPassword(*input.Password)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
passwordHash = &hashed
|
||||
}
|
||||
|
||||
return s.repo.Update(ctx, id, input, passwordHash)
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, userID string) error {
|
||||
id, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) SetActive(ctx context.Context, userID string, active bool) error {
|
||||
id, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
|
||||
@@ -21,7 +21,9 @@ type CreateRequest struct {
|
||||
}
|
||||
|
||||
type UpdateRequest struct {
|
||||
Role *string `json:"role"`
|
||||
DisplayName *string `json:"display_name"`
|
||||
Email *string `json:"email"`
|
||||
Password *string `json:"password"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user