From 045a2c2379cf079469274a317d77abb54652fdc6 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 10 Sep 2022 16:52:02 -0500 Subject: [PATCH] Add education toggle to user admin settings --- src/hmnurl/hmnurl_test.go | 2 +- src/hmnurl/urls.go | 6 ++--- src/templates/src/user_profile.html | 31 +++++++++++++++++--------- src/website/routes.go | 2 +- src/website/user.go | 34 ++++++++++++++++++++--------- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/hmnurl/hmnurl_test.go b/src/hmnurl/hmnurl_test.go index ab3c890e..16d4aed1 100644 --- a/src/hmnurl/hmnurl_test.go +++ b/src/hmnurl/hmnurl_test.go @@ -112,7 +112,7 @@ func TestUserSettings(t *testing.T) { func TestAdmin(t *testing.T) { AssertRegexMatch(t, BuildAdminAtomFeed(), RegexAdminAtomFeed, nil) AssertRegexMatch(t, BuildAdminApprovalQueue(), RegexAdminApprovalQueue, nil) - AssertRegexMatch(t, BuildAdminSetUserStatus(), RegexAdminSetUserStatus, nil) + AssertRegexMatch(t, BuildAdminSetUserOptions(), RegexAdminSetUserOptions, nil) AssertRegexMatch(t, BuildAdminNukeUser(), RegexAdminNukeUser, nil) } diff --git a/src/hmnurl/urls.go b/src/hmnurl/urls.go index 8b6b537b..9f566723 100644 --- a/src/hmnurl/urls.go +++ b/src/hmnurl/urls.go @@ -245,11 +245,11 @@ func BuildAdminApprovalQueue() string { return Url("/admin/approvals", nil) } -var RegexAdminSetUserStatus = regexp.MustCompile(`^/admin/setuserstatus$`) +var RegexAdminSetUserOptions = regexp.MustCompile(`^/admin/setuseroptions$`) -func BuildAdminSetUserStatus() string { +func BuildAdminSetUserOptions() string { defer CatchPanic() - return Url("/admin/setuserstatus", nil) + return Url("/admin/setuseroptions", nil) } var RegexAdminNukeUser = regexp.MustCompile(`^/admin/nukeuser$`) diff --git a/src/templates/src/user_profile.html b/src/templates/src/user_profile.html index 961afbb8..39627f4d 100644 --- a/src/templates/src/user_profile.html +++ b/src/templates/src/user_profile.html @@ -73,22 +73,33 @@
-
-
User status:
-
- {{ csrftoken .Session }} - - + + {{ csrftoken .Session }} + + +
+
User status:
- +
Only sets status. Doesn't delete anything.
- -
+
+
+
Education role:
+ +
+
+ +
+
Danger zone:
@@ -110,7 +121,7 @@ panelEl.style.display = "none"; }); - document.querySelector("#admin_set_status_form").addEventListener("submit", function(ev) { + document.querySelector("#admin_set_options_form").addEventListener("submit", function(ev) { if (!adminUnlocked) { ev.preventDefault(); } diff --git a/src/website/routes.go b/src/website/routes.go index fa85404d..4f225aef 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -81,7 +81,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool) http.Handler { hmnOnly.GET(hmnurl.RegexAdminAtomFeed, AdminAtomFeed) hmnOnly.GET(hmnurl.RegexAdminApprovalQueue, adminsOnly(AdminApprovalQueue)) hmnOnly.POST(hmnurl.RegexAdminApprovalQueue, adminsOnly(csrfMiddleware(AdminApprovalQueueSubmit))) - hmnOnly.POST(hmnurl.RegexAdminSetUserStatus, adminsOnly(csrfMiddleware(UserProfileAdminSetStatus))) + hmnOnly.POST(hmnurl.RegexAdminSetUserOptions, adminsOnly(csrfMiddleware(UserProfileAdminSetOptions))) hmnOnly.POST(hmnurl.RegexAdminNukeUser, adminsOnly(csrfMiddleware(UserProfileAdminNuke))) hmnOnly.GET(hmnurl.RegexFeed, Feed) diff --git a/src/website/user.go b/src/website/user.go index 66ad5cb8..2e6331ab 100644 --- a/src/website/user.go +++ b/src/website/user.go @@ -36,8 +36,8 @@ type UserProfileTemplateData struct { CanAddProject bool NewProjectUrl string - AdminSetStatusUrl string - AdminNukeUrl string + AdminSetOptionsUrl string + AdminNukeUrl string SnippetEdit templates.SnippetEdit } @@ -194,8 +194,8 @@ func UserProfile(c *RequestContext) ResponseData { CanAddProject: numPersonalProjects < maxPersonalProjects, NewProjectUrl: hmnurl.BuildProjectNew(), - AdminSetStatusUrl: hmnurl.BuildAdminSetUserStatus(), - AdminNukeUrl: hmnurl.BuildAdminNukeUser(), + AdminSetOptionsUrl: hmnurl.BuildAdminSetUserOptions(), + AdminNukeUrl: hmnurl.BuildAdminNukeUser(), SnippetEdit: snippetEdit, }, c.Perf) @@ -479,7 +479,7 @@ func UserSettingsSave(c *RequestContext) ResponseData { return res } -func UserProfileAdminSetStatus(c *RequestContext) ResponseData { +func UserProfileAdminSetOptions(c *RequestContext) ResponseData { c.Req.ParseForm() userIdStr := c.Req.Form.Get("user_id") @@ -503,17 +503,31 @@ func UserProfileAdminSetStatus(c *RequestContext) ResponseData { return c.RejectRequest("No legal user status provided") } + eduRole := c.Req.Form.Get("edu_role") + var desiredEduRole models.EduRole + switch eduRole { + case "none": + desiredEduRole = models.EduRoleNone + case "beta": + desiredEduRole = models.EduRoleBeta + case "author": + desiredEduRole = models.EduRoleAuthor + default: + return c.RejectRequest("the education role is bad and you should feel bad") + } + _, err = c.Conn.Exec(c, ` UPDATE hmn_user - SET status = $1 - WHERE id = $2 + SET status = $2, education_role = $3 + WHERE id = $1 `, - desiredStatus, userId, + desiredStatus, + desiredEduRole, ) if err != nil { - return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update user status")) + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update user admin settings")) } if desiredStatus == models.UserStatusBanned { err = auth.DeleteSessionForUser(c, c.Conn, c.Req.Form.Get("username")) @@ -522,7 +536,7 @@ func UserProfileAdminSetStatus(c *RequestContext) ResponseData { } } res := c.Redirect(hmnurl.BuildUserProfile(c.Req.Form.Get("username")), http.StatusSeeOther) - res.AddFutureNotice("success", "Successfully set status") + res.AddFutureNotice("success", "Successfully set admin options") return res }