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
|
||||
|
||||
Reference in new issue
Block a user