Plugin: OAuth2: Allow to set values for user status from oauth2 response - refs BT#20784

pull/4739/head
Angel Fernando Quiroz Campos 2 years ago
parent 60c78cb7c7
commit de80b91544
  1. 10
      plugin/oauth2/lang/english.php
  2. 45
      plugin/oauth2/src/OAuth2.php

@ -80,6 +80,16 @@ $strings['response_resource_owner_status_help'] = 'The value at this array key s
<dt>5</dt><dd>Student</dd>
<dt>6</dt><dd>Anonymous</dd>
</dl>';
$strings['response_resource_owner_teacher_status'] = 'Response Resource Owner status value for Course Manager / Teacher';
$strings['response_resource_owner_teacher_status_help'] = 'If this value matches the value obtained from the <i>Response Resource Owner status key</i>, the user will have the role of Course Manager / Teacher';
$strings['response_resource_owner_sessadmin_status'] = 'Response Resource Owner status value for Session Administrator';
$strings['response_resource_owner_sessadmin_status_help'] = 'If this value matches the value obtained from the <i>Response Resource Owner status key</i>, the user will have the role of Session Administrator';
$strings['response_resource_owner_drh_status'] = 'Response Resource Owner status value for HRM';
$strings['response_resource_owner_drh_status_help'] = 'If this value matches the value obtained from the <i>Response Resource Owner status key</i>, the user will have the role of HRM';
$strings['response_resource_owner_student_status'] = 'Response Resource Owner status value for Student';
$strings['response_resource_owner_student_status_help'] = 'If this value matches the value obtained from the <i>Response Resource Owner status key</i>, the user will have the role of Student';
$strings['response_resource_owner_anon_status'] = 'Response Resource Owner status value for Anonymous';
$strings['response_resource_owner_anon_status_help'] = 'If this value matches the value obtained from the <i>Response Resource Owner status key</i>, the user will have the role of Anonymous';
$strings['response_resource_owner_email'] = 'Response Resource Owner email key';
$strings['response_resource_owner_username'] = 'Response Resource Owner username key';

@ -49,6 +49,11 @@ class OAuth2 extends Plugin
public const SETTING_RESPONSE_RESOURCE_OWNER_FIRSTNAME = 'response_resource_owner_firstname';
public const SETTING_RESPONSE_RESOURCE_OWNER_LASTNAME = 'response_resource_owner_lastname';
public const SETTING_RESPONSE_RESOURCE_OWNER_STATUS = 'response_resource_owner_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_TEACHER_STATUS = 'response_resource_owner_teacher_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_SESSADMIN_STATUS = 'response_resource_owner_sessadmin_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_DRH_STATUS = 'response_resource_owner_drh_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_STUDENT_STATUS = 'response_resource_owner_student_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_ANON_STATUS = 'response_resource_owner_anon_status';
public const SETTING_RESPONSE_RESOURCE_OWNER_EMAIL = 'response_resource_owner_email';
public const SETTING_RESPONSE_RESOURCE_OWNER_USERNAME = 'response_resource_owner_username';
@ -106,6 +111,11 @@ class OAuth2 extends Plugin
self::SETTING_RESPONSE_RESOURCE_OWNER_FIRSTNAME => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_LASTNAME => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_TEACHER_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_SESSADMIN_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_DRH_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_STUDENT_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_ANON_STATUS => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_EMAIL => 'text',
self::SETTING_RESPONSE_RESOURCE_OWNER_USERNAME => 'text',
@ -255,11 +265,7 @@ class OAuth2 extends Plugin
$this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_LASTNAME),
$this->get_lang('DefaultLastname')
);
$status = $this->getValueByKey(
$response,
$this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS),
STUDENT
);
$status = $this->mapUserStatusFromResponse($response);
$email = $this->getValueByKey(
$response,
$this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_EMAIL),
@ -376,6 +382,25 @@ class OAuth2 extends Plugin
return $fieldValue->getCreatedAt() >= $lastLogin->getLoginDate();
}
private function mapUserStatusFromResponse(array $response, int $defaultStatus = STUDENT): bool
{
$status = $this->getValueByKey(
$response,
$this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS),
$defaultStatus
);
$map = array_flip([
COURSEMANAGER => $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_TEACHER_STATUS),
SESSIONADMIN => $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_SESSADMIN_STATUS),
DRH => $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_DRH_STATUS),
STUDENT => $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_STUDENT_STATUS),
ANONYMOUS => $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_ANON_STATUS),
]);
return $map[$status] ?? $status;
}
/**
* Extends ArrayAccessorTrait::getValueByKey to return a list of values
* $key can contain wild card character *
@ -445,13 +470,11 @@ class OAuth2 extends Plugin
$user->getEmail()
)
);
$user->setStatus(
$this->getValueByKey(
$response,
$this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS),
$user->getStatus()
)
$status = $this->mapUserStatusFromResponse(
$response,
$user->getStatus()
);
$user->setStatus($status);
$user->setAuthSource('oauth2');
$configFilePath = __DIR__.'/../config.php';
if (file_exists($configFilePath)) {

Loading…
Cancel
Save