Skip to content

Commit

Permalink
Integrate display names
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Feb 21, 2024
1 parent db74d3f commit c050b67
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
29 changes: 25 additions & 4 deletions v-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ where
emails: info.verified_emails,
provider: info.external_id.provider().to_string(),
provider_id: info.external_id.id().to_string(),
// TODO: Refactor in generic display name across providers. This cascades
// into changes needed within mappers
display_names: info
.github_username
.map(|name| vec![name])
.unwrap_or_default(),
},
)
.await
Expand All @@ -618,7 +624,21 @@ where
tracing::info!("Found an existing user. Ensuring mapped permissions and groups.");

// This branch ensures that there is a 0th indexed item
let provider = api_user_providers.into_iter().nth(0).unwrap();
let mut provider = api_user_providers.into_iter().nth(0).unwrap();

// Update the provider with the newest user info
provider.emails = info.verified_emails;
provider.display_names = info
.github_username
.map(|name| vec![name])
.unwrap_or_default();

tracing::info!(?provider.id, "Updating provider for user");

self.update_api_user_provider(caller, provider.clone().into())
.await
.map_err(|err| err.into())
.to_resource_result()?;

// Update the found user to ensure it has at least the mapped permissions and groups
let user = self
Expand Down Expand Up @@ -988,13 +1008,13 @@ where
pub async fn update_api_user_provider(
&self,
caller: &Caller<T>,
api_user: NewApiUserProvider,
api_user_provider: NewApiUserProvider,
) -> ResourceResult<ApiUserProvider, StoreError> {
if caller.any(&[
&ApiPermission::UpdateApiUser(api_user.id).into(),
&ApiPermission::UpdateApiUser(api_user_provider.id).into(),
&ApiPermission::UpdateApiUserAll.into(),
]) {
ApiUserProviderStore::upsert(&*self.storage, api_user)
ApiUserProviderStore::upsert(&*self.storage, api_user_provider)
.await
.to_resource_result()
} else {
Expand Down Expand Up @@ -1584,6 +1604,7 @@ mod tests {
provider: "test".to_string(),
provider_id: "test_id".to_string(),
emails: vec![],
display_names: vec![],
created_at: Utc::now(),
updated_at: Utc::now(),
deleted_at: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE api_user_provider DROP COLUMN display_names;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE api_user_provider ADD COLUMN display_names TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[];
1 change: 1 addition & 0 deletions v-model/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct ApiUserProviderModel {
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
pub display_names: Vec<Option<String>>,
}

#[derive(Debug, Deserialize, Serialize, Queryable, Insertable)]
Expand Down
1 change: 1 addition & 0 deletions v-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct ApiUserProvider {
pub provider: String,
pub provider_id: String,
pub emails: Vec<String>,
pub display_names: Vec<String>,
#[partial(NewApiUserProvider(skip))]
pub created_at: DateTime<Utc>,
#[partial(NewApiUserProvider(skip))]
Expand Down
1 change: 1 addition & 0 deletions v-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ diesel::table! {
created_at -> Timestamptz,
updated_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
display_names -> Array<Nullable<Text>>,
}
}

Expand Down
17 changes: 17 additions & 0 deletions v-model/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ impl ApiUserProviderStore for PostgresStore {
provider: provider.provider,
provider_id: provider.provider_id,
emails: provider.emails.into_iter().filter_map(|e| e).collect(),
display_names: provider
.display_names
.into_iter()
.filter_map(|d| d)
.collect(),
created_at: provider.created_at,
updated_at: provider.updated_at,
deleted_at: provider.deleted_at,
Expand All @@ -402,11 +407,13 @@ impl ApiUserProviderStore for PostgresStore {
api_user_provider::provider.eq(provider.provider),
api_user_provider::provider_id.eq(provider.provider_id),
api_user_provider::emails.eq(provider.emails),
api_user_provider::display_names.eq(provider.display_names),
))
.on_conflict(api_user_provider::id)
.do_update()
.set((
api_user_provider::emails.eq(excluded(api_user_provider::emails)),
api_user_provider::display_names.eq(excluded(api_user_provider::display_names)),
api_user_provider::updated_at.eq(Utc::now()),
))
.get_result_async(&*self.pool.get().await?)
Expand All @@ -418,6 +425,11 @@ impl ApiUserProviderStore for PostgresStore {
provider: provider_m.provider,
provider_id: provider_m.provider_id,
emails: provider_m.emails.into_iter().filter_map(|e| e).collect(),
display_names: provider_m
.display_names
.into_iter()
.filter_map(|d| d)
.collect(),
created_at: provider_m.created_at,
updated_at: provider_m.updated_at,
deleted_at: provider_m.deleted_at,
Expand Down Expand Up @@ -447,6 +459,11 @@ impl ApiUserProviderStore for PostgresStore {
provider: provider_m.provider,
provider_id: provider_m.provider_id,
emails: provider_m.emails.into_iter().filter_map(|e| e).collect(),
display_names: provider_m
.display_names
.into_iter()
.filter_map(|d| d)
.collect(),
created_at: provider_m.created_at,
updated_at: provider_m.updated_at,
deleted_at: provider_m.deleted_at,
Expand Down

0 comments on commit c050b67

Please sign in to comment.