From ccace395f7c17c7bf28e40e72e551a9e3519c382 Mon Sep 17 00:00:00 2001 From: Matt H Date: Thu, 5 Sep 2024 11:57:16 -0400 Subject: [PATCH] Use the data from the appropriate tab of the specified Google Sheet --- .../common/components/ExternalGroupsSync.php | 46 ++++++++++++++++++- application/common/components/Sheets.php | 38 +++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/application/common/components/ExternalGroupsSync.php b/application/common/components/ExternalGroupsSync.php index 213ec761..4e59d667 100644 --- a/application/common/components/ExternalGroupsSync.php +++ b/application/common/components/ExternalGroupsSync.php @@ -3,6 +3,7 @@ namespace common\components; use common\models\User; +use Webmozart\Assert\Assert; use Yii; use yii\base\Component; @@ -49,7 +50,7 @@ public static function syncAllSets(array $syncSetsParams) private static function syncSet(string $appPrefix, string $googleSheetId) { $desiredExternalGroups = self::getExternalGroupsFromGoogleSheet($googleSheetId); - $errors = User::syncExternalGroups($appPrefix, $desiredExternalGroups); + $errors = User::updateUsersExternalGroups($appPrefix, $desiredExternalGroups); Yii::warning(sprintf( "Ran sync for '%s' external groups.", $appPrefix @@ -63,4 +64,47 @@ private static function syncSet(string $appPrefix, string $googleSheetId) )); } } + + /** + * Get the desired external-group values, indexed by email address, from the + * specified Google Sheet, from the tab named after this IDP's code name + * (i.e. the name used in this IDP's subdomain). + * + * @throws \Google\Service\Exception + */ + private static function getExternalGroupsFromGoogleSheet(string $googleSheetId): array + { + $googleSheetsClient = new Sheets([ + 'applicationName' => Yii::$app->params['google']['applicationName'], + 'jsonAuthFilePath' => Yii::$app->params['google']['jsonAuthFilePath'], + 'jsonAuthString' => Yii::$app->params['google']['jsonAuthString'], + 'spreadsheetId' => $googleSheetId, + ]); + $tabName = Yii::$app->params['idpName']; + + $values = $googleSheetsClient->getValuesFromTab($tabName); + $columnLabels = $values[0]; + + Assert::eq($columnLabels[0], 'email', sprintf( + "The first column in the '%s' tab must be 'email'", + $tabName + )); + Assert::eq($columnLabels[1], 'groups', sprintf( + "The second column in the '%s' tab must be 'groups'", + $tabName + )); + Assert::eq( + count($columnLabels), + 2, + 'There should only be two columns with values' + ); + + $data = []; + for ($i = 1; $i < count($values); $i++) { + $email = trim($values[$i][0]); + $groups = trim($values[$i][1] ?? ''); + $data[$email] = $groups; + } + return $data; + } } diff --git a/application/common/components/Sheets.php b/application/common/components/Sheets.php index 3c989847..848e21dd 100644 --- a/application/common/components/Sheets.php +++ b/application/common/components/Sheets.php @@ -2,6 +2,7 @@ namespace common\components; +use Google\Service\Exception; use InvalidArgumentException; use yii\base\Component; use yii\helpers\Json; @@ -127,6 +128,43 @@ public function getHeader() return $header['values'][0]; } + /** + * Get all the values from the specified tab in this Google Sheet. + * + * @param string $tabName + * @param string $range + * @return array[] + * Example: + * ``` + * [ + * [ + * "A1's value", + * "B1's value" + * ], + * [ + * "A2's value", + * "B2's value" + * ], + * [ + * "A3's value", + * "B3's value" + * ] + * ] + * ``` + * @throws Exception + */ + public function getValuesFromTab(string $tabName, string $range = 'A:ZZ'): array + { + $client = $this->getGoogleClient(); + + $valueRange = $client->spreadsheets_values->get( + $this->spreadsheetId, + $tabName . '!' . $range + ); + + return $valueRange->values; + } + /** * @param string[] $header * @param array $records