API for chat project sharing (experiment `chat-projects`). Builds on #30479 (merged). Owners share a project with users, groups, or the whole organization (the Everyone group, ID = organization ID). Chats in a shared project stay private. | Role | Project and instructions | Memories | ACL | Delete | |---------|--------------------------|------------|------|--------| | `use` | read | read/write | read | no | | `admin` | read/write | read/write | edit | no | Only the owner, and administrators whose role grants delete, can delete a project. - **API:** `GET`/`PATCH .../chats/projects/{project}/acl`, same shapes as `/chats/{chat}/acl`, plus SDK methods. Project responses include the caller's `permissions` (`update`, `delete`, `share`); the UI hides edit and delete accordingly. - **Listing:** `GET /chats/projects` returns owned and shared projects. Removes `GetChatProjectsByOwnerID`. - **Chat access:** creating a chat in a project, and loading its memory and instructions on each turn, requires the chat owner to own the project or have an ACL grant (`chatd.ChatProjectUsableBy`). Role grants don't count. If the grant is removed, existing chats lose memory and instructions on their next turn. - **Validation:** users and groups must be in the project's organization; keys must be canonical UUIDs; callers can't change their own role; the owner can't be added. Migration `000624` drops a user's grants when they leave the organization, so rejoining doesn't restore access. - **Instructions:** readers who can't edit get 403 instead of 404, and the UI hides editing. - **Agent prompt:** memories are visible to and editable by sharees, so the agent is told not to save secrets or personal details. Known gap: project deletion still detaches chats, and a sharee's open sidebar keeps showing them until reload. #30683 replaces this. Not included: share notifications, `/acl/available`, sharing UI. <details> <summary>Decision log</summary> - `use` grants memory read/write over HTTP, because agents in a sharee's chats can already write memories. Making the agent read-only for sharees was the alternative (@f0ssel chose sharees changing memories through use). - An administrator with `share` can grant themselves access and then use the project. Accepted to keep access control simple (@f0ssel). </details> > Generated by Coder Agents.
61 lines
2.3 KiB
Go
61 lines
2.3 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
)
|
|
|
|
// usableChatProject loads the project and reports whether the chat owner can
|
|
// use it. It reports false when the project is missing, the lookup or access
|
|
// check fails, or the owner cannot use it, and logs every case except a
|
|
// missing project.
|
|
func (p *Server) usableChatProject(ctx context.Context, logger slog.Logger, projectID uuid.UUID, chat database.Chat) (database.ChatProject, bool) {
|
|
logger = logger.With(slog.F("project_id", projectID))
|
|
project, err := p.db.GetChatProjectByID(ctx, projectID)
|
|
if err != nil {
|
|
// A project deleted mid-turn is expected.
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
logger.Warn(ctx, "failed to load chat project, turn runs without project memory or instructions", slog.Error(err))
|
|
}
|
|
return database.ChatProject{}, false
|
|
}
|
|
usable, err := ChatProjectUsableBy(ctx, p.db, project, chat.OwnerID)
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to check chat project access, turn runs without project memory or instructions", slog.Error(err))
|
|
return database.ChatProject{}, false
|
|
}
|
|
if !usable {
|
|
logger.Debug(ctx, "chat owner cannot use chat project, turn runs without project memory or instructions",
|
|
slog.F("project_owner_id", project.OwnerID))
|
|
return database.ChatProject{}, false
|
|
}
|
|
return project, true
|
|
}
|
|
|
|
// ChatProjectUsableBy reports whether userID owns project or has an ACL
|
|
// grant on it. Role grants do not count, because the chat would read memory
|
|
// and instructions the project owner did not share. Memory tools and
|
|
// instruction loading run as chatd, so they must use this check, not dbauthz.
|
|
func ChatProjectUsableBy(ctx context.Context, db database.Store, project database.ChatProject, userID uuid.UUID) (bool, error) {
|
|
if project.OwnerID == userID {
|
|
return true, nil
|
|
}
|
|
//nolint:gocritic // The check answers for the chat owner, not the caller.
|
|
usable, err := db.IsChatProjectUsableByUserID(dbauthz.AsChatd(ctx), database.IsChatProjectUsableByUserIDParams{
|
|
ProjectID: project.ID,
|
|
UserID: userID,
|
|
})
|
|
if err != nil {
|
|
return false, xerrors.Errorf("check chat project access: %w", err)
|
|
}
|
|
return usable, nil
|
|
}
|