原来 uniqueIndex:uk_shop_username 只在 Username 字段上,导致用户名全局唯一。 S002/S003 的 admin 用户与 S001 冲突,INSERT 被静默跳过,无法登录。 将 User 的 TenantBase 改为直接定义 ShopID,使其参与复合唯一索引。 需要 Reset DB 使新索引生效。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -132,21 +132,21 @@ func main() {
|
|||||||
hash := mustHash("password123")
|
hash := mustHash("password123")
|
||||||
admin := upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "admin", func() any {
|
admin := upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "admin", func() any {
|
||||||
return &model.User{
|
return &model.User{
|
||||||
TenantBase: model.TenantBase{ShopID: shop.ID},
|
ShopID: shop.ID,
|
||||||
Username: "admin", PasswordHash: hash, RealName: "张三(管理员)",
|
Username: "admin", PasswordHash: hash, RealName: "张三(管理员)",
|
||||||
Phone: "13800000001", Role: "admin", IsActive: true,
|
Phone: "13800000001", Role: "admin", IsActive: true,
|
||||||
}
|
}
|
||||||
}).(model.User)
|
}).(model.User)
|
||||||
operator := upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "operator", func() any {
|
operator := upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "operator", func() any {
|
||||||
return &model.User{
|
return &model.User{
|
||||||
TenantBase: model.TenantBase{ShopID: shop.ID},
|
ShopID: shop.ID,
|
||||||
Username: "operator", PasswordHash: hash, RealName: "李四(操作员)",
|
Username: "operator", PasswordHash: hash, RealName: "李四(操作员)",
|
||||||
Phone: "13800000002", Role: "operator", IsActive: true,
|
Phone: "13800000002", Role: "operator", IsActive: true,
|
||||||
}
|
}
|
||||||
}).(model.User)
|
}).(model.User)
|
||||||
upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "test", func() any {
|
upsert(db, &model.User{}, "shop_id = ? AND username = ?", shop.ID, "test", func() any {
|
||||||
return &model.User{
|
return &model.User{
|
||||||
TenantBase: model.TenantBase{ShopID: shop.ID},
|
ShopID: shop.ID,
|
||||||
Username: "test", PasswordHash: hash, RealName: "王五(只读)",
|
Username: "test", PasswordHash: hash, RealName: "王五(只读)",
|
||||||
Phone: "13800000003", Role: "readonly", IsActive: true,
|
Phone: "13800000003", Role: "readonly", IsActive: true,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (h *UserHandler) Create(c *gin.Context) {
|
|||||||
role = "operator"
|
role = "operator"
|
||||||
}
|
}
|
||||||
u := model.User{
|
u := model.User{
|
||||||
TenantBase: model.TenantBase{ShopID: shopID},
|
ShopID: shopID,
|
||||||
Username: req.Username,
|
Username: req.Username,
|
||||||
PasswordHash: string(hash),
|
PasswordHash: string(hash),
|
||||||
RealName: req.RealName,
|
RealName: req.RealName,
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
TenantBase
|
Base
|
||||||
|
ShopID uint64 `gorm:"not null;index;uniqueIndex:uk_shop_username" json:"shop_id"`
|
||||||
Username string `gorm:"size:50;uniqueIndex:uk_shop_username" json:"username"`
|
Username string `gorm:"size:50;uniqueIndex:uk_shop_username" json:"username"`
|
||||||
PasswordHash string `gorm:"size:255" json:"-"`
|
PasswordHash string `gorm:"size:255" json:"-"`
|
||||||
RealName string `gorm:"size:50" json:"real_name"`
|
RealName string `gorm:"size:50" json:"real_name"`
|
||||||
|
|||||||
@@ -349,10 +349,7 @@ func hashPassword(plain string) string {
|
|||||||
func CreateTestUser(db *gorm.DB, shopID uint64, username, password, role string) *model.User {
|
func CreateTestUser(db *gorm.DB, shopID uint64, username, password, role string) *model.User {
|
||||||
hash := hashPassword(password)
|
hash := hashPassword(password)
|
||||||
user := &model.User{
|
user := &model.User{
|
||||||
TenantBase: model.TenantBase{
|
ShopID: shopID,
|
||||||
Base: model.Base{},
|
|
||||||
ShopID: shopID,
|
|
||||||
},
|
|
||||||
Username: username,
|
Username: username,
|
||||||
PasswordHash: hash,
|
PasswordHash: hash,
|
||||||
RealName: "Test User " + username,
|
RealName: "Test User " + username,
|
||||||
|
|||||||
Reference in New Issue
Block a user