feat(api/v2): add user search endpoints
Port to /api/v2:
- GET /users (global user search by username/name/email; emails are blanked)
- GET /projects/{project}/users/search (users with access to a project, for
share autocomplete; requires project read access)
Both are custom routes: the project search loads the project and enforces
CanRead explicitly.
This commit is contained in:
parent
3312716afd
commit
5dcc501d54
|
|
@ -0,0 +1,130 @@
|
||||||
|
// Vikunja is a to-do list application to facilitate your life.
|
||||||
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package apiv2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"code.vikunja.io/api/pkg/db"
|
||||||
|
"code.vikunja.io/api/pkg/models"
|
||||||
|
"code.vikunja.io/api/pkg/user"
|
||||||
|
|
||||||
|
"github.com/danielgtaylor/huma/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type userListBody struct {
|
||||||
|
Body Paginated[*user.User]
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterUserSearchRoutes wires the two user-search endpoints onto the Huma API:
|
||||||
|
// a global search and a per-project search used for share autocomplete.
|
||||||
|
func RegisterUserSearchRoutes(api huma.API) {
|
||||||
|
Register(api, huma.Operation{
|
||||||
|
OperationID: "users-search",
|
||||||
|
Summary: "Search users",
|
||||||
|
Description: "Searches users by username, name or full email. Matching by name or email requires the target user to have made themselves discoverable, unless both users share an external (OIDC/LDAP) team. Email addresses are never returned.",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/users",
|
||||||
|
Tags: []string{"user"},
|
||||||
|
}, usersSearch)
|
||||||
|
|
||||||
|
Register(api, huma.Operation{
|
||||||
|
OperationID: "projects-users-search",
|
||||||
|
Summary: "Search users with access to a project",
|
||||||
|
Description: "Returns the users who can access the project — through ownership, a direct share or a team — optionally filtered by a search string. Intended for share autocomplete. Requires read access to the project.",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/projects/{project}/users/search",
|
||||||
|
Tags: []string{"sharing"},
|
||||||
|
}, projectUsersSearch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { AddRouteRegistrar(RegisterUserSearchRoutes) }
|
||||||
|
|
||||||
|
func usersSearch(ctx context.Context, in *struct {
|
||||||
|
Q string `query:"q" doc:"Search query matched against username, name or full email."`
|
||||||
|
}) (*userListBody, error) {
|
||||||
|
a, err := authFromCtx(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s := db.NewSession()
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
currentUser, err := models.GetUserOrLinkShareUser(s, a)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := user.ListUsers(s, in.Q, currentUser, nil)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
if err := s.Commit(); err != nil {
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range users {
|
||||||
|
users[i].Email = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return &userListBody{Body: NewPaginated(users, int64(len(users)), 1, len(users))}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func projectUsersSearch(ctx context.Context, in *struct {
|
||||||
|
ProjectID int64 `path:"project"`
|
||||||
|
Q string `query:"q" doc:"Search query matched against username and name."`
|
||||||
|
}) (*userListBody, error) {
|
||||||
|
a, err := authFromCtx(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s := db.NewSession()
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
project := &models.Project{ID: in.ProjectID}
|
||||||
|
canRead, _, err := project.CanRead(s, a)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
if !canRead {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, huma.Error403Forbidden("forbidden")
|
||||||
|
}
|
||||||
|
|
||||||
|
currentUser, err := models.GetUserOrLinkShareUser(s, a)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := models.ListUsersFromProject(s, project, currentUser, in.Q)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
if err := s.Commit(); err != nil {
|
||||||
|
return nil, translateDomainError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &userListBody{Body: NewPaginated(users, int64(len(users)), 1, len(users))}, nil
|
||||||
|
}
|
||||||
|
|
@ -66,6 +66,53 @@ func TestHumaWebhookEvents(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHumaUserSearch covers the global user search. Emails must never leak.
|
||||||
|
func TestHumaUserSearch(t *testing.T) {
|
||||||
|
e, err := setupTestEnv()
|
||||||
|
require.NoError(t, err)
|
||||||
|
token := humaTokenFor(t, &testuser1)
|
||||||
|
|
||||||
|
t.Run("Search by username", func(t *testing.T) {
|
||||||
|
rec := humaRequest(t, e, http.MethodGet, "/api/v2/users?q=user2", "", token, "")
|
||||||
|
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
|
||||||
|
|
||||||
|
usernames, emails := usersFromSearch(t, rec.Body.Bytes())
|
||||||
|
assert.Contains(t, usernames, "user2")
|
||||||
|
for _, em := range emails {
|
||||||
|
assert.Empty(t, em, "user search must never return email addresses")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("Unauthenticated", func(t *testing.T) {
|
||||||
|
rec := humaRequest(t, e, http.MethodGet, "/api/v2/users?q=user2", "", "", "")
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, rec.Code, "body: %s", rec.Body.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHumaProjectUserSearch covers the per-project user search used for share
|
||||||
|
// autocomplete. It requires read access to the project.
|
||||||
|
func TestHumaProjectUserSearch(t *testing.T) {
|
||||||
|
e, err := setupTestEnv()
|
||||||
|
require.NoError(t, err)
|
||||||
|
token := humaTokenFor(t, &testuser1)
|
||||||
|
|
||||||
|
t.Run("Owned project", func(t *testing.T) {
|
||||||
|
// testuser1 owns project 1.
|
||||||
|
rec := humaRequest(t, e, http.MethodGet, "/api/v2/projects/1/users/search", "", token, "")
|
||||||
|
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
|
||||||
|
assert.Contains(t, rec.Body.String(), `"items"`)
|
||||||
|
})
|
||||||
|
t.Run("Forbidden - no access", func(t *testing.T) {
|
||||||
|
// project 2 is owned by user3; testuser1 has no access.
|
||||||
|
rec := humaRequest(t, e, http.MethodGet, "/api/v2/projects/2/users/search", "", token, "")
|
||||||
|
assert.Equal(t, http.StatusForbidden, rec.Code, "body: %s", rec.Body.String())
|
||||||
|
})
|
||||||
|
t.Run("Nonexistent project", func(t *testing.T) {
|
||||||
|
// CanRead surfaces ErrProjectDoesNotExist (404), not a bare forbidden.
|
||||||
|
rec := humaRequest(t, e, http.MethodGet, "/api/v2/projects/99999/users/search", "", token, "")
|
||||||
|
assert.Equal(t, http.StatusNotFound, rec.Code, "body: %s", rec.Body.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// TestHumaProjectBackgroundDelete covers removing a project background. It
|
// TestHumaProjectBackgroundDelete covers removing a project background. It
|
||||||
// mirrors the v1 background_test.go matrix: the owner clears the background
|
// mirrors the v1 background_test.go matrix: the owner clears the background
|
||||||
// (and keeps the title), a read-only user is refused.
|
// (and keeps the title), a read-only user is refused.
|
||||||
|
|
@ -147,3 +194,19 @@ func TestHumaUnsplashBackground(t *testing.T) {
|
||||||
assert.Equal(t, http.StatusForbidden, rec.Code, "body: %s", rec.Body.String())
|
assert.Equal(t, http.StatusForbidden, rec.Code, "body: %s", rec.Body.String())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func usersFromSearch(t *testing.T, body []byte) (usernames, emails []string) {
|
||||||
|
t.Helper()
|
||||||
|
var resp struct {
|
||||||
|
Items []struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
} `json:"items"`
|
||||||
|
}
|
||||||
|
require.NoError(t, json.Unmarshal(body, &resp), "search body must be a paginated envelope: %s", string(body))
|
||||||
|
for _, it := range resp.Items {
|
||||||
|
usernames = append(usernames, it.Username)
|
||||||
|
emails = append(emails, it.Email)
|
||||||
|
}
|
||||||
|
return usernames, emails
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue