rin
Публичный Open-source lightweight git hosting.
collaborators.go · 55 строк · 1.7 KB
Raw
1 package handlers
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7
8 "github.com/gofiber/fiber/v2"
9
10 "polar/internal/web/middleware"
11 )
12
13 // RepoCollaboratorAddSubmit grants a user access to the repository.
14 func (h *Handler) RepoCollaboratorAddSubmit(c *fiber.Ctx) error {
15 r, _, err := h.loadAuthorizedRepo(c, true)
16 if err != nil {
17 return nil
18 }
19 viewer := middleware.CurrentUser(c)
20 if viewer == nil || viewer.ID != r.OwnerID {
21 return h.renderErrorT(c, fiber.StatusForbidden, "err.forbidden", "err.collab_forbidden")
22 }
23
24 username := strings.TrimSpace(c.FormValue("username"))
25 role := strings.TrimSpace(c.FormValue("role"))
26
27 if err := h.repoSvc.AddCollaborator(c.UserContext(), r, username, role); err != nil {
28 h.setFlashT(c, "flash.collab_failed", err.Error())
29 } else {
30 h.setFlashT(c, "flash.collab_added", username)
31 }
32 return c.Redirect(fmt.Sprintf("/%s/%s/settings#collaborators", r.OwnerUsername, r.Name), fiber.StatusSeeOther)
33 }
34
35 // RepoCollaboratorRemoveSubmit revokes a user's access to the repository.
36 func (h *Handler) RepoCollaboratorRemoveSubmit(c *fiber.Ctx) error {
37 r, _, err := h.loadAuthorizedRepo(c, true)
38 if err != nil {
39 return nil
40 }
41 viewer := middleware.CurrentUser(c)
42 if viewer == nil || viewer.ID != r.OwnerID {
43 return h.renderErrorT(c, fiber.StatusForbidden, "err.forbidden", "err.collab_forbidden")
44 }
45
46 userID, _ := strconv.ParseInt(strings.TrimSpace(c.FormValue("user_id")), 10, 64)
47 if userID > 0 {
48 if err := h.repoSvc.RemoveCollaborator(c.UserContext(), r.ID, userID); err != nil {
49 h.setFlashT(c, "flash.collab_failed", err.Error())
50 } else {
51 h.setFlashT(c, "flash.collab_removed")
52 }
53 }
54 return c.Redirect(fmt.Sprintf("/%s/%s/settings#collaborators", r.OwnerUsername, r.Name), fiber.StatusSeeOther)
55 }
56