-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial implementation of cron-based external-groups sync
- Loading branch information
1 parent
763a853
commit 930e932
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace common\components; | ||
|
||
use common\models\User; | ||
use Yii; | ||
use yii\base\Component; | ||
|
||
class ExternalGroupsSync extends Component | ||
{ | ||
public const MAX_SYNC_SETS = 20; | ||
|
||
public static function syncAllSets(array $syncSetsParams) | ||
{ | ||
for ($i = 1; $i <= self::MAX_SYNC_SETS; $i++) { | ||
$appPrefixKey = sprintf('set%uAppPrefix', $i); | ||
$googleSheetIdKey = sprintf('set%uGoogleSheetId', $i); | ||
|
||
if (! array_key_exists($appPrefixKey, $syncSetsParams)) { | ||
Yii::warning(sprintf( | ||
'Finished syncing external groups after %s sync set(s).', | ||
($i - 1) | ||
)); | ||
break; | ||
} | ||
|
||
$appPrefix = $syncSetsParams[$appPrefixKey] ?? null; | ||
$googleSheetId = $syncSetsParams[$googleSheetIdKey] ?? null; | ||
|
||
if (empty($appPrefix) || empty($googleSheetId)) { | ||
Yii::error(sprintf( | ||
'Unable to do external-groups sync set %s: ' | ||
. 'app-prefix (%s) or Google Sheet ID (%s) was empty.', | ||
$i, | ||
json_encode($appPrefix), | ||
json_encode($googleSheetId), | ||
)); | ||
} else { | ||
Yii::warning(sprintf( | ||
'Syncing %s external groups from Google Sheet (%s)...', | ||
json_encode($appPrefix), | ||
$googleSheetId | ||
)); | ||
self::syncSet($appPrefix, $googleSheetId); | ||
} | ||
} | ||
} | ||
|
||
private static function syncSet(string $appPrefix, string $googleSheetId) | ||
{ | ||
$desiredExternalGroups = self::getExternalGroupsFromGoogleSheet($googleSheetId); | ||
$errors = User::syncExternalGroups($appPrefix, $desiredExternalGroups); | ||
Yii::warning(sprintf( | ||
'Ran sync for %s external groups.', | ||
$appPrefix | ||
)); | ||
|
||
if (! empty($errors)) { | ||
Yii::error(sprintf( | ||
"Errors that occurred while syncing %s external groups: \n%s", | ||
$appPrefix, | ||
join("\n", $errors) | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,3 +337,11 @@ [email protected] | |
|
||
# spreadsheet ID | ||
GOOGLE_spreadsheetId=putAnActualSheetIDHerejD70xAjqPnOCHlDK3YomH | ||
|
||
# === external groups sync sets === # | ||
|
||
# EXTERNAL_GROUPS_SYNC_set1AppPrefix= | ||
# EXTERNAL_GROUPS_SYNC_set1GoogleSheetId= | ||
# EXTERNAL_GROUPS_SYNC_set2AppPrefix= | ||
# EXTERNAL_GROUPS_SYNC_set2GoogleSheetId= | ||
# ... with as many sets as you want. |