OpenFlyers SSO WordPress plugin 1.2.0
Read the member name and email from a custom report (profile_report_id).
This commit is contained in:
@@ -252,6 +252,80 @@ class OF_SSO_Client
|
||||
return $this->apiRequest($this->config['resource_uri'], $post, $token, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the member's name and e-mail from an OpenFlyers custom report.
|
||||
*
|
||||
* The report filters on the reserved $connectedPersonId variable, which OpenFlyers replaces by the
|
||||
* user of the access token: no member identifier is sent, so a member can only read their own row.
|
||||
* It returns one row with the 'first_name', 'last_name' and 'email' columns. Requires the
|
||||
* 'reports.readonly' scope.
|
||||
*
|
||||
* @param array $token Token array returned by exchangeCode().
|
||||
* @param int $reportId Identifier of the custom report in OpenFlyers.
|
||||
* @param int $ofUserId OpenFlyers identifier of the member, used in the error message only.
|
||||
* @return array|false Profile with the first_name, last_name, user_email and display_name keys
|
||||
* that the report provides, or false on failure.
|
||||
*/
|
||||
public function getMemberProfile(array $token, $reportId, $ofUserId)
|
||||
{
|
||||
$csv = $this->getReport($token, $reportId, 'report');
|
||||
|
||||
if (!is_string($csv)) {
|
||||
$this->lastError = $this->lastError ? $this->lastError : 'Unexpected report response';
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = self::parseReportCsv($csv);
|
||||
|
||||
if (empty($row)) {
|
||||
$this->lastError = 'The report returned no row for member ' . (int) $ofUserId;
|
||||
return false;
|
||||
}
|
||||
|
||||
$profile = array();
|
||||
|
||||
foreach (array('first_name' => 'first_name', 'last_name' => 'last_name', 'email' => 'user_email') as $column => $field) {
|
||||
if (isset($row[$column]) and trim($row[$column]) !== '') {
|
||||
$profile[$field] = trim($row[$column]);
|
||||
}
|
||||
}
|
||||
|
||||
$displayName = trim((isset($profile['first_name']) ? $profile['first_name'] : '') . ' ' . (isset($profile['last_name']) ? $profile['last_name'] : ''));
|
||||
|
||||
if ($displayName !== '') {
|
||||
$profile['display_name'] = $displayName;
|
||||
}
|
||||
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first data row of a report exported by OpenFlyers, keyed by column name.
|
||||
*
|
||||
* OpenFlyers writes one header line, ';' as separator, '\n' as end of line, and replaces any ';'
|
||||
* found in the data by ',' : a plain split is therefore exact.
|
||||
*
|
||||
* @param string $csv CSV text returned by the resource server.
|
||||
* @return array Column name => value, empty when the report has no data row.
|
||||
*/
|
||||
public static function parseReportCsv($csv)
|
||||
{
|
||||
$lines = preg_split('/\r\n|\n/', trim((string) $csv, "\r\n"));
|
||||
|
||||
if (count($lines) < 2 or trim($lines[1]) === '') {
|
||||
return array();
|
||||
}
|
||||
|
||||
$header = array_map('trim', explode(';', $lines[0]));
|
||||
$values = explode(';', $lines[1]);
|
||||
|
||||
if (count($header) !== count($values)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return array_combine($header, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a token on the authorization server.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user