Skip to content

Commit

Permalink
Use the data from the appropriate tab of the specified Google Sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
forevermatt committed Sep 5, 2024
1 parent 9fb14c1 commit ccace39
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
46 changes: 45 additions & 1 deletion application/common/components/ExternalGroupsSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace common\components;

use common\models\User;
use Webmozart\Assert\Assert;
use Yii;
use yii\base\Component;

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
38 changes: 38 additions & 0 deletions application/common/components/Sheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace common\components;

use Google\Service\Exception;
use InvalidArgumentException;
use yii\base\Component;
use yii\helpers\Json;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ccace39

Please sign in to comment.