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:
Polyedre
2026-11-18 20:30:16 +01:00
parent c5f6232509
commit 6b54fd524b
6 changed files with 363 additions and 42 deletions

View File

@@ -93,6 +93,9 @@ class OF_SSO_Admin
$output[$key] = isset($input[$key]) ? trim(wp_strip_all_tags($input[$key])) : '';
}
// 0 means "no report": members then keep the technical name built from their identifier.
$output['profile_report_id'] = isset($input['profile_report_id']) ? absint($input['profile_report_id']) : 0;
$boolKeys = array('send_x_ssl_cert', 'auto_create', 'show_login_button', 'debug');
foreach ($boolKeys as $key) {
$output[$key] = empty($input[$key]) ? 0 : 1;
@@ -192,6 +195,8 @@ class OF_SSO_Admin
self::checkboxRow('auto_create', __('Creer automatiquement les comptes', 'openflyers-sso'), $settings['auto_create'], __('Si decoche, seuls les membres deja associes a un compte WordPress peuvent se connecter.', 'openflyers-sso'));
self::roleRow('default_role', __('Role des comptes crees', 'openflyers-sso'), $settings['default_role']);
self::textRow('redirect_after_login', __('Page apres connexion', 'openflyers-sso'), $settings['redirect_after_login'], home_url('/espace-membres/'));
self::textRow('profile_report_id', __('Rapport des coordonnees des membres', 'openflyers-sso'), $settings['profile_report_id'] ? $settings['profile_report_id'] : '', '');
self::descriptionRow(__('Numero du rapport personnalise OpenFlyers qui renvoie le prenom, le nom et l\'e-mail du membre connecte (filtre sur $connectedPersonId). Ils sont alors mis a jour a chaque connexion. Laisser vide pour ne pas les recuperer.', 'openflyers-sso'));
self::checkboxRow('show_login_button', __('Bouton sur la page de connexion WordPress', 'openflyers-sso'), $settings['show_login_button'], '');
self::checkboxRow('debug', __('Journalisation', 'openflyers-sso'), $settings['debug'], __('Ecrit les erreurs dans le journal PHP (error_log).', 'openflyers-sso'));
?>

View File

@@ -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.
*