diff --git a/coderd/chats.go b/coderd/chats.go index 501f0296a0..b30e62c2b5 100644 --- a/coderd/chats.go +++ b/coderd/chats.go @@ -2444,12 +2444,20 @@ func (api *API) deleteChatProvider(rw http.ResponseWriter, r *http.Request) { func (api *API) listChatModelConfigs(rw http.ResponseWriter, r *http.Request) { ctx := r.Context() - if !api.Authorize(r, policy.ActionRead, rbac.ResourceDeploymentConfig) { - httpapi.Forbidden(rw) - return - } - configs, err := api.Database.GetChatModelConfigs(ctx) + // Admin users can see all model configs (including disabled ones) + // for management purposes. Non-admin users see only enabled + // configs, which is sufficient for using the chat feature. + isAdmin := api.Authorize(r, policy.ActionRead, rbac.ResourceDeploymentConfig) + + var configs []database.ChatModelConfig + var err error + if isAdmin { + configs, err = api.Database.GetChatModelConfigs(ctx) + } else { + //nolint:gocritic // All authenticated users need to read enabled model configs to use the chat feature. + configs, err = api.Database.GetEnabledChatModelConfigs(dbauthz.AsSystemRestricted(ctx)) + } if err != nil { httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ Message: "Failed to list chat model configs.", diff --git a/coderd/chats_test.go b/coderd/chats_test.go index 34b34b5176..41129109bb 100644 --- a/coderd/chats_test.go +++ b/coderd/chats_test.go @@ -818,16 +818,29 @@ func TestListChatModelConfigs(t *testing.T) { require.True(t, found) }) - t.Run("ForbiddenForOrganizationMember", func(t *testing.T) { + t.Run("SuccessForOrganizationMember", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) adminClient := newChatClient(t) firstUser := coderdtest.CreateFirstUser(t, adminClient) + modelConfig := createChatModelConfig(t, adminClient) memberClient, _ := coderdtest.CreateAnotherUser(t, adminClient, firstUser.OrganizationID) - _, err := memberClient.ListChatModelConfigs(ctx) - requireSDKError(t, err, http.StatusForbidden) + // Non-admin users should see only enabled model configs. + configs, err := memberClient.ListChatModelConfigs(ctx) + require.NoError(t, err) + require.NotEmpty(t, configs) + + found := false + for _, config := range configs { + if config.ID == modelConfig.ID { + found = true + require.Equal(t, "openai", config.Provider) + require.Equal(t, "gpt-4o-mini", config.Model) + } + } + require.True(t, found) }) }