feat: expand companion platform, device controls, and memoir workflows
ci / verify (push) Canceled after 0s
ci / verify (push) Canceled after 0s
Add customer and operator login, BLE provisioning, device fleet controls, call memory, memoir generation, and refreshed family and maintenance portals. Include API contracts, migrations, firmware sources, tests, assets, and deployment documentation.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var ErrInvalidDisplayName = errors.New("invalid display name")
|
||||
|
||||
func ValidateDisplayName(name string) (string, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if !utf8.ValidString(name) || utf8.RuneCountInString(name) < 1 || utf8.RuneCountInString(name) > 80 {
|
||||
return "", ErrInvalidDisplayName
|
||||
}
|
||||
for _, r := range name {
|
||||
if unicode.IsControl(r) {
|
||||
return "", ErrInvalidDisplayName
|
||||
}
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// The principal comes from authenticated middleware. Callers cannot select another account.
|
||||
func (r *Repository) UpdateDisplayName(ctx context.Context, principal Principal, name string) error {
|
||||
name, err := ValidateDisplayName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if principal.Issuer == "" || principal.Subject == "" {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
result, err := r.pool.Exec(ctx, `UPDATE user_account ua SET display_name=$3, updated_at=now()
|
||||
FROM tenant t WHERE ua.tenant_id=t.id AND t.status='active' AND ua.status='active'
|
||||
AND ua.oidc_issuer=$1 AND ua.oidc_subject=$2`, principal.Issuer, principal.Subject, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.RowsAffected() == 0 {
|
||||
return ErrAccountNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateDisplayName(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "名字\n另一行", strings.Repeat("名", 81), string([]byte{0xff})} {
|
||||
if _, err := ValidateDisplayName(name); !errors.Is(err, ErrInvalidDisplayName) {
|
||||
t.Errorf("accepted invalid nickname %q", name)
|
||||
}
|
||||
}
|
||||
if name, err := ValidateDisplayName(" 家庭成员 "); err != nil || name != "家庭成员" {
|
||||
t.Fatalf("normalization %q %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisplayNameOnlyChangesAuthenticatedIdentity(t *testing.T) {
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
repo := NewRepository(pool)
|
||||
owner := Principal{Issuer: localIssuer, Subject: ownerSubject}
|
||||
outsider := Principal{Issuer: localIssuer, Subject: outsiderSubject}
|
||||
before, err := repo.CurrentUser(ctx, owner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other, err := repo.CurrentUser(ctx, outsider)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer repo.UpdateDisplayName(ctx, owner, before.Accounts[0].DisplayName)
|
||||
if err := repo.UpdateDisplayName(ctx, owner, "测试家人"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, err := repo.CurrentUser(ctx, owner)
|
||||
if err != nil || after.Accounts[0].DisplayName != "测试家人" {
|
||||
t.Fatalf("nickname not saved: %v", err)
|
||||
}
|
||||
unchanged, err := repo.CurrentUser(ctx, outsider)
|
||||
if err != nil || unchanged.Accounts[0].DisplayName != other.Accounts[0].DisplayName {
|
||||
t.Fatal("another identity was modified")
|
||||
}
|
||||
if err := repo.UpdateDisplayName(ctx, Principal{}, "其他"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatalf("empty principal accepted: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user