From de80b91544f8696e63f73a35388565625966cc1c Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 7 Jun 2023 00:08:10 -0500 Subject: [PATCH 1/2] Plugin: OAuth2: Allow to set values for user status from oauth2 response - refs BT#20784 --- plugin/oauth2/lang/english.php | 10 ++++++++ plugin/oauth2/src/OAuth2.php | 45 +++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/plugin/oauth2/lang/english.php b/plugin/oauth2/lang/english.php index 6d780b444e..9815d7be15 100644 --- a/plugin/oauth2/lang/english.php +++ b/plugin/oauth2/lang/english.php @@ -80,6 +80,16 @@ $strings['response_resource_owner_status_help'] = 'The value at this array key s
5
Student
6
Anonymous
'; +$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 Response Resource Owner status key, 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 Response Resource Owner status key, 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 Response Resource Owner status key, 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 Response Resource Owner status key, 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 Response Resource Owner status key, 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'; diff --git a/plugin/oauth2/src/OAuth2.php b/plugin/oauth2/src/OAuth2.php index fff9bbe53e..cbf250410e 100644 --- a/plugin/oauth2/src/OAuth2.php +++ b/plugin/oauth2/src/OAuth2.php @@ -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)) { From d59fcb484ec2f88cca4a0c11356c53058b5603b0 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 19 Jun 2023 17:44:34 -0500 Subject: [PATCH 2/2] Plugin: OAuth2: Fix function to set user status - refs BT#20784 --- plugin/oauth2/src/OAuth2.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/plugin/oauth2/src/OAuth2.php b/plugin/oauth2/src/OAuth2.php index cbf250410e..aebbaa09bb 100644 --- a/plugin/oauth2/src/OAuth2.php +++ b/plugin/oauth2/src/OAuth2.php @@ -382,7 +382,7 @@ class OAuth2 extends Plugin return $fieldValue->getCreatedAt() >= $lastLogin->getLoginDate(); } - private function mapUserStatusFromResponse(array $response, int $defaultStatus = STUDENT): bool + private function mapUserStatusFromResponse(array $response, int $defaultStatus = STUDENT): int { $status = $this->getValueByKey( $response, @@ -390,13 +390,29 @@ class OAuth2 extends Plugin $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), - ]); + $responseStatus = []; + + if ($teacherStatus = $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_TEACHER_STATUS)) { + $responseStatus[COURSEMANAGER] = $teacherStatus; + } + + if ($sessAdminStatus = $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_SESSADMIN_STATUS)) { + $responseStatus[SESSIONADMIN] = $sessAdminStatus; + } + + if ($drhStatus = $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_DRH_STATUS)) { + $responseStatus[DRH] = $drhStatus; + } + + if ($studentStatus = $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_STUDENT_STATUS)) { + $responseStatus[STUDENT] = $studentStatus; + } + + if ($anonStatus = $this->get(self::SETTING_RESPONSE_RESOURCE_OWNER_ANON_STATUS)) { + $responseStatus[ANONYMOUS] = $anonStatus; + } + + $map = array_flip($responseStatus); return $map[$status] ?? $status; }