diff --git a/controllers/middlewares/middlewares.go b/controllers/middlewares/middlewares.go index a0b3db71..283b39a1 100644 --- a/controllers/middlewares/middlewares.go +++ b/controllers/middlewares/middlewares.go @@ -46,6 +46,17 @@ func ModMiddleware() gin.HandlerFunc { } } +// LoggedInMiddleware make sure that the user is logged in +func LoggedInMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + currentUser := router.GetUser(c) + if currentUser.ID == 0 { + NotFoundHandler(c) + } + c.Next() + } +} + func ScopesRequired(scopes ...string) gin.HandlerFunc { return func(c *gin.Context) { mySessionData := oauth2.NewSession("", "") diff --git a/controllers/torrent/router.go b/controllers/torrent/router.go index 73241104..aa307e8a 100644 --- a/controllers/torrent/router.go +++ b/controllers/torrent/router.go @@ -1,11 +1,14 @@ package torrentController -import "github.com/NyaaPantsu/nyaa/controllers/router" +import ( + "github.com/NyaaPantsu/nyaa/controllers/middlewares" + "github.com/NyaaPantsu/nyaa/controllers/router" +) func init() { router.Get().Any("/download/:hash", DownloadTorrent) - torrentRoutes := router.Get().Group("/torrent") + torrentRoutes := router.Get().Group("/torrent", middlewares.LoggedInMiddleware()) { torrentRoutes.GET("/", TorrentEditUserPanel) torrentRoutes.POST("/", TorrentPostEditUserPanel) diff --git a/controllers/torrent/tag.go b/controllers/torrent/tag.go index d105e2e0..2d5e7e04 100644 --- a/controllers/torrent/tag.go +++ b/controllers/torrent/tag.go @@ -64,36 +64,36 @@ func postTags(c *gin.Context, torrent *models.Torrent, user *models.User) []mode // ViewFormTag is a controller displaying a form to add a tag to a torrent func ViewFormTag(c *gin.Context) { - messages := msg.GetMessages(c) user := router.GetUser(c) - id, _ := strconv.ParseInt(c.Query("id"), 10, 32) - // Retrieve the torrent - torrent, err := torrents.FindByID(uint(id)) - - // If torrent not found, display 404 - if err != nil { - c.Status(http.StatusNotFound) - return - } - - // We add a tag if posted + // We add a tag only if user logged if user.ID > 0 { + messages := msg.GetMessages(c) + id, _ := strconv.ParseInt(c.Query("id"), 10, 32) + // Retrieve the torrent + torrent, err := torrents.FindByID(uint(id)) + + var tagsForm models.Tags + // If torrent not found, display 404 + if err != nil { + c.Status(http.StatusNotFound) + return + } // We load tags for user so we can check if they have them user.LoadTags(torrent) - tag := postTags(c, torrent, user) - if _, ok := c.GetQuery("json"); ok { - apiUtils.ResponseHandler(c, tag) - return + if c.Request.Method == "POST" { + tagsForm = postTags(c, torrent, user) + if _, ok := c.GetQuery("json"); ok { + apiUtils.ResponseHandler(c, tagsForm) + return + } + if !messages.HasErrors() { + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id)) + } } - if !messages.HasErrors() { - c.Redirect(http.StatusSeeOther, fmt.Sprintf("/view/%d", id)) - } - } - tagForm := &tagsValidator.CreateForm{} - c.Bind(tagForm) - templates.Form(c, "/site/torrents/tag.jet.html", tagForm) + templates.Form(c, "/site/torrents/tag.jet.html", tagsForm) + } } // AddTag is a controller to add a tag diff --git a/templates/layouts/partials/helpers/tags.jet.html b/templates/layouts/partials/helpers/tags.jet.html index 2cf41108..cf8fca4b 100644 --- a/templates/layouts/partials/helpers/tags.jet.html +++ b/templates/layouts/partials/helpers/tags.jet.html @@ -2,7 +2,8 @@ {{if isset(tag) && (accepted || User.ID > 0) }} - {{ if tag.Type != Config.Torrents.Tags.Default }}{{ T("tagtype_" + tag.Type) }}: {{ end }}{{ tag.Tag }} + {{ if tag.Type != Config.Torrents.Tags.Default }}{{ T("tagtype_" + tag.Type) }}: {{ end }} + {{ if len(Config.Torrents.Tags.Types.Get(tag.Type).Defaults) > 0 && Config.Torrents.Tags.Types.Get(tag.Type).Defaults[0] != "db" }}{{ T("tagvalue_"+tag.Tag) }}{{else}}{{ tag.Tag }}{{end}} {{ if !accepted }} diff --git a/templates/site/torrents/tag.jet.html b/templates/site/torrents/tag.jet.html index 4363ede9..790f48e1 100644 --- a/templates/site/torrents/tag.jet.html +++ b/templates/site/torrents/tag.jet.html @@ -1,25 +1,21 @@ {{ extends "layouts/index_site" }} {{ import "layouts/partials/helpers/csrf" }} {{ import "layouts/partials/helpers/errors" }} +{{ import "layouts/partials/helpers/tag_form" }} {{block title()}}{{T("add_tag")}}{{end}} {{block content_body()}}

{{ T("add_tag") }}

- {{ yield csrf_field() }} -
- - - {{ yield errors(name="Tag")}} -
-
- - - {{ yield errors(name="Type")}} +
+ {{ yield csrf_field() }} + {{ range Config.Torrents.Tags.Types }} + {{ yield tagForm(tagType=., acceptedTag=Form.Get(.Name)) }} + {{ end }} +
+ + +

diff --git a/templates/template_test.go b/templates/template_test.go index c7861208..0d9a621e 100644 --- a/templates/template_test.go +++ b/templates/template_test.go @@ -207,7 +207,7 @@ func walkDirTest(dir string, t *testing.T) { return variables }, "tag.jet.html": func(variables jet.VarMap) jet.VarMap { - variables.Set("Form", fakeTag) + variables.Set("Form", models.Tags{*fakeTag, *fakeTag, *fakeTag}) return variables }, }