Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: exit client after changing current user's password #458

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl<'a> FormatDisplay<'a> {
let (rows, mut rows_str, kind, total_rows, total_bytes) = match self.kind {
QueryKind::Explain => (self.rows, "rows", "explain", 0, 0),
QueryKind::Query => (self.rows, "rows", "read", stats.read_rows, stats.read_bytes),
QueryKind::Update => (
QueryKind::Update | QueryKind::AlterUserPassword => (
stats.write_rows,
"rows",
"written",
Expand Down
40 changes: 35 additions & 5 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ static VERSION_SHORT: Lazy<String> = Lazy::new(|| {
}
});

// alter current user's password tokens
const ALTER_USER_PASSWORD_TOKENS: [TokenKind; 6] = [
TokenKind::USER,
TokenKind::USER,
TokenKind::LParen,
TokenKind::RParen,
TokenKind::IDENTIFIED,
TokenKind::BY,
];

pub struct Session {
client: Client,
conn: Box<dyn Connection>,
Expand Down Expand Up @@ -386,8 +396,14 @@ impl Session {

let start = Instant::now();
let kind = QueryKind::from(query);
match (kind, is_repl) {
(QueryKind::Update, false) => {
match kind {
QueryKind::AlterUserPassword => {
// When changing the current user's password,
// exit the client and login again with the new password.
let _ = self.conn.exec(query).await?;
Ok(None)
}
QueryKind::Update => {
let affected = self.conn.exec(query).await?;
if is_repl {
if affected > 0 {
Expand All @@ -410,7 +426,7 @@ impl Session {
replace_newline_in_box_display(query)
};

let data = match other.0 {
let data = match other {
QueryKind::Put => {
let args: Vec<String> = get_put_get_args(query);
if args.len() != 3 {
Expand Down Expand Up @@ -562,6 +578,7 @@ pub enum QueryKind {
Explain,
Put,
Get,
AlterUserPassword,
}

impl From<&str> for QueryKind {
Expand All @@ -572,8 +589,21 @@ impl From<&str> for QueryKind {
TokenKind::EXPLAIN => QueryKind::Explain,
TokenKind::PUT => QueryKind::Put,
TokenKind::GET => QueryKind::Get,
TokenKind::ALTER
| TokenKind::DELETE
TokenKind::ALTER => {
let mut tzs = vec![];
while let Some(Ok(t)) = tz.next() {
tzs.push(t.kind);
if tzs.len() == ALTER_USER_PASSWORD_TOKENS.len() {
break;
}
}
if tzs == ALTER_USER_PASSWORD_TOKENS {
QueryKind::AlterUserPassword
} else {
QueryKind::Update
}
}
TokenKind::DELETE
| TokenKind::UPDATE
| TokenKind::INSERT
| TokenKind::CREATE
Expand Down
Loading