Skip to content

Commit

Permalink
Cannot create new user from shvspy #14
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Nov 8, 2024
1 parent 8a10c3a commit 963d8ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shvbroker"
version = "3.2.1"
version = "3.2.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
42 changes: 41 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,26 @@ impl User {
let cpon = serde_json::to_string(self).map_err(|e| e.to_string())?;
RpcValue::from_cpon(&cpon).map_err(|e| e.to_string())
}
fn from_v2(user: UserV2) -> Result<Self, String> {
Ok(Self {
password: Password::from_v2(user.password)?,
roles: user.roles
})
}
}
impl TryFrom<&RpcValue> for User {
type Error = String;
fn try_from(value: &RpcValue) -> Result<Self, Self::Error> {
let cpon = value.to_cpon();
serde_json::from_str(&cpon).map_err(|e| e.to_string())
match serde_json::from_str(&cpon) {
Ok(user) => { Ok(user) }
Err(e) => {
match UserV2::try_from(cpon.as_str()) {
Ok(user) => { User::from_v2(user) }
Err(_) => { Err(e.to_string()) }
}
}
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -82,6 +96,32 @@ pub enum Password {
Plain(String),
Sha1(String),
}
impl Password {
fn from_v2(password: PasswordV2) -> Result<Self, String> {
let format = password.format.to_lowercase();
match format.as_str() {
"plain" => { Ok(Password::Plain(password.password)) }
"sha1" => { Ok(Password::Sha1(password.password)) }
s => { Err(format!("Invalid password format {s}")) }
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserV2 {
pub password: PasswordV2,
pub roles: Vec<String>,
}
impl TryFrom<&str> for UserV2 {
type Error = String;
fn try_from(cpon: &str) -> Result<Self, Self::Error> {
serde_json::from_str(&cpon).map_err(|e| e.to_string())
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PasswordV2 {
format: String,
password: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(PartialEq)]
pub struct Role {
Expand Down
14 changes: 9 additions & 5 deletions src/shvnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,15 @@ impl ShvNode for crate::shvnode::BrokerAccessUsersNode {
let param = param.as_list();
let key = param.first().ok_or("Key is missing")?;
let rv = param.get(1).and_then(|m| if m.is_null() {None} else {Some(m)});
let user = rv.map(crate::config::User::try_from);
let user = match user {
None => None,
Some(Ok(user)) => {Some(user)}
Some(Err(e)) => { return Err(e.into() )}
let user = if let Some(rv) = rv {
match crate::config::User::try_from(rv) {
Ok(user) => { Some(user) }
Err(e) => {
return Err(e.into())
}
}
} else {
None
};
state_writer(&ctx.state).set_access_user(key.as_str(), user);
Ok(ProcessRequestRetval::Retval(().into()))
Expand Down

0 comments on commit 963d8ed

Please sign in to comment.