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


##########
admin/dao/impl/guest.go:
##########
@@ -57,33 +60,105 @@ func (d *GuestDao) Login(username, password string) (bool, 
int) {
        return true, id
 }
 
-func (d *GuestDao) Register(username, password string) error {
+func (d *GuestDao) EnsureUsernameUniqueIndex() error {
+       indexed, err := hasUniqueUsernameIndex(d.db)
+       if err != nil {
+               return err
+       }
+       if indexed {
+               return nil
+       }
 
-       if username == "" {
-               return errors.New("void username")
+       if username, err := findDuplicateUsername(d.db); err == nil {
+               return fmt.Errorf("cannot add unique username index: duplicate 
username %q exists; resolve duplicate accounts and restart", username)
+       } else if !errors.Is(err, SQL.ErrNoRows) {
+               return err
        }
 
-       db := d.db
-       var id int
-       err := db.QueryRow("SELECT id FROM pixiu_user WHERE username = ?", 
username).Scan(&id)
+       _, err = d.db.Exec("ALTER TABLE pixiu_user ADD UNIQUE INDEX 
uniq_pixiu_user_username (username)")
        if err == nil {
-               return errors.New("User already exists, please login")
+               return nil
        }
-       //now := time.Now().Format("2006-01-02 15:04:05")
-       stmt, err := db.Prepare("INSERT INTO pixiu_user (username,password) 
VALUES (?,?)")
+
+       // Another Pixiu instance may have added the index while this instance 
was starting.
+       if indexed, checkErr := hasUniqueUsernameIndex(d.db); checkErr == nil 
&& indexed {
+               return nil
+       }
+       if username, duplicateErr := findDuplicateUsername(d.db); duplicateErr 
== nil {
+               return fmt.Errorf("cannot add unique username index: duplicate 
username %q exists; resolve duplicate accounts and restart", username)
+       }
+       return fmt.Errorf("failed to add unique username index: %w", err)
+}
+
+func hasUniqueUsernameIndex(db *SQL.DB) (bool, error) {
+       var indexed bool
+       err := db.QueryRow(`
+               SELECT EXISTS (
+                       SELECT index_name
+                       FROM information_schema.statistics
+                       WHERE table_schema = DATABASE()
+                         AND table_name = 'pixiu_user'
+                         AND non_unique = 0
+                       GROUP BY index_name
+                       HAVING COUNT(*) = 1 AND MAX(column_name = 'username') = 
1
+               )
+       `).Scan(&indexed)
+       return indexed, err
+}
+
+func findDuplicateUsername(db *SQL.DB) (string, error) {
+       var username string
+       err := db.QueryRow(`
+               SELECT username
+               FROM pixiu_user
+               GROUP BY username
+               HAVING COUNT(*) > 1
+               LIMIT 1
+       `).Scan(&username)
+       return username, err
+}
+
+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
+}

Review Comment:
   Fixed in `09994b9` (`fix(admin): initialize and resolve admin roles`). 
`EnsureDefaultAdmin` now idempotently initializes the admin role and user-role 
association, and `GetUserRole` resolves the requested user instead of using a 
hard-coded user ID. I added opt-in MySQL integration coverage for role 
initialization and username-scoped lookup. `go test ./admin/...` passes; the 
MySQL integration tests still require `PIXIU_TEST_MYSQL_DSN` and have not run 
locally.
   
   <details><summary>中文</summary>
   
   已在 `09994b9` 修复。`EnsureDefaultAdmin` 现在会幂等地初始化管理员角色及用户角色关联;`GetUserRole` 
按请求的用户名查询,不再使用硬编码用户 ID。新增了角色初始化和按用户名查询的 MySQL 集成测试。`go test ./admin/...` 
已通过;MySQL 集成测试需要配置 `PIXIU_TEST_MYSQL_DSN`,本地尚未实际运行。
   </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]

Reply via email to