diff --git a/v-api/src/context.rs b/v-api/src/context.rs index c66a234..3ce61d1 100644 --- a/v-api/src/context.rs +++ b/v-api/src/context.rs @@ -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 @@ -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 @@ -988,13 +1008,13 @@ where pub async fn update_api_user_provider( &self, caller: &Caller, - api_user: NewApiUserProvider, + api_user_provider: NewApiUserProvider, ) -> ResourceResult { 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 { @@ -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, diff --git a/v-model/migrations/2024-02-14-042831_add_display_name/down.sql b/v-model/migrations/2024-02-14-042831_add_display_name/down.sql new file mode 100644 index 0000000..2728246 --- /dev/null +++ b/v-model/migrations/2024-02-14-042831_add_display_name/down.sql @@ -0,0 +1 @@ +ALTER TABLE api_user_provider DROP COLUMN display_names; \ No newline at end of file diff --git a/v-model/migrations/2024-02-14-042831_add_display_name/up.sql b/v-model/migrations/2024-02-14-042831_add_display_name/up.sql new file mode 100644 index 0000000..c087624 --- /dev/null +++ b/v-model/migrations/2024-02-14-042831_add_display_name/up.sql @@ -0,0 +1 @@ +ALTER TABLE api_user_provider ADD COLUMN display_names TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[]; diff --git a/v-model/src/db.rs b/v-model/src/db.rs index 3c79b23..c9c6226 100644 --- a/v-model/src/db.rs +++ b/v-model/src/db.rs @@ -52,6 +52,7 @@ pub struct ApiUserProviderModel { pub created_at: DateTime, pub updated_at: DateTime, pub deleted_at: Option>, + pub display_names: Vec>, } #[derive(Debug, Deserialize, Serialize, Queryable, Insertable)] diff --git a/v-model/src/lib.rs b/v-model/src/lib.rs index e938bd4..f762807 100644 --- a/v-model/src/lib.rs +++ b/v-model/src/lib.rs @@ -48,6 +48,7 @@ pub struct ApiUserProvider { pub provider: String, pub provider_id: String, pub emails: Vec, + pub display_names: Vec, #[partial(NewApiUserProvider(skip))] pub created_at: DateTime, #[partial(NewApiUserProvider(skip))] diff --git a/v-model/src/schema.rs b/v-model/src/schema.rs index 09b4fac..e3803fe 100644 --- a/v-model/src/schema.rs +++ b/v-model/src/schema.rs @@ -65,6 +65,7 @@ diesel::table! { created_at -> Timestamptz, updated_at -> Timestamptz, deleted_at -> Nullable, + display_names -> Array>, } } diff --git a/v-model/src/storage/postgres.rs b/v-model/src/storage/postgres.rs index 6cb1ca0..ca5f240 100644 --- a/v-model/src/storage/postgres.rs +++ b/v-model/src/storage/postgres.rs @@ -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, @@ -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?) @@ -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, @@ -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,