Created
September 25, 2012 15:13
-
-
Save StephenKing/3782518 to your computer and use it in GitHub Desktop.
Gerrit authentication
This file contains hidden or 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
<?php | |
class AccountSync { | |
protected $apiKey; | |
protected $pdo; | |
protected $accountIds = array(); | |
protected $statements = array(); | |
public function __construct($apiKey) { | |
$this->apiKey = $apiKey; | |
$config = @parse_ini_file('/var/gerrit/review/etc/gerrit.config', TRUE); | |
$secureConfig = @parse_ini_file('/var/gerrit/review/etc/secure.config', TRUE); | |
$this->pdo = new PDO('mysql:host='. $config['database']['hostname'] . ';dbname=' . $config['database']['database'], $config['database']['username'], $secureConfig['database']['password']); | |
$this->statements['selectAccountId'] = $this->pdo->prepare('SELECT account_id FROM account_external_ids WHERE external_id=?'); | |
$this->statements['updateAccount'] = $this->pdo->prepare('UPDATE accounts SET full_name=?, preferred_email=? WHERE account_id=?'); | |
$this->statements['updateExternalIdEmail'] = $this->pdo->prepare('UPDATE account_external_ids SET email_address=?, external_id=? WHERE account_id=? AND email_address IS NOT NULL'); | |
$this->statements['updateCla'] = $this->pdo->prepare('UPDATE account_agreements SET status=? WHERE account_id=?'); | |
$this->statements['insertAccount'] = $this->pdo->prepare('INSERT INTO accounts (full_name, preferred_email, account_id) VALUES (?, ?, ?)'); | |
$this->statements['insertExternalId'] = $this->pdo->prepare('INSERT INTO account_external_ids (email_address, external_id, account_id) VALUES (?, ?, ?)'); | |
$this->statements['insertCla'] = $this->pdo->prepare('INSERT INTO account_agreements (status, account_id, cla_id) VALUES (?, ?, 0)'); | |
} | |
public function sync() { | |
$url = 'https://example.org/services/userinfo.php?' . http_build_query(array('apiKey' => $this->apiKey)); | |
$json = file_get_contents($url); | |
$accounts = json_decode($json); | |
foreach ($accounts as $account) { | |
$accountId = $this->fetchAccountId($account->username); | |
if (!$accountId) { | |
$this->addAccount($account); | |
} else { | |
$claState = ($account->tx_t3ocla_hassignedcla == '1') ? 'V' : 'n'; | |
$this->statements['updateAccount']->execute(array(utf8_decode($account->name), $account->email, $accountId)); | |
$this->statements['updateExternalIdEmail']->execute(array($account->email, 'mailto:' . $account->email, $accountId)); | |
$this->statements['updateCla']->execute(array($claState, $accountId)); | |
} | |
} | |
} | |
protected function fetchAccountId($username) { | |
if (!array_key_exists($username, $this->accountIds)) { | |
$this->statements['selectAccountId']->execute(array('username:' . $username)); | |
$this->accountIds[$username] = $this->statements['selectAccountId']->fetchColumn(); | |
} | |
return $this->accountIds[$username]; | |
} | |
protected function getNextAccountId() { | |
$this->pdo->exec('INSERT INTO account_id (s) VALUES (NULL)'); | |
$accountId = $this->pdo->lastInsertId(); | |
$this->pdo->exec('DELETE FROM account_id'); | |
return $accountId; | |
} | |
protected function addAccount($account) { | |
$accountId = $this->getNextAccountId(); | |
$claState = ($account->tx_t3ocla_hassignedcla == '1') ? 'V' : 'n'; | |
$this->statements['insertAccount']->execute(array(utf8_decode($account->name), $account->email, $accountId)); | |
$this->statements['insertExternalId']->execute(array($account->email, 'mailto:' . $account->email, $accountId)); | |
$this->statements['insertExternalId']->execute(array(NULL, 'username:' . $account->username, $accountId)); | |
$this->statements['insertExternalId']->execute(array(NULL, 'gerrit:' . $account->username, $accountId)); | |
$this->statements['insertCla']->execute(array($claState, $accountId)); | |
} | |
} | |
$sync = new AccountSync($apiKey); | |
$sync->sync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment