XnLemon commented on code in PR #1087:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1087#discussion_r4528873468


##########
admin/dao/impl/guest.go:
##########
@@ -57,33 +55,43 @@ func (d *GuestDao) Login(username, password string) (bool, 
int) {
        return true, id
 }
 
-func (d *GuestDao) Register(username, password string) error {
+func (d *GuestDao) EnsureDefaultAdmin(username, password string) error {
+       _, err := d.db.Exec(`
+               INSERT INTO pixiu_user (username, password, role)
+               SELECT ?, ?, 1 FROM DUAL
+               WHERE NOT EXISTS (SELECT 1 FROM pixiu_user WHERE username = ?)
+       `, username, password, username)
+       return err
+}
 
-       if username == "" {
-               return errors.New("void username")
+func (d *GuestDao) Register(username, password string) error {
+       tx, err := d.db.Begin()
+       if err != nil {
+               return err
        }
+       defer tx.Rollback()
 
-       db := d.db
-       var id int
-       err := db.QueryRow("SELECT id FROM pixiu_user WHERE username = ?", 
username).Scan(&id)
+       var userID int64
+       err = tx.QueryRow("SELECT id FROM pixiu_user WHERE username = ? LIMIT 
1", username).Scan(&userID)

Review Comment:
   Fixed in `9942f8b7`.
   
   This finding is valid and in scope: duplicate usernames violate the 
registration contract and make username-based account lookups ambiguous. 
Startup now adds a unique, single-column username index for existing schemas 
(fresh schemas include it too). It refuses to migrate if duplicate usernames 
already exist and preserves those rows for manual resolution. Registration 
translates MySQL duplicate-key error 1062 into `user already exists`.
   
   I added an opt-in MySQL integration regression test that concurrently 
registers the same username and asserts that exactly one user and role are 
persisted. A second test verifies that migration leaves pre-existing duplicates 
untouched. `go test ./admin/...` passed. The MySQL integration tests are gated 
by `PIXIU_TEST_MYSQL_DSN`; they were skipped locally because no MySQL service 
is available.
   
   <details><summary>中文</summary>
   
   已在提交 `9942f8b7` 修复。
   
   这个问题成立且属于本 PR 
范围:重名账号违反注册接口的预期,也会让按用户名进行的账号查询产生歧义。服务启动时现在会为已有表补充单列用户名唯一索引,新建表也包含该索引。若迁移前已存在重名账号,服务会停止启动并保留数据,等待人工处理。注册时
 MySQL 1062 重复键错误会转换为“用户已存在”。
   
   新增了可选的 MySQL 集成回归测试:并发注册同名账号并断言最终只写入一个用户和一个角色;另一个测试验证已有重名数据不会被迁移过程改动。`go 
test ./admin/...` 已通过。MySQL 集成测试需要设置 `PIXIU_TEST_MYSQL_DSN`,本机没有 MySQL 
服务,因此此次跳过。
   </details>
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

  • [PR] feat(adm... XnLemon (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... github-advanced-security[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... Alanxtl (via GitHub) via notifications
    • Re: [PR]... codecov-commenter (via GitHub) via notifications
    • Re: [PR]... AlexStocks (via GitHub) via notifications

Reply via email to