mirror of
https://github.com/lldap/lldap.git
synced 2024-11-25 09:06:03 +00:00
server: map email and display_name from attributes into user fields
This commit is contained in:
parent
b1384818d2
commit
6cf0f6df06
5 changed files with 66 additions and 23 deletions
|
@ -88,7 +88,7 @@ impl CommonComponent<CreateUserForm> for CreateUserForm {
|
||||||
let req = create_user::Variables {
|
let req = create_user::Variables {
|
||||||
user: create_user::CreateUserInput {
|
user: create_user::CreateUserInput {
|
||||||
id: model.username,
|
id: model.username,
|
||||||
email: model.email,
|
email: Some(model.email),
|
||||||
displayName: to_option(model.display_name),
|
displayName: to_option(model.display_name),
|
||||||
firstName: to_option(model.first_name),
|
firstName: to_option(model.first_name),
|
||||||
lastName: to_option(model.last_name),
|
lastName: to_option(model.last_name),
|
||||||
|
|
|
@ -189,7 +189,7 @@ impl TryFrom<ResultEntry> for User {
|
||||||
Ok(User::new(
|
Ok(User::new(
|
||||||
crate::lldap::CreateUserInput {
|
crate::lldap::CreateUserInput {
|
||||||
id,
|
id,
|
||||||
email,
|
email: Some(email),
|
||||||
display_name,
|
display_name,
|
||||||
first_name,
|
first_name,
|
||||||
last_name,
|
last_name,
|
||||||
|
|
2
schema.graphql
generated
2
schema.graphql
generated
|
@ -63,7 +63,7 @@ type Query {
|
||||||
"The details required to create a user."
|
"The details required to create a user."
|
||||||
input CreateUserInput {
|
input CreateUserInput {
|
||||||
id: String!
|
id: String!
|
||||||
email: String!
|
email: String
|
||||||
displayName: String
|
displayName: String
|
||||||
firstName: String
|
firstName: String
|
||||||
lastName: String
|
lastName: String
|
||||||
|
|
|
@ -7,8 +7,9 @@ use crate::{
|
||||||
AttributeList, BackendHandler, CreateAttributeRequest, CreateGroupRequest,
|
AttributeList, BackendHandler, CreateAttributeRequest, CreateGroupRequest,
|
||||||
CreateUserRequest, UpdateGroupRequest, UpdateUserRequest,
|
CreateUserRequest, UpdateGroupRequest, UpdateUserRequest,
|
||||||
},
|
},
|
||||||
|
schema::PublicSchema,
|
||||||
types::{
|
types::{
|
||||||
AttributeName, AttributeType, AttributeValue as DomainAttributeValue, GroupId,
|
AttributeName, AttributeType, AttributeValue as DomainAttributeValue, Email, GroupId,
|
||||||
JpegPhoto, LdapObjectClass, UserId,
|
JpegPhoto, LdapObjectClass, UserId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -39,7 +40,7 @@ impl<Handler: BackendHandler> Mutation<Handler> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
|
#[derive(Clone, PartialEq, Eq, Debug, GraphQLInputObject)]
|
||||||
// This conflicts with the attribute values returned by the user/group queries.
|
// This conflicts with the attribute values returned by the user/group queries.
|
||||||
#[graphql(name = "AttributeValueInput")]
|
#[graphql(name = "AttributeValueInput")]
|
||||||
struct AttributeValue {
|
struct AttributeValue {
|
||||||
|
@ -58,7 +59,8 @@ struct AttributeValue {
|
||||||
/// The details required to create a user.
|
/// The details required to create a user.
|
||||||
pub struct CreateUserInput {
|
pub struct CreateUserInput {
|
||||||
id: String,
|
id: String,
|
||||||
email: String,
|
// The email can be specified as an attribute, but one of the two is required.
|
||||||
|
email: Option<String>,
|
||||||
display_name: Option<String>,
|
display_name: Option<String>,
|
||||||
first_name: Option<String>,
|
first_name: Option<String>,
|
||||||
last_name: Option<String>,
|
last_name: Option<String>,
|
||||||
|
@ -120,6 +122,44 @@ impl Success {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct UnpackedAttributes {
|
||||||
|
email: Option<Email>,
|
||||||
|
display_name: Option<String>,
|
||||||
|
attributes: Vec<DomainAttributeValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unpack_attributes(
|
||||||
|
attributes: Vec<AttributeValue>,
|
||||||
|
schema: &PublicSchema,
|
||||||
|
is_admin: bool,
|
||||||
|
) -> FieldResult<UnpackedAttributes> {
|
||||||
|
let email = attributes
|
||||||
|
.iter()
|
||||||
|
.find(|attr| attr.name == "mail")
|
||||||
|
.cloned()
|
||||||
|
.map(|attr| deserialize_attribute(&schema.get_schema().user_attributes, attr, is_admin))
|
||||||
|
.transpose()?
|
||||||
|
.map(|attr| attr.value.unwrap::<String>())
|
||||||
|
.map(Email::from);
|
||||||
|
let display_name = attributes
|
||||||
|
.iter()
|
||||||
|
.find(|attr| attr.name == "displayName")
|
||||||
|
.cloned()
|
||||||
|
.map(|attr| deserialize_attribute(&schema.get_schema().user_attributes, attr, is_admin))
|
||||||
|
.transpose()?
|
||||||
|
.map(|attr| attr.value.unwrap::<String>());
|
||||||
|
let attributes = attributes
|
||||||
|
.into_iter()
|
||||||
|
.filter(|attr| attr.name != "mail" && attr.name != "displayName")
|
||||||
|
.map(|attr| deserialize_attribute(&schema.get_schema().user_attributes, attr, is_admin))
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
Ok(UnpackedAttributes {
|
||||||
|
email,
|
||||||
|
display_name,
|
||||||
|
attributes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[graphql_object(context = Context<Handler>)]
|
#[graphql_object(context = Context<Handler>)]
|
||||||
impl<Handler: BackendHandler> Mutation<Handler> {
|
impl<Handler: BackendHandler> Mutation<Handler> {
|
||||||
async fn create_user(
|
async fn create_user(
|
||||||
|
@ -143,17 +183,20 @@ impl<Handler: BackendHandler> Mutation<Handler> {
|
||||||
.transpose()
|
.transpose()
|
||||||
.context("Provided image is not a valid JPEG")?;
|
.context("Provided image is not a valid JPEG")?;
|
||||||
let schema = handler.get_schema().await?;
|
let schema = handler.get_schema().await?;
|
||||||
let attributes = user
|
let UnpackedAttributes {
|
||||||
.attributes
|
email,
|
||||||
.unwrap_or_default()
|
display_name,
|
||||||
.into_iter()
|
attributes,
|
||||||
.map(|attr| deserialize_attribute(&schema.get_schema().user_attributes, attr, true))
|
} = unpack_attributes(user.attributes.unwrap_or_default(), &schema, true)?;
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
|
||||||
handler
|
handler
|
||||||
.create_user(CreateUserRequest {
|
.create_user(CreateUserRequest {
|
||||||
user_id: user_id.clone(),
|
user_id: user_id.clone(),
|
||||||
email: user.email.into(),
|
email: user
|
||||||
display_name: user.display_name,
|
.email
|
||||||
|
.map(Email::from)
|
||||||
|
.or(email)
|
||||||
|
.ok_or_else(|| anyhow!("Email is required when creating a new user"))?,
|
||||||
|
display_name: user.display_name.or(display_name),
|
||||||
first_name: user.first_name,
|
first_name: user.first_name,
|
||||||
last_name: user.last_name,
|
last_name: user.last_name,
|
||||||
avatar,
|
avatar,
|
||||||
|
@ -216,17 +259,17 @@ impl<Handler: BackendHandler> Mutation<Handler> {
|
||||||
.transpose()
|
.transpose()
|
||||||
.context("Provided image is not a valid JPEG")?;
|
.context("Provided image is not a valid JPEG")?;
|
||||||
let schema = handler.get_schema().await?;
|
let schema = handler.get_schema().await?;
|
||||||
let insert_attributes = user
|
let user_insert_attributes = user.insert_attributes.unwrap_or_default();
|
||||||
.insert_attributes
|
let UnpackedAttributes {
|
||||||
.unwrap_or_default()
|
email,
|
||||||
.into_iter()
|
display_name,
|
||||||
.map(|attr| deserialize_attribute(&schema.get_schema().user_attributes, attr, is_admin))
|
attributes: insert_attributes,
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
} = unpack_attributes(user_insert_attributes, &schema, is_admin)?;
|
||||||
handler
|
handler
|
||||||
.update_user(UpdateUserRequest {
|
.update_user(UpdateUserRequest {
|
||||||
user_id,
|
user_id,
|
||||||
email: user.email.map(Into::into),
|
email: user.email.map(Into::into).or(email),
|
||||||
display_name: user.display_name,
|
display_name: user.display_name.or(display_name),
|
||||||
first_name: user.first_name,
|
first_name: user.first_name,
|
||||||
last_name: user.last_name,
|
last_name: user.last_name,
|
||||||
avatar,
|
avatar,
|
||||||
|
|
|
@ -103,7 +103,7 @@ impl LLDAPFixture {
|
||||||
create_user::Variables {
|
create_user::Variables {
|
||||||
user: create_user::CreateUserInput {
|
user: create_user::CreateUserInput {
|
||||||
id: user.clone(),
|
id: user.clone(),
|
||||||
email: format!("{}@lldap.test", user),
|
email: Some(format!("{}@lldap.test", user)),
|
||||||
avatar: None,
|
avatar: None,
|
||||||
display_name: None,
|
display_name: None,
|
||||||
first_name: None,
|
first_name: None,
|
||||||
|
|
Loading…
Reference in a new issue