X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=bin%2Fcron%2Fgoogle_apps.php;h=ee4a1801b4dd3ffde5cf64caf23b763039d6a61a;hb=3424387cde6c635ff16c3b5459c4caa88bb76e2e;hp=698968e93a0c7b9e65bbf37f987426939a7bfd6b;hpb=bb0727ea85cddbe533517403cdb332daee3537fd;p=platal.git diff --git a/bin/cron/google_apps.php b/bin/cron/google_apps.php index 698968e..ee4a180 100755 --- a/bin/cron/google_apps.php +++ b/bin/cron/google_apps.php @@ -1,7 +1,7 @@ #!/usr/bin/php5 -q mailstorage->googleapps_domain) { exit; } /* Updates the l_userid parameter for newer user accounts. */ $res = XDB::iterator( - "SELECT g.g_account_name, a.id + "SELECT g.g_account_name, s.uid FROM gapps_accounts AS g - LEFT JOIN aliases as a ON (a.alias = g.g_account_name AND a.type = 'a_vie') - WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND a.id IS NOT NULL"); + LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife') + WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NOT NULL"); while ($account = $res->next()) { XDB::execute( "UPDATE gapps_accounts SET l_userid = {?} WHERE g_account_name = {?}", - $account['id'], $account['g_account_name']); + $account['uid'], $account['g_account_name']); } -/* Emits a warning for GApps accounts without local user_id. */ +/* Emits a warning for GApps accounts without local uid. */ $res = XDB::iterator( "SELECT g.g_account_name FROM gapps_accounts AS g - LEFT JOIN aliases as a ON (a.alias = g.g_account_name AND a.type = 'a_vie') - WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND a.id IS NULL"); + LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife') + WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NULL"); while ($account = $res->next()) { if (!preg_match("/^admin-/", $account['g_account_name'])) { - printf("Warning: GApps account '%s' has no local user_id.\n", $account['g_account_name']); + printf("Warning: GApps account '%s' has no local uid.\n", $account['g_account_name']); + } +} + +/* Updates the l_userid parameter for newer nicknames. */ +$res = XDB::iterator( + "SELECT g.g_account_name, s.uid + FROM gapps_nicknames AS g + LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife') + WHERE (g.l_userid IS NULL or g.l_userid <= 0) AND s.uid IS NOT NULL + GROUP BY g_account_name"); +while ($nickname = $res->next()) { + XDB::execute( + "UPDATE gapps_nicknames + SET l_userid = {?} + WHERE g_account_name = {?}", + $nickname['uid'], $nickname['g_account_name']); +} + +/* Emits a warning for nicknames without local uid. */ +$res = XDB::iterator( + "SELECT g.g_account_name + FROM gapps_nicknames AS g + LEFT JOIN email_source_account AS s ON (s.email = g.g_account_name AND s.type = 'forlife') + WHERE (g.l_userid IS NULL OR g.l_userid <= 0) AND s.uid IS NULL"); +while ($nickname = $res->next()) { + if (!preg_match("/^admin-/", $nickname['g_account_name'])) { + printf("Warning: Nickname '%s' has no local uid.\n", $nickname['g_account_name']); + } +} + +/* Checks that all nicknames have been synchronized to GoogleApps. Creates the + missing ones. */ +$res = XDB::iterator( + "SELECT g.l_userid AS id, s1.email AS username, s2.email AS nickname + FROM gapps_accounts AS g + INNER JOIN email_source_account AS s1 ON (s1.uid = g.l_userid AND s1.type = 'forlife') + INNER JOIN email_source_account AS s2 ON (s2.uid = g.l_userid AND s2.type = 'alias') + LEFT JOIN gapps_nicknames AS n ON (n.l_userid = g.l_userid AND n.g_nickname = s2.email) + WHERE g.g_status = 'active' AND n.g_nickname IS NULL AND g.l_userid IS NOT NULL"); +while ($nickname = $res->next()) { + // Checks that the requested nickname doesn't look like a regular forlife; + // we might run in troubler later if we don't keep the two repos. If we need + // to add a forlife-looking nickname at some point, we'll do it manually. + if (!preg_match('/^[-a-z]+\.[-a-z]+\.\d{4}$/', $nickname['nickname'])) { + $pending_tasks = XDB::fetchOneCell( + "SELECT COUNT(*) + FROM gapps_queue + WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_create' AND j_parameters = {?}", + $nickname['id'], json_encode($nickname)); + if ($pending_tasks == 0) { + XDB::execute( + "INSERT INTO gapps_queue + SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(), + p_priority = 'offline', j_type = 'n_create', j_parameters = {?}", + $nickname['id'], json_encode($nickname)); + } + } +} + +/* Checks that all nicknames in GoogleApps are also aliases on plat/al side. + Deletes the invalid ones. */ +$res = XDB::iterator( + "SELECT g.l_userid AS id, g.g_nickname AS nickname + FROM gapps_nicknames AS g + LEFT JOIN email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'alias' AND s.email = g.g_nickname) + WHERE g.l_userid IS NOT NULL AND s.email IS NULL"); +while ($nickname = $res->next()) { + $pending_tasks = XDB::fetchOneCell( + "SELECT COUNT(*) + FROM gapps_queue + WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_delete' AND j_parameters = {?}", + $nickname['id'], json_encode($nickname)); + if ($pending_tasks == 0) { + XDB::execute( + "INSERT INTO gapps_queue + SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(), + p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}", + $nickname['id'], json_encode($nickname)); } } @@ -75,5 +153,5 @@ XDB::execute( WHERE p_status = 'success' OR (p_status = 'hardfail' AND p_end_date < DATE_SUB(NOW(), INTERVAL 15 DAY))"); -// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: ?>