A person leaves licences out, and no key of theirs reaches a model under one
CI / test (push) Successful in 11m28s
CI / test (push) Successful in 11m28s
A game sold in the EU cannot ship what a licence excluding the EU made. On the profile page a person leaves out licences one by one, or by rule: every licence forbidding commercial use, every licence granting nothing in a region. The rules cover models added later. The choice reaches every principal through the resolved limits, so permits() leaves such models out of /v1/models, requests, jobs, fallbacks and the portal's pickers, and a request for one is refused 403 licence_excluded saying which choice did. Kept in user_excluded_licences (migration 0046), copied with the store and gone with the person. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a313b3e0da
commit
4bbac0833d
20 files changed
+658
-7
No files matched your search
@@ -25,6 +25,8 @@ var copyOrder = []string{
|
||||
"users",
|
||||
// After users and the three levels of the tree, which it points at.
|
||||
"memberships",
|
||||
// After users, which it points at.
|
||||
"user_excluded_licences",
|
||||
"api_keys",
|
||||
"key_claims",
|
||||
"sessions",
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UserExcludedLicences is what a person has said not to use, sorted: a
|
||||
// licence by its id, or a rule over licences ("rule:non-commercial",
|
||||
// "rule:region:EU"). What each means is the router's to match; the store
|
||||
// keeps the words.
|
||||
func (s *DB) UserExcludedLicences(ctx context.Context, userID string) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT licence FROM user_excluded_licences WHERE user_id = ? ORDER BY licence`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var l string
|
||||
if err := rows.Scan(&l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SetUserExcludedLicences replaces what a person has said not to use; an
|
||||
// empty list excludes nothing.
|
||||
func (s *DB) SetUserExcludedLicences(ctx context.Context, userID string, licences []string) error {
|
||||
keys := make([]string, 0, len(licences))
|
||||
for _, l := range licences {
|
||||
if l = strings.TrimSpace(l); l != "" {
|
||||
keys = append(keys, l)
|
||||
}
|
||||
}
|
||||
slices.Sort(keys)
|
||||
keys = slices.Compact(keys)
|
||||
return s.inTx(ctx, func(c conn) error {
|
||||
if _, err := c.ExecContext(ctx, `DELETE FROM user_excluded_licences WHERE user_id = ?`, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, k := range keys {
|
||||
if _, err := c.ExecContext(ctx,
|
||||
`INSERT INTO user_excluded_licences (user_id, licence) VALUES (?, ?)`, userID, k); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAPersonsExcludedLicencesReachEveryWayTheyAsk(t *testing.T) {
|
||||
db := openDB(t)
|
||||
ctx := t.Context()
|
||||
if err := db.CreateUser(ctx, User{ID: "usr-1", Email: "a@b.c", State: UserActive, CreatedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.SetUserExcludedLicences(ctx, "usr-1", []string{"tencent-hunyuan-community", " CC-BY-NC-4.0", "", "CC-BY-NC-4.0", "rule:region:EU"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := db.UserExcludedLicences(ctx, "usr-1")
|
||||
want := []string{"CC-BY-NC-4.0", "rule:region:EU", "tencent-hunyuan-community"}
|
||||
if err != nil || !slices.Equal(got, want) {
|
||||
t.Errorf("excluded %v %v, want %v", got, err, want)
|
||||
}
|
||||
|
||||
// A session, which has no key row, gets them as a key does.
|
||||
eff, err := db.ResolveFor(ctx, "session:usr-1", "usr-1", "")
|
||||
if err != nil || !slices.Equal(eff.ExcludedLicences, want) {
|
||||
t.Errorf("session: %v %v", eff.ExcludedLicences, err)
|
||||
}
|
||||
|
||||
if err := db.SetUserExcludedLicences(ctx, "usr-1", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, _ := db.UserExcludedLicences(ctx, "usr-1"); len(got) != 0 {
|
||||
t.Errorf("cleared, still %v", got)
|
||||
}
|
||||
}
|
||||
@@ -224,6 +224,13 @@ func (s *DB) ResolveFor(ctx context.Context, callerID, userID, projectID string)
|
||||
// person's limit by every key they own; a per-member limit by one person
|
||||
// within one scope.
|
||||
func (s *DB) resolveChain(ctx context.Context, e *Effective, keyLevel bool) error {
|
||||
if e.UserID != "" {
|
||||
ex, err := s.UserExcludedLicences(ctx, e.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.ExcludedLicences = ex
|
||||
}
|
||||
if e.ProjectID != "" {
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT t.id, t.org_id FROM projects p JOIN teams t ON t.id = p.team_id WHERE p.id = ?`,
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
-- Ported from the SQLite migration of the same number.
|
||||
-- The licences a person has said not to use. Models under them are not
|
||||
-- listed to the person, nor served to their keys or their sessions: a
|
||||
-- game sold in the EU cannot ship what a licence excluding the EU made.
|
||||
CREATE TABLE user_excluded_licences (
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
licence TEXT NOT NULL,
|
||||
PRIMARY KEY (user_id, licence)
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- The licences a person has said not to use. Models under them are not
|
||||
-- listed to the person, nor served to their keys or their sessions: a
|
||||
-- game sold in the EU cannot ship what a licence excluding the EU made.
|
||||
CREATE TABLE user_excluded_licences (
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
licence TEXT NOT NULL,
|
||||
PRIMARY KEY (user_id, licence)
|
||||
);
|
||||
@@ -165,6 +165,9 @@ type Effective struct {
|
||||
BudgetPeriod Period
|
||||
// AllowedModels is the intersection along the chain. nil means every model.
|
||||
AllowedModels []string
|
||||
// ExcludedLicences are the licences the caller's person has said not to
|
||||
// use, as LicenceKey gives them; none for a key without a person.
|
||||
ExcludedLicences []string
|
||||
|
||||
// RPMOwner, TPMOwner and BudgetOwner say which scope set the value that
|
||||
// won, so consumption can be counted against that scope rather than
|
||||
|
||||
Reference in new issue
Block a user