From 33b9917c7dc9127f5c63ff4db15f8d2fca7dc1d7 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 14 Jan 2025 15:01:13 +0100 Subject: [PATCH] fix(auth): return correct error when trying to do a user action as a link share Resolves https://vikunja.sentry.io/share/issue/77f7aa4cda9442439deba3cfe2cb32c6/ --- pkg/user/error.go | 27 +++++++++++++++++++++++++++ pkg/user/user.go | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/user/error.go b/pkg/user/error.go index 750a1bcc7..2903f193c 100644 --- a/pkg/user/error.go +++ b/pkg/user/error.go @@ -532,3 +532,30 @@ func (err *ErrUsernameMustNotContainSpaces) HTTPError() web.HTTPError { Message: "The username must not contain spaces.", } } + +// ErrMustNotBeLinkShare represents a "MustNotBeLinkShare" kind of error. +type ErrMustNotBeLinkShare struct { + Username string +} + +// IsErrMustNotBeLinkShare checks if an error is a ErrMustNotBeLinkShare. +func IsErrMustNotBeLinkShare(err error) bool { + _, ok := err.(*ErrMustNotBeLinkShare) + return ok +} + +func (err *ErrMustNotBeLinkShare) Error() string { + return "user must be a *User, not a *models.LinkSharing" +} + +// ErrCodeMustNotBeLinkShare holds the unique world-error code of this error +const ErrCodeMustNotBeLinkShare = 1023 + +// HTTPError holds the http error description +func (err *ErrMustNotBeLinkShare) HTTPError() web.HTTPError { + return web.HTTPError{ + HTTPCode: http.StatusForbidden, + Code: ErrCodeMustNotBeLinkShare, + Message: "You can't do that as a link share.", + } +} diff --git a/pkg/user/user.go b/pkg/user/user.go index e18558532..2c96dc42b 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -186,7 +186,11 @@ func (u *User) GetFailedPasswordAttemptsKey() string { func GetFromAuth(a web.Auth) (*User, error) { u, is := a.(*User) if !is { - return &User{}, fmt.Errorf("user is not user element, is %s", reflect.TypeOf(a)) + typ := reflect.TypeOf(a) + if typ.String() == "*models.LinkSharing" { + return nil, &ErrMustNotBeLinkShare{} + } + return &User{}, fmt.Errorf("user is not user element, is %s", typ) } return u, nil }