WeKnora/internal/application/repository
wizardchen f3d1210577 refactor(rbac): address PR 1 review feedback
Tighten correctness, security and observability of the tenant RBAC
foundation introduced in 3c4cdd70 in response to internal review.

Bugfixes
- custom_agents.runnable_by_viewer: drop the GORM `default:true` tag.
  GORM treats a plain bool's zero value as "unspecified" and silently
  rewrites it to the column default on insert, which would prevent
  admins from ever creating an agent with viewer-runtime disabled. The
  column-level DEFAULT TRUE on the database side still backstops rows
  inserted by raw SQL / migrations.
- TenantMemberRepository.HasAnyMembers: replace `Limit(1).Count(&n)`
  with `Take(&probe)`. GORM's Count ignores Limit and scans the whole
  table, which matters because this runs on the auth middleware's
  hot path for users without a cached membership.
- service/user.LoginWithOIDC: include User, Tenant and Memberships in
  the OIDCCallbackResponse so the OIDC flow returns the same shape as
  password login (previously User and friends were left nil).

Security
- middleware/auth.resolveTenantRole: restrict orphan-tenant
  auto-promotion to the caller's home tenant. Cross-tenant superuser
  visits and JWTs minted for foreign tenants no longer trigger silent
  Owner grants. Two regression tests pin the new behaviour.
- service/user.synthFallbackMembership: default the synthesised role
  to Viewer instead of Owner. The login response only feeds UI; the
  backend re-derives the real role from tenant_members on every
  request, so showing a Viewer UI is preferable to a misleading Owner
  UI that would surface admin controls the backend will then 403.
- migrations/versioned/000043_tenant_rbac.down.sql: gate the destructive
  drop behind a `weknora.allow_destructive_migration` GUC so an
  accidental `migrate down` cannot wipe role data in production.
- frontend/src/stores/auth.ts: comment currentTenantRole as
  rendering-only so future PRs cannot mistake the localStorage-backed
  value for an authorisation signal.

API contract
- Memberships in LoginResponse and OIDCCallbackResponse now serialises
  without omitempty, and buildMembershipsForUser / synthFallbackMembership
  always return a non-nil slice — frontend can rely on JSON `[]`.
- handler/auth.GetCurrentUser includes memberships so /auth/me refreshes
  recover currentTenantRole after a page reload.

Performance
- Add TenantService.GetTenantsByIDs (with the matching repo method) and
  use it inside buildMembershipsForUser to batch tenant-name lookups
  instead of issuing one query per membership.

Observability
- Replace the remaining `log.Printf` calls in middleware/auth with
  contextual logger.Warnf / logger.Infof so role resolution events ride
  the standard request-id / Langfuse trace plumbing. The successful
  auto-promote line is tagged `[audit]` for log mining.

Tests
- internal/application/service/tenant_member_test.go: hand-rolled
  fakeTenantMemberRepo plus 11 cases covering AddMember validation,
  EnsureOwner idempotence, "cannot demote/remove the last Owner",
  same-role no-op, ErrInvalidTenantRole / ErrMembershipNotFound, and
  the TenantRole.HasPermission lattice.
- internal/middleware/auth_role_test.go: fakeMemberService plus 8 cases
  pinning every branch of resolveTenantRole, including the two H1
  regressions (cross-tenant superuser must not auto-promote; orphan
  auto-promote requires the home tenant).
2026-05-18 17:28:58 +08:00
..
memory/neo4j fix: revert to neo4j v6 and workaround Go 1.24 Windows compiler bug with -p=1 2026-04-11 22:08:02 +08:00
retriever chore(deps): update dependencies in /docreader and adjust dependabot configuration 2026-05-12 14:58:52 +08:00
agent_share.go feat(api): integrate per-organization resource counts into organization listing 2026-02-09 11:40:25 +08:00
chunk.go fix(faq): correct PostgreSQL cast precedence in FAQ search 2026-05-12 16:42:21 +08:00
chunk_sqlite_test.go fix: SQLite/Lite mode compatibility for FAQ import and chunk operations 2026-04-15 20:37:10 +08:00
custom_agent.go refactor: 调整智能体配置,增强智能体选择与状态管理 2025-12-26 21:31:02 +08:00
datasource_repo.go refactor(datasource): remove WeCom document integration and related components 2026-03-30 20:46:07 +08:00
kbshare.go feat(api): integrate per-organization resource counts into organization listing 2026-02-09 11:40:25 +08:00
knowledge.go feat(knowledge-base): document list filters and explicit batch-management UX 2026-05-12 18:29:47 +08:00
knowledgebase.go feat(knowledge-base): validate vector store bindings on create, copy, and delete 2026-05-18 15:58:46 +08:00
knowledgebase_sqlite_test.go feat(rbac): add tenant_members schema and auth wiring (PR 1 of 3, #1303) 2026-05-18 17:28:58 +08:00
mcp_service.go feat: add built-in MCP service support 2026-03-05 21:57:01 +08:00
mcp_tool_approval_repository.go fix(agent): 加固 MCP 工具人审批的跨实例与并发语义 2026-05-10 22:57:12 +08:00
message.go feat(datasource): implement data source management functionality 2026-03-30 20:46:07 +08:00
model.go refactor: Update function signatures across multiple files to improve readability by adding context parameters and enhancing code structure 2025-11-28 16:17:52 +08:00
organization.go feat(api): integrate per-organization resource counts into organization listing 2026-02-09 11:40:25 +08:00
session.go fix(session): scope session access by user 2026-05-14 15:56:43 +08:00
session_test.go fix(session): scope session access by user 2026-05-14 15:56:43 +08:00
tag.go feat(datasource): implement data source management functionality 2026-03-30 20:46:07 +08:00
task_queue.go perf(wiki-ingest): scale to 4w-doc KBs + generic task queue / dead-letter 2026-05-10 00:01:06 +08:00
task_queue_test.go perf(wiki-ingest): scale to 4w-doc KBs + generic task queue / dead-letter 2026-05-10 00:01:06 +08:00
tenant.go refactor(rbac): address PR 1 review feedback 2026-05-18 17:28:58 +08:00
tenant_disabled_shared_agent.go feat(agent): enhance agent and skill management features 2026-02-09 11:40:25 +08:00
tenant_member.go refactor(rbac): address PR 1 review feedback 2026-05-18 17:28:58 +08:00
tenant_test.go fix(tenant): prevent api_key encryption loss on tenant settings update 2026-03-25 21:25:21 +08:00
user.go feat: add GetUserByTenantID functionality to user repository and service 2026-03-05 18:14:14 +08:00
vectorstore.go feat: add VectorStore service layer with types and connection testing 2026-04-13 22:57:17 +08:00
web_search_provider.go feat(web-search): refactor web search provider 2026-03-31 20:22:34 +08:00
wiki_log_entry.go perf(wiki): move ingest log to event table and index to on-demand API 2026-05-08 21:48:52 +08:00
wiki_log_entry_test.go perf(wiki): move ingest log to event table and index to on-demand API 2026-05-08 21:48:52 +08:00
wiki_page.go fix(wiki-ingest,asynqdl): review fixups for PR #1241 2026-05-10 00:01:06 +08:00
wiki_page_test.go perf(wiki): move ingest log to event table and index to on-demand API 2026-05-08 21:48:52 +08:00