server: add support for creating a user with attributes

This commit is contained in:
Valentin Tolmer
2023-09-25 01:18:02 +02:00
committed by nitnelave
parent 81204dcee5
commit 3fadfb1944
4 changed files with 30 additions and 3 deletions
+1
View File
@@ -104,6 +104,7 @@ pub struct CreateUserRequest {
pub first_name: Option<String>, pub first_name: Option<String>,
pub last_name: Option<String>, pub last_name: Option<String>,
pub avatar: Option<JpegPhoto>, pub avatar: Option<JpegPhoto>,
pub attributes: Vec<AttributeValue>,
} }
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)]
+27 -3
View File
@@ -312,7 +312,7 @@ impl UserBackendHandler for SqlBackendHandler {
} }
if let Some(avatar) = request.avatar { if let Some(avatar) = request.avatar {
new_user_attributes.push(model::user_attributes::ActiveModel { new_user_attributes.push(model::user_attributes::ActiveModel {
user_id: Set(request.user_id), user_id: Set(request.user_id.clone()),
attribute_name: Set("avatar".to_owned()), attribute_name: Set("avatar".to_owned()),
value: Set(Serialized::from(&avatar)), value: Set(Serialized::from(&avatar)),
}); });
@@ -320,6 +320,26 @@ impl UserBackendHandler for SqlBackendHandler {
self.sql_pool self.sql_pool
.transaction::<_, (), DomainError>(|transaction| { .transaction::<_, (), DomainError>(|transaction| {
Box::pin(async move { Box::pin(async move {
let schema = Self::get_schema_with_transaction(transaction).await?;
for attribute in request.attributes {
if schema
.user_attributes
.get_attribute_type(&attribute.name)
.is_some()
{
new_user_attributes.push(model::user_attributes::ActiveModel {
user_id: Set(request.user_id.clone()),
attribute_name: Set(attribute.name),
value: Set(attribute.value),
});
} else {
return Err(DomainError::InternalError(format!(
"Attribute name {} doesn't exist in the schema,
yet was attempted to be inserted in the database",
&attribute.name
)));
}
}
new_user.insert(transaction).await?; new_user.insert(transaction).await?;
if !new_user_attributes.is_empty() { if !new_user_attributes.is_empty() {
model::UserAttributes::insert_many(new_user_attributes) model::UserAttributes::insert_many(new_user_attributes)
@@ -982,9 +1002,13 @@ mod tests {
user_id: UserId::new("james"), user_id: UserId::new("james"),
email: "email".to_string(), email: "email".to_string(),
display_name: Some("display_name".to_string()), display_name: Some("display_name".to_string()),
first_name: Some("first_name".to_string()), first_name: None,
last_name: Some("last_name".to_string()), last_name: Some("last_name".to_string()),
avatar: Some(JpegPhoto::for_tests()), avatar: Some(JpegPhoto::for_tests()),
attributes: vec![AttributeValue {
name: "first_name".to_owned(),
value: Serialized::from("First Name"),
}],
}) })
.await .await
.unwrap(); .unwrap();
@@ -1005,7 +1029,7 @@ mod tests {
}, },
AttributeValue { AttributeValue {
name: "first_name".to_owned(), name: "first_name".to_owned(),
value: Serialized::from("first_name") value: Serialized::from("First Name")
}, },
AttributeValue { AttributeValue {
name: "last_name".to_owned(), name: "last_name".to_owned(),
+1
View File
@@ -104,6 +104,7 @@ impl<Handler: BackendHandler> Mutation<Handler> {
first_name: user.first_name, first_name: user.first_name,
last_name: user.last_name, last_name: user.last_name,
avatar, avatar,
..Default::default()
}) })
.instrument(span.clone()) .instrument(span.clone())
.await?; .await?;
+1
View File
@@ -711,6 +711,7 @@ impl<Backend: BackendHandler + LoginHandler + OpaqueHandler> LdapHandler<Backend
code: LdapResultCode::ConstraintViolation, code: LdapResultCode::ConstraintViolation,
message: format!("Invalid JPEG photo: {:#?}", e), message: format!("Invalid JPEG photo: {:#?}", e),
})?, })?,
..Default::default()
}) })
.await .await
.map_err(|e| LdapError { .map_err(|e| LdapError {