From 04839064b75e41032011c3aac83373f081c7d721 Mon Sep 17 00:00:00 2001 From: Ilia Frenkel Date: Thu, 29 Jul 2021 17:59:50 +1000 Subject: [PATCH] Implement paste privacy (#27) --- src/api/api.go | 2 ++ src/api/http/http.go | 17 ++++++++--- src/api/http/http_test.go | 2 ++ src/api/paste/memory/memory.go | 1 + src/api/paste/sqldb/sqldb.go | 1 + src/web/http/http.go | 12 +++++++- src/web/templates/form.html | 54 +++++++++++++++++++--------------- src/web/templates/header.html | 4 ++- src/web/templates/index.html | 2 +- src/web/templates/paste.html | 21 +++++++++++-- src/web/templates/view.html | 29 +++++++++++++++--- 11 files changed, 107 insertions(+), 38 deletions(-) diff --git a/src/api/api.go b/src/api/api.go index d065f4f..1988b63 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -21,6 +21,7 @@ type Paste struct { Body string `json:"body"` Expires time.Time `json:"expires" gorm:"index"` DeleteAfterRead bool `json:"delete_after_read"` + Privacy string `json:"privacy"` Password string `json:"password"` Created time.Time `json:"created"` Syntax string `json:"syntax"` @@ -105,6 +106,7 @@ type PasteForm struct { Body string `json:"body" form:"body" binding:"required"` Expires string `json:"expires" form:"expires" binding:"required"` DeleteAfterRead bool `json:"delete_after_read" form:"delete_after_read" binding:"-"` + Privacy string `json:"privacy" form:"privacy" binding:"required"` Password string `json:"password" form:"password"` Syntax string `json:"syntax" form:"syntax" binding:"required"` UserID int64 `json:"user_id"` diff --git a/src/api/http/http.go b/src/api/http/http.go index 29ae015..773b2ab 100644 --- a/src/api/http/http.go +++ b/src/api/http/http.go @@ -348,10 +348,10 @@ func (h *APIServer) handlePasteGetWithPassword(c *gin.Context) { // prepared it for us. var pwd string if data, ok := c.Get("payload"); !ok { - log.Println("handlePasteGetWithPassword: unexpected error: ", err.Error()) + log.Println("handlePasteGetWithPassword: unexpected error: can't get the payload") c.JSON(http.StatusInternalServerError, api.HTTPError{ Code: http.StatusInternalServerError, - Message: fmt.Sprintf("%s: %s", http.StatusText(http.StatusInternalServerError), err.Error()), + Message: fmt.Sprintf("%s: can't get the payload", http.StatusText(http.StatusInternalServerError)), }) return } else { @@ -407,9 +407,18 @@ func (h *APIServer) handlePasteGetWithPassword(c *gin.Context) { // error. Body size is currently limited to a configurable value of // Options.MaxBodySize. func (h *APIServer) handlePasteCreate(c *gin.Context) { - data := c.MustGet("payload").(*api.PasteForm) + var data interface{} + var ok bool + if data, ok = c.Get("payload"); !ok { + log.Println("handlePasteGetWithPassword: unexpected error: can't get the payload") + c.JSON(http.StatusInternalServerError, api.HTTPError{ + Code: http.StatusInternalServerError, + Message: fmt.Sprintf("%s: can't get the payload", http.StatusText(http.StatusInternalServerError)), + }) + return + } - p, err := h.PasteService.Create(*data) + p, err := h.PasteService.Create(*data.(*api.PasteForm)) if err != nil { log.Printf("handleCreate: failed to create paste: %v\n", err) c.JSON(http.StatusBadRequest, api.HTTPError{ diff --git a/src/api/http/http_test.go b/src/api/http/http_test.go index e373068..258f2a7 100644 --- a/src/api/http/http_test.go +++ b/src/api/http/http_test.go @@ -28,6 +28,7 @@ func createTestPaste() *api.PasteForm { Body: "Test body", Expires: "never", DeleteAfterRead: false, + Privacy: "public", Password: "", Syntax: "none", UserID: 0, @@ -343,6 +344,7 @@ func Test_CreatePasteWrongFieldType(t *testing.T) { "title": 1, "body": "body", "expires":"never", + "privacy":"public", "syntax":"none" }` resp, err := http.Post(mckSrv.URL+"/paste", "application/json", bytes.NewBuffer([]byte(body))) diff --git a/src/api/paste/memory/memory.go b/src/api/paste/memory/memory.go index 4146750..2f998e4 100644 --- a/src/api/paste/memory/memory.go +++ b/src/api/paste/memory/memory.go @@ -96,6 +96,7 @@ func (s *PasteService) Create(p api.PasteForm) (*api.Paste, error) { Body: p.Body, Expires: expires, DeleteAfterRead: p.DeleteAfterRead, + Privacy: p.Privacy, Password: p.Password, Created: created, Syntax: p.Syntax, diff --git a/src/api/paste/sqldb/sqldb.go b/src/api/paste/sqldb/sqldb.go index a786ed0..2f666fa 100644 --- a/src/api/paste/sqldb/sqldb.go +++ b/src/api/paste/sqldb/sqldb.go @@ -119,6 +119,7 @@ func (s *PasteService) Create(p api.PasteForm) (*api.Paste, error) { Body: p.Body, Expires: expires, DeleteAfterRead: p.DeleteAfterRead, + Privacy: p.Privacy, Password: p.Password, Created: created, Syntax: p.Syntax, diff --git a/src/web/http/http.go b/src/web/http/http.go index 5b8d278..e4a67a6 100644 --- a/src/web/http/http.go +++ b/src/web/http/http.go @@ -554,8 +554,18 @@ func (h *WebServer) handleGetPaste(c *gin.Context) { return } - // Get user pastes userid, _ := c.Get("user_id") + + // Check if paste is private + if p.Privacy == "private" && p.UserID != userid { + c.Set("errorCode", http.StatusNotFound) + c.Set("errorText", http.StatusText(http.StatusNotFound)) + c.Set("errorMessage", "The paste cannot be found.") + h.showError(c) + return + } + + // Get user pastes var pastes []api.Paste if userid != nil && userid.(int64) != 0 { diff --git a/src/web/templates/form.html b/src/web/templates/form.html index 0af94b7..7086f35 100644 --- a/src/web/templates/form.html +++ b/src/web/templates/form.html @@ -1,16 +1,18 @@
-
- - -
-
- - -
-
-
- + +
+
+ + +
+
+
+
+
-
-
-
+
-
-
-
+
-
-
-
+
+ + +
+
-
- - +
+
+ + +
\ No newline at end of file diff --git a/src/web/templates/header.html b/src/web/templates/header.html index 8f07c10..b757070 100644 --- a/src/web/templates/header.html +++ b/src/web/templates/header.html @@ -20,9 +20,11 @@

{{.username}} {{else}} diff --git a/src/web/templates/index.html b/src/web/templates/index.html index 4afe0b4..e4b2719 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -11,7 +11,7 @@