rin
Public Open-source lightweight git hosting.
admin.go · 115 lines · 4.0 KB
Raw
1 package handlers
2
3 import (
4 "errors"
5 "strconv"
6 "strings"
7
8 "polar/internal/user"
9 "polar/internal/web/middleware"
10
11 "github.com/gofiber/fiber/v2"
12 )
13
14 func (h *Handler) AdminApproveUserSubmit(c *fiber.Ctx) error {
15 id, err := strconv.ParseInt(c.Params("id"), 10, 64)
16 if err != nil {
17 return c.Redirect("/admin", fiber.StatusSeeOther)
18 }
19 target, err := h.userSvc.GetByID(c.UserContext(), id)
20 if err != nil {
21 h.setFlashT(c, "flash.admin_user_missing")
22 return c.Redirect("/admin", fiber.StatusSeeOther)
23 }
24 if err := h.userSvc.SetApproved(c.UserContext(), target.ID, true); err != nil {
25 h.logger.Error("approve account failed", "user_id", target.ID, "err", err)
26 h.setFlashT(c, "flash.admin_user_update_failed")
27 return c.Redirect("/admin", fiber.StatusSeeOther)
28 }
29 h.setFlashT(c, "flash.admin_user_approved", target.Username)
30 return c.Redirect("/admin", fiber.StatusSeeOther)
31 }
32
33 func (h *Handler) AdminToggleUserAdminSubmit(c *fiber.Ctx) error {
34 id, err := strconv.ParseInt(c.Params("id"), 10, 64)
35 if err != nil {
36 return c.Redirect("/admin", fiber.StatusSeeOther)
37 }
38 current := middleware.CurrentUser(c)
39 target, err := h.userSvc.GetByID(c.UserContext(), id)
40 if err != nil {
41 h.setFlashT(c, "flash.admin_user_missing")
42 return c.Redirect("/admin", fiber.StatusSeeOther)
43 }
44 if current != nil && target.ID == current.ID {
45 h.setFlashT(c, "flash.admin_self_role")
46 return c.Redirect("/admin", fiber.StatusSeeOther)
47 }
48 if err := h.userSvc.SetAdmin(c.UserContext(), target.ID, !target.IsAdmin); err != nil {
49 if errors.Is(err, user.ErrLastAdmin) {
50 h.setFlashT(c, "flash.admin_last_admin")
51 } else {
52 h.logger.Error("update administrator role failed", "user_id", target.ID, "err", err)
53 h.setFlashT(c, "flash.admin_user_update_failed")
54 }
55 return c.Redirect("/admin", fiber.StatusSeeOther)
56 }
57 h.setFlashT(c, "flash.admin_role_updated", target.Username)
58 return c.Redirect("/admin", fiber.StatusSeeOther)
59 }
60
61 func (h *Handler) AdminDeleteUserSubmit(c *fiber.Ctx) error {
62 id, err := strconv.ParseInt(c.Params("id"), 10, 64)
63 if err != nil {
64 return c.Redirect("/admin", fiber.StatusSeeOther)
65 }
66 current := middleware.CurrentUser(c)
67 target, err := h.userSvc.GetByID(c.UserContext(), id)
68 if err != nil {
69 h.setFlashT(c, "flash.admin_user_missing")
70 return c.Redirect("/admin", fiber.StatusSeeOther)
71 }
72 if current != nil && target.ID == current.ID {
73 h.setFlashT(c, "flash.admin_delete_self")
74 return c.Redirect("/admin", fiber.StatusSeeOther)
75 }
76 if !strings.EqualFold(strings.TrimSpace(c.FormValue("confirm_username")), target.Username) {
77 h.setFlashT(c, "flash.admin_delete_mismatch")
78 return c.Redirect("/admin", fiber.StatusSeeOther)
79 }
80
81 if err := h.deleteAccount(c.UserContext(), target); err != nil {
82 h.logger.Error("admin delete account failed", "user_id", target.ID, "err", err)
83 if errors.Is(err, user.ErrLastAdmin) {
84 h.setFlashT(c, "flash.admin_last_admin")
85 } else {
86 h.setFlashT(c, "flash.admin_user_delete_failed")
87 }
88 return c.Redirect("/admin", fiber.StatusSeeOther)
89 }
90 h.setFlashT(c, "flash.admin_user_deleted", target.Username)
91 return c.Redirect("/admin", fiber.StatusSeeOther)
92 }
93
94 func (h *Handler) AdminDeleteRepoSubmit(c *fiber.Ctx) error {
95 id, err := strconv.ParseInt(c.Params("id"), 10, 64)
96 if err != nil {
97 return c.Redirect("/admin", fiber.StatusSeeOther)
98 }
99 r, err := h.repoSvc.GetByID(c.UserContext(), id)
100 if err != nil {
101 h.setFlashT(c, "flash.admin_repo_missing")
102 return c.Redirect("/admin", fiber.StatusSeeOther)
103 }
104 if !strings.EqualFold(strings.TrimSpace(c.FormValue("confirm_name")), r.Name) {
105 h.setFlashT(c, "flash.admin_delete_mismatch")
106 return c.Redirect("/admin", fiber.StatusSeeOther)
107 }
108 if err := h.repoSvc.Delete(c.UserContext(), r); err != nil {
109 h.logger.Error("admin delete repository failed", "repo_id", r.ID, "err", err)
110 h.setFlashT(c, "flash.admin_repo_delete_failed")
111 return c.Redirect("/admin", fiber.StatusSeeOther)
112 }
113 h.setFlashT(c, "flash.admin_repo_deleted", r.OwnerUsername, r.Name)
114 return c.Redirect("/admin", fiber.StatusSeeOther)
115 }
116