Impl /auth/user/update

This commit is contained in:
2022-10-03 14:14:49 +00:00
committed by GitHub
parent 7693cc3329
commit 2b6d3c05f7
4 changed files with 169 additions and 2 deletions

View File

@@ -404,6 +404,36 @@ impl PixivDownloaderSqlite {
}
}
#[cfg(feature = "server")]
async fn _update_user(
&self,
id: u64,
name: &str,
username: &str,
password: &[u8],
is_admin: bool,
) -> Result<usize, SqliteError> {
let con = self.db.lock().await;
let has_username = con
.query_row(
"SELECT * FROM users WHERE username = ?;",
[username],
|row| {
let uid: u64 = row.get(0)?;
Ok(uid != id)
},
)
.optional()?
.unwrap_or(false);
if has_username {
return Err(SqliteError::UserNameAlreadyExists);
}
Ok(con.execute(
"UPDATE users SET name = ?, username = ?, password = ?, is_admin = ? WHERE id = ?;",
(name, username, password, is_admin, id),
)?)
}
fn _write_version<'a>(&self, ts: &Transaction<'a>) -> Result<(), SqliteError> {
let mut stmt = ts.prepare(
"INSERT OR REPLACE INTO version (id, v1, v2, v3, v4) VALUES ('main', ?, ?, ?, ?);",
@@ -549,4 +579,18 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
._set_user(id, name, username, password, is_admin)
.await?)
}
#[cfg(feature = "server")]
async fn update_user(
&self,
id: u64,
name: &str,
username: &str,
password: &[u8],
is_admin: bool,
) -> Result<User, PixivDownloaderDbError> {
self._update_user(id, name, username, password, is_admin)
.await?;
Ok(self.get_user(id).await?.expect("User not found:"))
}
}

View File

@@ -93,4 +93,22 @@ pub trait PixivDownloaderDb {
password: &[u8],
is_admin: bool,
) -> Result<User, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Update a user's information
/// * `id`: The user's ID
/// * `name`: The user's name
/// * `username`: The user's username
/// * `password`: The user's hashed password
/// * `is_admin`: Whether the user is an admin
/// # Note
/// If the user does not exist, the operation must failed.
/// If the user's `username` is taked by another user, the operation must failed.
async fn update_user(
&self,
id: u64,
name: &str,
username: &str,
password: &[u8],
is_admin: bool,
) -> Result<User, PixivDownloaderDbError>;
}