fix(auth): return correct error when trying to do a user action as a link share

Resolves https://vikunja.sentry.io/share/issue/77f7aa4cda9442439deba3cfe2cb32c6/
This commit is contained in:
kolaente 2025-01-14 15:01:13 +01:00
parent 845fe8ea88
commit 33b9917c7d
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
2 changed files with 32 additions and 1 deletions

View File

@ -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.",
}
}

View File

@ -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
}