package handlers import ( "fmt" "strconv" "strings" "github.com/gofiber/fiber/v2" "polar/internal/web/middleware" ) // RepoCollaboratorAddSubmit grants a user access to the repository. func (h *Handler) RepoCollaboratorAddSubmit(c *fiber.Ctx) error { r, _, err := h.loadAuthorizedRepo(c, true) if err != nil { return nil } viewer := middleware.CurrentUser(c) if viewer == nil || viewer.ID != r.OwnerID { return h.renderErrorT(c, fiber.StatusForbidden, "err.forbidden", "err.collab_forbidden") } username := strings.TrimSpace(c.FormValue("username")) role := strings.TrimSpace(c.FormValue("role")) if err := h.repoSvc.AddCollaborator(c.UserContext(), r, username, role); err != nil { h.setFlashT(c, "flash.collab_failed", err.Error()) } else { h.setFlashT(c, "flash.collab_added", username) } return c.Redirect(fmt.Sprintf("/%s/%s/settings#collaborators", r.OwnerUsername, r.Name), fiber.StatusSeeOther) } // RepoCollaboratorRemoveSubmit revokes a user's access to the repository. func (h *Handler) RepoCollaboratorRemoveSubmit(c *fiber.Ctx) error { r, _, err := h.loadAuthorizedRepo(c, true) if err != nil { return nil } viewer := middleware.CurrentUser(c) if viewer == nil || viewer.ID != r.OwnerID { return h.renderErrorT(c, fiber.StatusForbidden, "err.forbidden", "err.collab_forbidden") } userID, _ := strconv.ParseInt(strings.TrimSpace(c.FormValue("user_id")), 10, 64) if userID > 0 { if err := h.repoSvc.RemoveCollaborator(c.UserContext(), r.ID, userID); err != nil { h.setFlashT(c, "flash.collab_failed", err.Error()) } else { h.setFlashT(c, "flash.collab_removed") } } return c.Redirect(fmt.Sprintf("/%s/%s/settings#collaborators", r.OwnerUsername, r.Name), fiber.StatusSeeOther) }