From 8ddc00bd29e1c67a514e2c9c314edf422a1b575b Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 7 Apr 2021 15:02:57 +0200 Subject: [PATCH] Fix getting user info from /user endpoint for link shares --- pkg/models/kanban.go | 2 +- pkg/models/task_attachment.go | 2 +- pkg/models/task_comments.go | 2 +- pkg/models/task_relation.go | 2 +- pkg/models/tasks.go | 2 +- pkg/models/users.go | 4 ++-- pkg/routes/api/v1/user_show.go | 14 +++++--------- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/pkg/models/kanban.go b/pkg/models/kanban.go index 1e827a51..d9dbd6ef 100644 --- a/pkg/models/kanban.go +++ b/pkg/models/kanban.go @@ -231,7 +231,7 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int // @Failure 500 {object} models.Message "Internal error" // @Router /lists/{id}/buckets [put] func (b *Bucket) Create(s *xorm.Session, a web.Auth) (err error) { - b.CreatedBy, err = getUserOrLinkShareUser(s, a) + b.CreatedBy, err = GetUserOrLinkShareUser(s, a) if err != nil { return } diff --git a/pkg/models/task_attachment.go b/pkg/models/task_attachment.go index 8ab8f56e..70708feb 100644 --- a/pkg/models/task_attachment.go +++ b/pkg/models/task_attachment.go @@ -65,7 +65,7 @@ func (ta *TaskAttachment) NewAttachment(s *xorm.Session, f io.ReadCloser, realna // Add an entry to the db ta.FileID = file.ID - ta.CreatedBy, err = getUserOrLinkShareUser(s, a) + ta.CreatedBy, err = GetUserOrLinkShareUser(s, a) if err != nil { // remove the uploaded file if adding it to the db fails if err2 := file.Delete(); err2 != nil { diff --git a/pkg/models/task_comments.go b/pkg/models/task_comments.go index d213ef88..7d0c7393 100644 --- a/pkg/models/task_comments.go +++ b/pkg/models/task_comments.go @@ -67,7 +67,7 @@ func (tc *TaskComment) Create(s *xorm.Session, a web.Auth) (err error) { return err } - tc.Author, err = getUserOrLinkShareUser(s, a) + tc.Author, err = GetUserOrLinkShareUser(s, a) if err != nil { return err } diff --git a/pkg/models/task_relation.go b/pkg/models/task_relation.go index b0bdd98b..b0d66536 100644 --- a/pkg/models/task_relation.go +++ b/pkg/models/task_relation.go @@ -144,7 +144,7 @@ func (rel *TaskRelation) Create(s *xorm.Session, a web.Auth) error { } } - rel.CreatedBy, err = getUserOrLinkShareUser(s, a) + rel.CreatedBy, err = GetUserOrLinkShareUser(s, a) if err != nil { return err } diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 14c6431e..9c81d5be 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -817,7 +817,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err return err } - createdBy, err := getUserOrLinkShareUser(s, a) + createdBy, err := GetUserOrLinkShareUser(s, a) if err != nil { return err } diff --git a/pkg/models/users.go b/pkg/models/users.go index 84fd3811..b29bb0c4 100644 --- a/pkg/models/users.go +++ b/pkg/models/users.go @@ -22,8 +22,8 @@ import ( "xorm.io/xorm" ) -// Returns either a user or a link share disguised as a user. -func getUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err error) { +// GetUserOrLinkShareUser returns either a user or a link share disguised as a user. +func GetUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err error) { if u, is := a.(*user.User); is { uu, err = user.GetUserByID(s, u.ID) return diff --git a/pkg/routes/api/v1/user_show.go b/pkg/routes/api/v1/user_show.go index 512f0c0b..2265089b 100644 --- a/pkg/routes/api/v1/user_show.go +++ b/pkg/routes/api/v1/user_show.go @@ -19,9 +19,11 @@ package v1 import ( "net/http" + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/modules/auth" + "code.vikunja.io/api/pkg/db" - user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" ) @@ -38,7 +40,7 @@ import ( // @Failure 500 {object} models.Message "Internal server error." // @Router /user [get] func UserShow(c echo.Context) error { - userInfos, err := user2.GetCurrentUser(c) + a, err := auth.GetAuthFromClaims(c) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Error getting current user.") } @@ -46,14 +48,8 @@ func UserShow(c echo.Context) error { s := db.NewSession() defer s.Close() - user, err := user2.GetUserByID(s, userInfos.ID) + user, err := models.GetUserOrLinkShareUser(s, a) if err != nil { - _ = s.Rollback() - return handler.HandleHTTPError(err, c) - } - - if err := s.Commit(); err != nil { - _ = s.Rollback() return handler.HandleHTTPError(err, c) }