Fix getting user info from /user endpoint for link shares

This commit is contained in:
kolaente 2021-04-07 15:02:57 +02:00
parent cb0df3ebbc
commit 8ddc00bd29
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
7 changed files with 12 additions and 16 deletions

View file

@ -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" // @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id}/buckets [put] // @Router /lists/{id}/buckets [put]
func (b *Bucket) Create(s *xorm.Session, a web.Auth) (err error) { 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 { if err != nil {
return return
} }

View file

@ -65,7 +65,7 @@ func (ta *TaskAttachment) NewAttachment(s *xorm.Session, f io.ReadCloser, realna
// Add an entry to the db // Add an entry to the db
ta.FileID = file.ID ta.FileID = file.ID
ta.CreatedBy, err = getUserOrLinkShareUser(s, a) ta.CreatedBy, err = GetUserOrLinkShareUser(s, a)
if err != nil { if err != nil {
// remove the uploaded file if adding it to the db fails // remove the uploaded file if adding it to the db fails
if err2 := file.Delete(); err2 != nil { if err2 := file.Delete(); err2 != nil {

View file

@ -67,7 +67,7 @@ func (tc *TaskComment) Create(s *xorm.Session, a web.Auth) (err error) {
return err return err
} }
tc.Author, err = getUserOrLinkShareUser(s, a) tc.Author, err = GetUserOrLinkShareUser(s, a)
if err != nil { if err != nil {
return err return err
} }

View file

@ -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 { if err != nil {
return err return err
} }

View file

@ -817,7 +817,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
return err return err
} }
createdBy, err := getUserOrLinkShareUser(s, a) createdBy, err := GetUserOrLinkShareUser(s, a)
if err != nil { if err != nil {
return err return err
} }

View file

@ -22,8 +22,8 @@ import (
"xorm.io/xorm" "xorm.io/xorm"
) )
// Returns either a user or a link share disguised as a user. // 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) { func GetUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err error) {
if u, is := a.(*user.User); is { if u, is := a.(*user.User); is {
uu, err = user.GetUserByID(s, u.ID) uu, err = user.GetUserByID(s, u.ID)
return return

View file

@ -19,9 +19,11 @@ package v1
import ( import (
"net/http" "net/http"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/auth"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
user2 "code.vikunja.io/api/pkg/user"
"code.vikunja.io/web/handler" "code.vikunja.io/web/handler"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -38,7 +40,7 @@ import (
// @Failure 500 {object} models.Message "Internal server error." // @Failure 500 {object} models.Message "Internal server error."
// @Router /user [get] // @Router /user [get]
func UserShow(c echo.Context) error { func UserShow(c echo.Context) error {
userInfos, err := user2.GetCurrentUser(c) a, err := auth.GetAuthFromClaims(c)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Error getting current user.") return echo.NewHTTPError(http.StatusInternalServerError, "Error getting current user.")
} }
@ -46,14 +48,8 @@ func UserShow(c echo.Context) error {
s := db.NewSession() s := db.NewSession()
defer s.Close() defer s.Close()
user, err := user2.GetUserByID(s, userInfos.ID) user, err := models.GetUserOrLinkShareUser(s, a)
if err != nil { if err != nil {
_ = s.Rollback()
return handler.HandleHTTPError(err, c)
}
if err := s.Commit(); err != nil {
_ = s.Rollback()
return handler.HandleHTTPError(err, c) return handler.HandleHTTPError(err, c)
} }