feat(server): passwd 子命令支持 -new-username 同时改用户名
This commit is contained in:
1 parent
b80c64a115
commit
8dae63ee70
2 files changed
+32
No files matched your search
@@ -202,6 +202,28 @@ func (s *Service) SetPassword(ctx context.Context, username, password string, mu
|
||||
return s.RevokeUserSessions(ctx, id)
|
||||
}
|
||||
|
||||
// SetUsername 修改用户名(供本机管理入口免登录调用),并作废该用户全部会话。
|
||||
func (s *Service) SetUsername(ctx context.Context, oldUsername, newUsername string) error {
|
||||
if newUsername == "" {
|
||||
return fmt.Errorf("新用户名不能为空")
|
||||
}
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`UPDATE users SET username = ?, updated_at = datetime('now') WHERE username = ?`,
|
||||
newUsername, oldUsername)
|
||||
if err != nil {
|
||||
return fmt.Errorf("更新用户名: %w", err)
|
||||
}
|
||||
if n, err := res.RowsAffected(); err == nil && n == 0 {
|
||||
return fmt.Errorf("用户 %s 不存在", oldUsername)
|
||||
}
|
||||
|
||||
var id int64
|
||||
if err := s.db.QueryRowContext(ctx, `SELECT id FROM users WHERE username = ?`, newUsername).Scan(&id); err != nil {
|
||||
return fmt.Errorf("查询用户: %w", err)
|
||||
}
|
||||
return s.RevokeUserSessions(ctx, id)
|
||||
}
|
||||
|
||||
// VerifyPassword 校验指定用户的原密码,不创建会话(供「修改密码」等场景使用)。
|
||||
func (s *Service) VerifyPassword(ctx context.Context, userID int64, password string) error {
|
||||
var hash string
|
||||
|
||||
@@ -16,6 +16,7 @@ func runPasswd(args []string) error {
|
||||
fs := flag.NewFlagSet("passwd", flag.ExitOnError)
|
||||
configPath := fs.String("config", "config.yaml", "配置文件路径")
|
||||
username := fs.String("user", "admin", "用户名")
|
||||
newUsername := fs.String("new-username", "", "同时把用户名改成这个(留空则不改)")
|
||||
password := fs.String("password", "", "新密码;留空则随机生成并打印")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
@@ -45,6 +46,15 @@ func runPasswd(args []string) error {
|
||||
}
|
||||
|
||||
svc := auth.New(st.DB(), auth.Options{})
|
||||
|
||||
if *newUsername != "" && *newUsername != *username {
|
||||
if err := svc.SetUsername(context.Background(), *username, *newUsername); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("已将用户名 %s 改为 %s\n", *username, *newUsername)
|
||||
*username = *newUsername
|
||||
}
|
||||
|
||||
if err := svc.SetPassword(context.Background(), *username, pwd, generated); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user