feat(devices): 设备默认名「平台简写·主机名」+ 支持重命名(1.0.13)
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 17s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Failing after 14s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 6s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m3s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 17s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Failing after 14s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 6s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m3s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s
① 默认名:DeviceIdentity 改「平台简写(Win/Mac/Linux/iOS/Andr)·主机名」,主机名截
到 8、总长≈12(替代裸主机名,更短)。
② 重命名:新端点 POST /v1/me/devices/{uuid}/rename(Service.RenameDevice 校验归属+
截 64);store.UpdateName;客户端 account_api.renameDevice + provider.rename +
「我的设备」⋯ 菜单加「重命名」+ 输入弹窗;l10n(zh/en)。
③ 版本 1.0.13。
测试:server RenameDevice(归属/空名/404)+ 客户端 rename widget;全量 go/flutter 绿。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -28,6 +28,7 @@ func (h *Handler) RegisterRoutes(r chi.Router) {
|
||||
r.Get("/devices", h.ListDevices)
|
||||
r.Delete("/devices/{id}", h.DeleteDevice)
|
||||
r.Post("/devices/{id}/logout", h.ForceLogout)
|
||||
r.Post("/devices/{id}/rename", h.RenameDevice)
|
||||
}
|
||||
|
||||
type listDevicesResponse struct {
|
||||
@@ -87,3 +88,25 @@ func (h *Handler) ForceLogout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// RenameDevice handles POST /v1/me/devices/{id}/rename — set a device's name.
|
||||
func (h *Handler) RenameDevice(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := auth.UserIDFromContext(r.Context())
|
||||
if !ok {
|
||||
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<14)).Decode(&req); err != nil {
|
||||
apierr.WriteJSON(w, http.StatusBadRequest, apierr.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
deviceUUID := chi.URLParam(r, "id")
|
||||
if apiErr := h.svc.RenameDevice(r.Context(), userID, deviceUUID, req.Name); apiErr != nil {
|
||||
apierr.WriteJSON(w, StatusForError(apiErr), apiErr)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -312,6 +312,36 @@ func (svc *Service) ForceLogout(ctx context.Context, userID int64, deviceUUID st
|
||||
return nil
|
||||
}
|
||||
|
||||
// RenameDevice sets a device's display name (ownership-checked). Empty name →
|
||||
// 400; name is trimmed + truncated to 64 runes.
|
||||
//
|
||||
// - device not found → 404
|
||||
// - device owned by another user → 403
|
||||
func (svc *Service) RenameDevice(ctx context.Context, userID int64, deviceUUID, rawName string) *apierr.Error {
|
||||
uuid := strings.TrimSpace(deviceUUID)
|
||||
name := strings.TrimSpace(rawName)
|
||||
if uuid == "" || name == "" {
|
||||
return apierr.ErrBadRequest
|
||||
}
|
||||
if r := []rune(name); len(r) > 64 {
|
||||
name = string(r[:64])
|
||||
}
|
||||
dev, err := svc.store.FindByUUID(ctx, uuid)
|
||||
if err != nil {
|
||||
return apierr.ErrInternal
|
||||
}
|
||||
if dev == nil {
|
||||
return apierr.ErrNotFound
|
||||
}
|
||||
if dev.UserID != userID {
|
||||
return apierr.ErrForbidden
|
||||
}
|
||||
if err := svc.store.UpdateName(ctx, dev.ID, name); err != nil {
|
||||
return apierr.ErrInternal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// revokeDeviceSessions marks all of a device's sessions revoked and drops their
|
||||
// refresh JTIs from the Redis whitelist. Best-effort.
|
||||
func (svc *Service) revokeDeviceSessions(ctx context.Context, userID, deviceID int64) {
|
||||
|
||||
@@ -179,6 +179,14 @@ func nullStr(s string) any {
|
||||
return s
|
||||
}
|
||||
|
||||
// UpdateName sets a device's display name (non-tx). Used by rename.
|
||||
func (s *Store) UpdateName(ctx context.Context, deviceID int64, name string) error {
|
||||
if _, err := s.db.ExecContext(ctx, `UPDATE devices SET name=? WHERE id=?`, name, deviceID); err != nil {
|
||||
return fmt.Errorf("store.UpdateName: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteDeviceTx hard-deletes a device row inside tx.
|
||||
func (s *Store) deleteDeviceTx(ctx context.Context, tx *sql.Tx, deviceID int64) error {
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM devices WHERE id=?`, deviceID); err != nil {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/wangjia/pangolin/server/internal/devices"
|
||||
)
|
||||
|
||||
func TestSQLite_RenameDevice(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := openSQLite(t)
|
||||
if _, err := db.Exec(`INSERT INTO users (id,uuid,email,pw_hash,dp_uuid,status) VALUES (1,'u1','u1@e','h','dp1','active'),(2,'u2','u2@e','h','dp2','active')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO devices (id,uuid,user_id,name,platform) VALUES (10,'devA',1,'Win·old','windows'),(30,'devC',2,'C','ios')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc := devices.NewService(devices.NewStore(db), nil)
|
||||
|
||||
if e := svc.RenameDevice(ctx, 1, "devA", "我的台式机"); e != nil {
|
||||
t.Fatalf("rename: %v", e)
|
||||
}
|
||||
var name string
|
||||
db.QueryRow(`SELECT name FROM devices WHERE id=10`).Scan(&name)
|
||||
if name != "我的台式机" {
|
||||
t.Fatalf("name = %q, want 我的台式机", name)
|
||||
}
|
||||
// 空名 → 400
|
||||
if e := svc.RenameDevice(ctx, 1, "devA", " "); e == nil {
|
||||
t.Fatal("empty name should fail")
|
||||
}
|
||||
// 他人设备 → 403
|
||||
if e := svc.RenameDevice(ctx, 1, "devC", "x"); e == nil {
|
||||
t.Fatal("renaming other user's device should fail")
|
||||
}
|
||||
// 不存在 → 404
|
||||
if e := svc.RenameDevice(ctx, 1, "nope", "x"); e == nil {
|
||||
t.Fatal("unknown device should fail")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user