From af93d2090b7cc6504f89e0135ac9af57a322eb60 Mon Sep 17 00:00:00 2001 From: Wilfred Asomani Date: Fri, 21 Jul 2023 17:09:54 +0000 Subject: [PATCH] satellite/admin: add endpoint to unwarn user This change enables the admin UI to remove the warning status of users. resolves: storj-private/issues/342 Change-Id: Ib960ffb33fdabc045884ce7fa2c55c3553db0fb0 --- satellite/admin/server.go | 1 + satellite/admin/ui/src/lib/api.ts | 8 +++++++ satellite/admin/user.go | 29 ++++++++++++++++++++++++ satellite/admin/user_test.go | 37 +++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/satellite/admin/server.go b/satellite/admin/server.go index d27bfa435..5b5683c8c 100644 --- a/satellite/admin/server.go +++ b/satellite/admin/server.go @@ -151,6 +151,7 @@ func NewServer(log *zap.Logger, listener net.Listener, db DB, buckets *buckets.S limitUpdateAPI.HandleFunc("/users/{useremail}/limits", server.updateLimits).Methods("PUT") limitUpdateAPI.HandleFunc("/users/{useremail}/freeze", server.freezeUser).Methods("PUT") limitUpdateAPI.HandleFunc("/users/{useremail}/freeze", server.unfreezeUser).Methods("DELETE") + limitUpdateAPI.HandleFunc("/users/{useremail}/warning", server.unWarnUser).Methods("DELETE") limitUpdateAPI.HandleFunc("/projects/{project}/limit", server.getProjectLimit).Methods("GET") limitUpdateAPI.HandleFunc("/projects/{project}/limit", server.putProjectLimit).Methods("PUT", "POST") diff --git a/satellite/admin/ui/src/lib/api.ts b/satellite/admin/ui/src/lib/api.ts index a498d3c98..7e6c6fd72 100644 --- a/satellite/admin/ui/src/lib/api.ts +++ b/satellite/admin/ui/src/lib/api.ts @@ -464,6 +464,14 @@ Blank fields will not be updated.`, func: async (email: string): Promise => { return this.fetch('DELETE', `users/${email}/freeze`) as Promise; } + }, + { + name: 'unwarn user', + desc: "Remove a user's warning status", + params: [['email', new InputText('email', true)]], + func: async (email: string): Promise => { + return this.fetch('DELETE', `users/${email}/warning`) as Promise; + } } ], rest_api_keys: [ diff --git a/satellite/admin/user.go b/satellite/admin/user.go index b7a456b99..748d9194c 100644 --- a/satellite/admin/user.go +++ b/satellite/admin/user.go @@ -630,6 +630,35 @@ func (server *Server) unfreezeUser(w http.ResponseWriter, r *http.Request) { } } +func (server *Server) unWarnUser(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + vars := mux.Vars(r) + userEmail, ok := vars["useremail"] + if !ok { + sendJSONError(w, "user-email missing", "", http.StatusBadRequest) + return + } + + u, err := server.db.Console().Users().GetByEmail(ctx, userEmail) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail), + "", http.StatusNotFound) + return + } + sendJSONError(w, "failed to get user details", + err.Error(), http.StatusInternalServerError) + return + } + + if err = server.freezeAccounts.UnWarnUser(ctx, u.ID); err != nil { + sendJSONError(w, "failed to unwarn user", + err.Error(), http.StatusInternalServerError) + return + } +} + func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/satellite/admin/user_test.go b/satellite/admin/user_test.go index 39ba7a694..f2c7d041c 100644 --- a/satellite/admin/user_test.go +++ b/satellite/admin/user_test.go @@ -428,6 +428,43 @@ func TestFreezeUnfreezeUser(t *testing.T) { }) } +func TestWarnUnwarnUser(t *testing.T) { + testplanet.Run(t, testplanet.Config{ + SatelliteCount: 1, + StorageNodeCount: 0, + UplinkCount: 1, + Reconfigure: testplanet.Reconfigure{ + Satellite: func(_ *zap.Logger, _ int, config *satellite.Config) { + config.Admin.Address = "127.0.0.1:0" + }, + }, + }, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) { + address := planet.Satellites[0].Admin.Admin.Listener.Addr() + user, err := planet.Satellites[0].DB.Console().Users().Get(ctx, planet.Uplinks[0].Projects[0].Owner.ID) + require.NoError(t, err) + + err = planet.Satellites[0].Admin.FreezeAccounts.Service.WarnUser(ctx, user.ID) + require.NoError(t, err) + + freeze, warning, err := planet.Satellites[0].DB.Console().AccountFreezeEvents().GetAll(ctx, user.ID) + require.NoError(t, err) + require.Nil(t, freeze) + require.NotNil(t, warning) + + link := fmt.Sprintf("http://"+address.String()+"/api/users/%s/warning", user.Email) + body := assertReq(ctx, t, link, http.MethodDelete, "", http.StatusOK, "", planet.Satellites[0].Config.Console.AuthToken) + require.Len(t, body, 0) + + freeze, warning, err = planet.Satellites[0].DB.Console().AccountFreezeEvents().GetAll(ctx, user.ID) + require.NoError(t, err) + require.Nil(t, freeze) + require.Nil(t, warning) + + body = assertReq(ctx, t, link, http.MethodDelete, "", http.StatusInternalServerError, "", planet.Satellites[0].Config.Console.AuthToken) + require.Contains(t, string(body), "user is not warned") + }) +} + func TestUserDelete(t *testing.T) { testplanet.Run(t, testplanet.Config{ SatelliteCount: 1,