From a8a2b73b8b56b33c42dd1380b64a169de1cd2ba0 Mon Sep 17 00:00:00 2001 From: kerdorina <50053502+kerdorina@users.noreply.github.com> Date: Tue, 3 Aug 2021 11:53:51 +0200 Subject: [PATCH 1/4] =?UTF-8?q?OE-11162:=20Connect=20to=20CSD=20API,=20add?= =?UTF-8?q?=20lookup=20user=20button=20and=20refresh=20user=E2=80=A6=20(#2?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * OE-11162: Connect to CSD API, add lookup user button and refresh user after log in * OE-11162: Changes requested in code review * OE-11162: Changes requested in PR * OE-11162: Requests in PR Co-authored-by: kerdorina --- CSDClientModule.php | 46 +++ commands/UserCheckerCommand.php | 67 ----- commands/UserFixerCommand.php | 72 ----- commands/UserUpdateCommand.php | 73 ----- components/CSDClient/CSDClient.php | 61 ++++ components/CSDClient/GetUserDataRequest.php | 44 +++ components/CSDClient/Request.php | 121 ++++++++ components/CSDClient/RequestInterface.php | 28 ++ components/Singleton.php | 63 +++++ components/UserObserver.php | 264 +++++++++++++----- config/common.php | 2 + config/console.php | 2 + models/StaffDB_AgressoTable.php | 99 ------- models/StaffDB_User.php | 209 -------------- .../CSDClient/GetUserDataRequestTest.php | 45 +++ tests/unit/components/SingletonTest.php | 36 +++ tests/unit/components/UserObserverTest.php | 69 +++++ 17 files changed, 711 insertions(+), 590 deletions(-) create mode 100644 CSDClientModule.php delete mode 100644 commands/UserCheckerCommand.php delete mode 100644 commands/UserFixerCommand.php delete mode 100644 commands/UserUpdateCommand.php create mode 100644 components/CSDClient/CSDClient.php create mode 100644 components/CSDClient/GetUserDataRequest.php create mode 100644 components/CSDClient/Request.php create mode 100644 components/CSDClient/RequestInterface.php create mode 100644 components/Singleton.php delete mode 100644 models/StaffDB_AgressoTable.php delete mode 100644 models/StaffDB_User.php create mode 100644 tests/unit/components/CSDClient/GetUserDataRequestTest.php create mode 100644 tests/unit/components/SingletonTest.php create mode 100644 tests/unit/components/UserObserverTest.php diff --git a/CSDClientModule.php b/CSDClientModule.php new file mode 100644 index 0000000..8cb5b0f --- /dev/null +++ b/CSDClientModule.php @@ -0,0 +1,46 @@ +. + * + * @link http://www.openeyes.org.uk + * + * @author OpenEyes + * @copyright Copyright (c) 2011-2013, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\mehstaffdb; + +class CSDClientModule extends \BaseModule +{ + public function init() + { + // this method is called when the module is being created + // you may place code here to customize the module or the application + + // import the module-level models and components + $this->setImport(array( + 'mehstaffdb.components.*', + 'mehstaffdb.controllers.*', + )); + parent::init(); + } + + public function beforeControllerAction($controller, $action) + { + if (parent::beforeControllerAction($controller, $action)) { + // this method is called before any module controller action is performed + // you may place customized code here + return true; + } else { + return false; + } + } +} diff --git a/commands/UserCheckerCommand.php b/commands/UserCheckerCommand.php deleted file mode 100644 index 9298a86..0000000 --- a/commands/UserCheckerCommand.php +++ /dev/null @@ -1,67 +0,0 @@ -. - * - * @package OpenEyes - * @link http://www.openeyes.org.uk - * @author OpenEyes - * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust - * @copyright Copyright (c) 2011-2013, OpenEyes Foundation - * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 - */ - -/** - * Class CreateTherapyApplicationFileCollections - * - * command script to find PDF files in a nested directory structure and create appropriate file collections for use in - * the Therapy application module - * - */ -class UserCheckerCommand extends CConsoleCommand -{ - protected $attribute_comparison = array( - 'is_surgeon' => 'MUUID_Staff_IsSurgeon'); - - public function getName() - { - return 'UserChecker'; - } - - public function getHelp() - { - return $this->getName() . ":\n\n" . <<findAll(array('order' => 'username')) as $user) { - if ($staff_user = StaffDB_User::model()->find("MUUID_Staff_DomainUsername=?", array($user->username)) ) { - if (!$staff_user->MUUID_Staff_LeftMEH) { - foreach ($this->attribute_comparison as $oe_field => $staff_field) { - if ($user->$oe_field != $staff_user->$staff_field) { - echo $user->username . " field mismatch " . $oe_field . " oe:" . $user->$oe_field . ", staff:" . $staff_user->$staff_field . "\n"; - } - } - } - } - } - - } - -} diff --git a/commands/UserFixerCommand.php b/commands/UserFixerCommand.php deleted file mode 100644 index cc75c05..0000000 --- a/commands/UserFixerCommand.php +++ /dev/null @@ -1,72 +0,0 @@ -. - * - * @package OpenEyes - * @link http://www.openeyes.org.uk - * @author OpenEyes - * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust - * @copyright Copyright (c) 2011-2013, OpenEyes Foundation - * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 - */ - -/** - * Class CreateTherapyApplicationFileCollections - * - * command script to find PDF files in a nested directory structure and create appropriate file collections for use in - * the Therapy application module - * - */ -class UserFixerCommand extends CConsoleCommand -{ - protected $attribute_comparison = array( - 'is_surgeon' => 'MUUID_Staff_IsSurgeon'); - - public function getName() - { - return 'UserFixer'; - } - - public function getHelp() - { - return $this->getName() . ":\n\n" . <<findAll(array('order' => 'username')) as $user) { - if ($staff_user = StaffDB_User::model()->find("MUUID_Staff_DomainUsername=?", array($user->username)) ) { - if (!$staff_user->MUUID_Staff_LeftMEH) { - foreach ($this->attribute_comparison as $oe_field => $staff_field) { - if ($user->$oe_field != $staff_user->$staff_field) { - echo $user->username . " has field mismatch " . $oe_field . " oe:" . $user->$oe_field . ", staff:" . $staff_user->$staff_field . ". Fixing..."; - $user->$oe_field = $staff_user->$staff_field; - if(!$user->save()) { - throw new CException('Could not save user'); - } - echo "done\n"; - } - } - } - } - } - - } - -} diff --git a/commands/UserUpdateCommand.php b/commands/UserUpdateCommand.php deleted file mode 100644 index 1a251f9..0000000 --- a/commands/UserUpdateCommand.php +++ /dev/null @@ -1,73 +0,0 @@ -. - * - * @package OpenEyes - * @link http://www.openeyes.org.uk - * @author OpenEyes - * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust - * @copyright Copyright (c) 2011-2013, OpenEyes Foundation - * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 - */ - -/** - * Class CreateTherapyApplicationFileCollections - * - * command script to find PDF files in a nested directory structure and create appropriate file collections for use in - * the Therapy application module - * - */ -class UserUpdateCommand extends CConsoleCommand -{ - public function getName() - { - return 'UserUpdate'; - } - - public function getHelp() - { - return $this->getName() . ":\n\n" . <<php yiic.php userupdate all - -This will update all users in OE those in the MEH Staff Database - -Type 'yes' or 'no'. - -Are you sure ?: [no] -******************************************************************* - -EOH; - } - - /** - * Update all users from the staff db - */ - public function actionAll() - { - echo "\nThis will update all users in OE those in the MEH Staff Database\n"; - echo "\nType 'yes' or 'no'.\n\n"; - $confirmed = $this->prompt('Are you sure ?:','no'); - - if( $confirmed === 'yes' ){ - $observer = new UserObserver(); - - $users = User::model()->findAll(array('order' => 'username')); - foreach ($users as $user) { - $params['username'] = $user->username; - $observer->updateUser($params); - } - } - } -} \ No newline at end of file diff --git a/components/CSDClient/CSDClient.php b/components/CSDClient/CSDClient.php new file mode 100644 index 0000000..93ed490 --- /dev/null +++ b/components/CSDClient/CSDClient.php @@ -0,0 +1,61 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\mehstaffdb\components\CSDClient; + +use OEModule\mehstaffdb\components\CSDClient\GetUserDataRequest; +use OEModule\mehstaffdb\components\Singleton; + +/** + * Class CSDClient + * + * This is a facade for all CSD API calls. Other classes should only + * call methods of this class. + * + * @method static CSDClient get() + */ + +class CSDClient extends Singleton +{ + + /** + * Sends request + * + * @param Request $request + * @return string|null JSON-encoded data on success or null on failure + */ + private function sendRequest(Request $request): ?string + { + $response = $request->submit(); + if(!$response["success"]) { + return null; + } + + return $response["success"] > 0 && isset($response["result"]) ? json_encode($response["result"]) : null; + } + + /** + * Retrieves patient data from CSD + * + * @param string $username + * @return string|null JSON-encoded data on success or null on failure + */ + public function getUserData(string $username): ?string + { + return $this->sendRequest((new GetUserDataRequest($this))->setUsername($username)); + } +} \ No newline at end of file diff --git a/components/CSDClient/GetUserDataRequest.php b/components/CSDClient/GetUserDataRequest.php new file mode 100644 index 0000000..90ddbe6 --- /dev/null +++ b/components/CSDClient/GetUserDataRequest.php @@ -0,0 +1,44 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\mehstaffdb\components\CSDClient; + +use OEModule\mehstaffdb\components\CSDClient\Request; + +class GetUserDataRequest extends Request +{ + /** @var string */ + protected $username; + + /** + * @param string $username + * @return GetUserDataRequest + */ + public function setUsername(string $username): GetUserDataRequest + { + $this->username = $username; + return $this; + } + + /** + * @inheritDoc + */ + public function getActionName(): string + { + return "CSDAPI/api/staff?DomainUsername=".$this->username; + } +} \ No newline at end of file diff --git a/components/CSDClient/Request.php b/components/CSDClient/Request.php new file mode 100644 index 0000000..f7f5298 --- /dev/null +++ b/components/CSDClient/Request.php @@ -0,0 +1,121 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust + * @copyright Copyright (c) 2011-2012, OpenEyes Foundation + * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 + */ + +namespace OEModule\mehstaffdb\components\CSDClient; + +abstract class Request implements RequestInterface +{ + /** @var string */ + protected string $csd_api_key; + /** @var string */ + protected string $csd_api_url; + + /** + * Child classes must extend this to be able + * to automatically configure the class instance + * + * @return array + */ + protected function getSettingKeys(): array + { + return [ + "csd_api_key", + "csd_api_url", + "csd_api_timeout" + ]; + } + + public function __construct() + { + foreach ($this->getSettingKeys() as $setting_key) { + $setting_value = \Yii::app()->params[$setting_key]; + if (strlen($setting_value) === 0) { + throw new \Exception("$setting_key not set"); + } + $this->$setting_key = $setting_value; + } + } + + /** + * @inheritDoc + */ + public function getTimeout(): int + { + return $this->csd_api_timeout; + } + + /** + * HTTP headers sent along with the request + * + * @return string[] + */ + protected function getRequestHeader(): array + { + return [ + 'Content-type: application/json', + 'APIKey: ' . $this->csd_api_key, + ]; + } + + /** + * @inheritDoc + */ + public function submit() + { + $base_url = $this->csd_api_url; + $url = $base_url."/".$this->getActionName(); + $ch = curl_init($url); + + if(!empty($header = $this->getRequestHeader())) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); + } + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FAILONERROR, true); + curl_setopt($ch, CURLOPT_TIMEOUT, $this->getTimeout()); + curl_setopt($ch, CURLOPT_VERBOSE, false); + + $result = curl_exec($ch); + $err = curl_errno($ch); + + if($err > 0) { + \Yii::log("CSD Client error: ". $err, \CLogger::LEVEL_ERROR); + return [ + 'success' => 0, + 'message' => "CSD Client error: ". $err + ]; + } + + $result_array = json_decode($result, true); + + $success = 1; + if($result_array==null) { + $success = 0; + \Yii::log("CSD Client error: User not found", \CLogger::LEVEL_ERROR); + } else if(!array_key_exists("code", $result_array) || !array_key_exists("username", $result_array)) { + $success = 0; + \Yii::log("CSD Client error: ".$result_array["message"], \CLogger::LEVEL_ERROR); + } + + return [ + 'success' => $success, + 'result' => $result_array + ]; + } +} \ No newline at end of file diff --git a/components/CSDClient/RequestInterface.php b/components/CSDClient/RequestInterface.php new file mode 100644 index 0000000..f3fd388 --- /dev/null +++ b/components/CSDClient/RequestInterface.php @@ -0,0 +1,28 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\mehstaffdb\components\CSDClient; + +interface RequestInterface +{ + /** Action name to be appended to the request URI */ + public function getActionName() : string; + /** Number of seconds before the request times out */ + public function getTimeout() : int; + /** Submit the request */ + public function submit(); +} \ No newline at end of file diff --git a/components/Singleton.php b/components/Singleton.php new file mode 100644 index 0000000..982a82f --- /dev/null +++ b/components/Singleton.php @@ -0,0 +1,63 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\mehstaffdb\components; + +/** + * Class Singleton + * + * Base for singleton classes + */ + +abstract class Singleton +{ + /** + * @var Singleton[] + */ + protected static $instances; + + /** + * Constructor + * + * @return void + */ + private function __construct() + { + $this->init(); + } + + /** + * Extend and put initialization code here + * instead of the constructor + */ + protected function init() {} + + /** + * Get instance + * + * @return Singleton + */ + public final static function get() : Singleton + { + $class = get_called_class(); + if (!isset(self::$instances[$class]) || is_null(self::$instances[$class])) { + self::$instances[$class] = new static(); + } + + return self::$instances[$class]; + } +} \ No newline at end of file diff --git a/components/UserObserver.php b/components/UserObserver.php index f591ace..75a1531 100644 --- a/components/UserObserver.php +++ b/components/UserObserver.php @@ -17,112 +17,238 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 */ -class UserObserver +//namespace OEModule\CSDClient\components; + +use OEModule\mehstaffdb\components\CSDClient\CSDClient; + +class UserObserver extends \BaseAPI { + + /** + * @return CSDClient + */ + protected function getCSDClient(): CSDClient + { + return CSDClient::get(); + } + + + /** + * Updates user from CSD database + * + * @param array $params + * @return User + */ public function updateUser($params) { if (in_array($params['username'],Yii::app()->params['local_users'])) { return; } - if (Yii::app()->params['mehstaffdb_always_refresh'] || $this->is_stale($params['username'])) { + if (Yii::app()->params['mehstaffdb_always_refresh'] || $this->isStale($params['username'], $params['institution_authentication_id'])) { try { - if ($remote_user = StaffDB_User::model()->find("MUUID_Staff_DomainUsername=?", array($params['username']))) { - if (!$user = User::model()->find('username=?',array($params['username']))) { - $user = new User; + $username = $params['username']; + $institution_authentication_id = $params['institution_authentication_id']; + $remote_user = $this->getCSDClient()->getUserData($username); + if ($remote_user = $this->getCSDClient()->getUserData($username)) { + $remote_user = json_decode($remote_user, true); + + $user = $this->getUser($username, $institution_authentication_id); + $user_authentication = $this->getUserAuthentication($username, $institution_authentication_id); + + if (!$user) { + $user = new User(); $preexists = false; } else { $preexists = true; } - $user->code = $remote_user->MUUID_Staff_MUUID; - $user->username = $remote_user->MUUID_Staff_DomainUsername; - $user->first_name = $remote_user->MUUID_Staff_NameFirst; - $user->last_name = $remote_user->MUUID_Staff_NameLast; - $user->email = Yii::app()->params['mehstaffdb_default_email']; - $user->title = $remote_user->MUUID_Staff_Title; - $user->qualifications = $remote_user->EPR_MedicalDegrees; - $user->role = $remote_user->MUUID_Staff_JobTitle; - $user->doctor_grade_id = $this->getDoctorGradeFromJobTitle($remote_user->MUUID_Staff_JobTitle); - $user->registration_code = $this->getGMCRegistrationNumber($remote_user->MUUID_Staff_PersonnelID); - $user->password = 'faed6633f5a86241f3e0c2bb2bb768fd'; - $user->is_doctor = $remote_user->MUUID_Staff_IsDoctor; - $user->is_clinical = $remote_user->MUUID_Staff_IsClinical; - $user->is_consultant = $remote_user->MUUID_Staff_IsConsultant; - $user->is_surgeon = $remote_user->MUUID_Staff_IsSurgeon; - $user->active = !$remote_user->MUUID_Staff_LeftMEH; - $user->global_firm_rights = 1; - - if (!$user->save(false)) { - throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); - } - + $user = $this->saveUser($user, $user_authentication, $remote_user); + if (!$preexists) { - $contact = new Contact; + $contact = new Contact(); } else { if ($user->contact) { $contact = $user->contact; } else { - $contact = new Contact; + $contact = new Contact(); } } - $contact->nick_name = $user->first_name; - $contact->title = $user->title; - $contact->first_name = $user->first_name; - $contact->last_name = $user->last_name; - $contact->qualifications = $user->qualifications; + $contact = $this->saveContact($user, $contact); - if (!$contact->save()) { - throw new Exception('Unable to save contact: '.print_r($contact->getErrors(),true)); - } - - if ($user->contact_id != $contact->id) { + if ($user->contact_id !== $contact->id) { $user->contact_id = $contact->id; if (!$user->save()) { + \Yii::log("Unable to save user contact: ".print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); throw new Exception("Unable to save user contact: ".print_r($user->getErrors(),true)); } } + return $user; + } else { + \Yii::log("User " . $username . " not found in the CSD database.", \CLogger::LEVEL_ERROR); + throw new Exception("Unable to save user contact: ".$username); } } catch (Exception $e) { - // silently return back to UserIdentity without having refreshed the user + \Yii::log("Unable to update user. Error: ". $e->getMessage(), \CLogger::LEVEL_ERROR); + throw new Exception("Unable to save user contact: ".print_r($user->getErrors(),true)); } } } - public function is_stale($username) + + /** + * Finds User by username + * + * @param string $username + * @return User + */ + private function getUser(string $username, int $institution_authentication_id): User + { + $criteria = new \CDbCriteria(); + $criteria->join = 'JOIN user_authentication ua ON t.id = ua.user_id'; + $criteria->addCondition('ua.username = :username'); + $criteria->params[':username'] = $username; + $criteria->addCondition('ua.institution_authentication_id = :institution_authentication_id'); + $criteria->params[':institution_authentication_id'] = $institution_authentication_id; + $user = \User::model()->find($criteria); + return $user; + } + + /** + * Finds UserAuthentication by username + * + * @param string $username + * @return UserAuthentication + */ + private function getUserAuthentication(string $username, int $institution_authentication_id): UserAuthentication + { + $criteria = new \CDbCriteria(); + $criteria->join = 'JOIN institution_authentication ia ON t.institution_authentication_id = ia.id'; + $criteria->addCondition('username = :username'); + $criteria->params[':username'] = $username; + $criteria->addCondition('institution_authentication_id = :institution_authentication_id'); + $criteria->params[':institution_authentication_id'] = $institution_authentication_id; + $user_authentication = \UserAuthentication::model()->find($criteria); + return $user_authentication; + } + + /** + * Saves new User data which is coming from $remote_user + * + * @param User $user + * @param UserAuthentication $user_authentication + * @param array $remote_user + * @return User + */ + private function saveUser(User $user, UserAuthentication $user_authentication, array $remote_user): User + { + $user->code = $remote_user['code']; + $user_authentication->username = $remote_user['username']; + $user->first_name = $remote_user['first_name']; + $user->last_name = $remote_user['last_name']; + $mehstaffdb_default_email = Yii::app()->params['mehstaffdb_default_email']; + if(strlen($mehstaffdb_default_email) != 0) { + $user->email = $mehstaffdb_default_email; + } + $user->title = $remote_user['title']; + $user->qualifications = $remote_user['qualifications']; + $user->role = $remote_user['role']; + $user->doctor_grade_id = $this->getDoctorGradeFromJobTitle($remote_user['role']); + if(isset($remote_user['registration_code']) && isset($remote_user['registration_code'][0]['ProfessionalRegistration'])) { + $user->registration_code = $this->getGMCRegistrationNumber($remote_user['registration_code'][0]['ProfessionalRegistration']); + } + $user->is_consultant = $remote_user['is_consultant']; + $user->is_surgeon = $remote_user['is_surgeon']; + $user_authentication->active = !$remote_user['active']; + $user->global_firm_rights = 1; + + if (!$user->save()) { + \Yii::log('Unable to save user: '.print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); + throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); + } + + if (!$user_authentication->save()) { + \Yii::log('Unable to save user: '.print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); + throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); + } + + return $user; + } + + /** + * Saves new User data which is coming from $remote_user + * + * @param User $user + * @param Contact $contact + * @return Contact + * + */ + private function saveContact(User $user, Contact $contact): Contact { - if (!$user = User::model()->find('username=?',array($username))) { + $contact->nick_name = $user->first_name; + $contact->title = $user->title; + $contact->first_name = $user->first_name; + $contact->last_name = $user->last_name; + $contact->qualifications = $user->qualifications; + + if (!$contact->save()) { + \Yii::log('Unable to save contact: '.print_r($contact->getErrors(),true), \CLogger::LEVEL_ERROR); + throw new Exception('Unable to save contact: '.print_r($contact->getErrors(),true)); + } + + return $contact; + } + + /** + * Check if user is stale + * + * @param string $username + * @return bool + */ + private function isStale(string $username, int $institution_authentication_id): bool + { + $user = $this->getUser($username, $institution_authentication_id); + + if (!$user) { return true; } return (strtotime($user->last_modified_date) < (time() - Yii::app()->params['mehstaffdb_cache_time'])); } - private function getDoctorGradeFromJobTitle($jobTitle){ - $MEHDescription = array( - "1" => "Consultant", - "3" => "Associate Specialist", - "4" => "Fellow", - "5" => "Specialist Registrar", - "7" => "Trust Doctor", - "8" => "Senior House Officer", - "16" => "House Officer", - "20" => "Anaesthetist", - "21" => "Orthoptist", - "22" => "Optometrist", - "23" => "Clinical nurse specialist", - "24" => "Nurse", - "25" => "Health Care Assistant", - "26" => "Ophthalmic Technician", - "27" => "Surgical Care Practitioner", - "28" => "Clinical Assistant", - "29" => "RG1", - "30" => "RG2", - "31" => "ODP", - "32" => "Administration staff"); - //"33" => "Other" + /** + * Connect job title to doctor grade id + * + * @param string $jobTitle + * @return int ID of the doctor grade + */ + private function getDoctorGradeFromJobTitle(string $jobTitle): int + { + $MEHDescription = array + ( + 1 => "Consultant", + 3 => "Associate Specialist", + 4 => "Fellow", + 5 => "Specialist Registrar", + 7 => "Trust Doctor", + 8 => "Senior House Officer", + 16 => "House Officer", + 20 => "Anaesthetist", + 21 => "Orthoptist", + 22 => "Optometrist", + 23 => "Clinical nurse specialist", + 24 => "Nurse", + 25 => "Health Care Assistant", + 26 => "Ophthalmic Technician", + 27 => "Surgical Care Practitioner", + 28 => "Clinical Assistant", + 29 => "RG1", + 30 => "RG2", + 31 => "ODP", + 32 => "Administration staff" + ); foreach($MEHDescription as $key=>$description){ if(strpos($jobTitle, $description) !== false){ @@ -132,11 +258,9 @@ private function getDoctorGradeFromJobTitle($jobTitle){ return 33; // default value is Other } - private function getGMCRegistrationNumber($personnelID){ - $StaffData = StaffDB_AgressoTable::model()->find("PersonnelID=?", array($personnelID)); - if($StaffData) { - $GMCData = $StaffData->ProfessionalRegistration; - $GMC = explode(" - ", $GMCData); + private function getGMCRegistrationNumber($professional_registration){ + if($professional_registration) { + $GMC = explode(" - ", $professional_registration); if (is_array($GMC) && count($GMC) > 0) { return $GMC[1]; } else { diff --git a/config/common.php b/config/common.php index 62e2b40..37c76be 100644 --- a/config/common.php +++ b/config/common.php @@ -17,6 +17,8 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 */ +namespace OEModule\mehstaffdb\config; + return array( 'import' => array( 'application.modules.mehstaffdb.*', diff --git a/config/console.php b/config/console.php index 59e6c07..b8f84b9 100644 --- a/config/console.php +++ b/config/console.php @@ -17,6 +17,8 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 */ +namespace OEModule\mehstaffdb\config; + $config = array(); $dh = opendir(dirname(__FILE__)."/../commands"); diff --git a/models/StaffDB_AgressoTable.php b/models/StaffDB_AgressoTable.php deleted file mode 100644 index 1f802b5..0000000 --- a/models/StaffDB_AgressoTable.php +++ /dev/null @@ -1,99 +0,0 @@ -. - * - * @package OpenEyes - * @link http://www.openeyes.org.uk - * @author OpenEyes - * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust - * @copyright Copyright (c) 2011-2012, OpenEyes Foundation - * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 - */ - -/** - * This is the model class for table "MUUID_Staff_Table". - * - * The followings are the available columns in table 'MUUID_Staff_Table': - * @property integer PersonnelID - * @property string ProfessionalRegistration - * - **/ - -class StaffDB_AgressoTable extends MultiActiveRecord -{ - /** - * Returns the static model of the specified AR class. - * @return StaffDB_User the static model class - */ - public static function model($className=__CLASS__) - { - return parent::model($className); - } - - /** - * @return string the associated db connection name - */ - public function connectionId() - { - return 'db_staff'; - } - - /** - * @return string the associated database table name - */ - public function tableName() - { - return 'MUUID_AgressoStaff_Table'; - } - /** - * @return array validation rules for model attributes. - */ - public function rules() - { - return array( - ); - } - - /** - * @return array relational rules. - */ - public function relations() - { - return array( - ); - } - - /** - * @return array customized attribute labels (name=>label) - */ - public function attributeLabels() - { - return array( - ); - } - - /** - * Retrieves a list of models based on the current search/filter conditions. - * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. - */ - public function search() - { - $criteria = new CDbCriteria; - - $criteria->compare('PersonnelID', $this->PersonnelID, true); - $criteria->compare('ProfessionalRegistration', $this->ProfessionalRegistration, true); - - return new CActiveDataProvider(get_class($this), array( - 'criteria'=>$criteria, - )); - } - -} \ No newline at end of file diff --git a/models/StaffDB_User.php b/models/StaffDB_User.php deleted file mode 100644 index b40d77c..0000000 --- a/models/StaffDB_User.php +++ /dev/null @@ -1,209 +0,0 @@ -. - * - * @package OpenEyes - * @link http://www.openeyes.org.uk - * @author OpenEyes - * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust - * @copyright Copyright (c) 2011-2012, OpenEyes Foundation - * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0 - */ - -/** - * This is the model class for table "MUUID_Staff_Table". - * - * The followings are the available columns in table 'MUUID_Staff_Table': - * @property integer MUUID_Staff_UniqueID - * @property string MUUID_Staff_MUUID - * @property string MUUID_Staff_Title - * @property string MUUID_Staff_NameFirst - * @property string MUUID_Staff_NameMiddle - * @property string MUUID_Staff_NameLast - * @property string MUUID_Staff_KnownAs_NameFirst - * @property string MUUID_Staff_Gender - * @property string MUUID_Staff_JobTitle - * @property integer MUUID_Staff_NHSOrganisationNameID - * @property integer MUUID_Staff_MoorfieldsSiteID - * @property integer MUUID_Staff_DepartmentSduID - * @property integer MUUID_Staff_DepartmentID - * @property string MUUID_Staff_DepartmentSub - * @property string MUUID_Staff_Location - * @property string MUUID_Staff_Comments - * @property string MUUID_Staff_Home_Phone - * @property string MUUID_Staff_Mobile_Phone - * @property string MUUID_Staff_Internal_Phone - * @property string MUUID_Staff_Internal_Bleep - * @property string MUUID_Staff_PPsecretary_Phone - * @property string MUUID_Staff_NHSsecretary_Phone - * @property string MUUID_Staff_Clerk1_Phone - * @property string MUUID_Staff_Clerk2_Phone - * @property boolean MUUID_Staff_IsVoiceMailRequired - * @property string MUUID_Staff_Notes_Phone - * @property string MUUID_Staff_MyBossMUUID - * @property string MUUID_Staff_MyAppraiserMUUID - * @property string MUUID_Staff_MyCoordinatorMUUID - * @property string MUUID_Staff_EmailAddress - * @property string MUUID_Staff_EmailAddressDelegate - * @property string MUUID_Staff_DomainUsername - * @property integer MUUID_Staff_EthnicityID - * @property string MUUID_Staff_EmployeeID - * @property date MUUID_Staff_DateOfStarting - * @property boolean MUUID_Staff_LeftMEH - * @property date MUUID_Staff_DateOfLeaving - * @property string MUUID_Staff_PersonnelID - * @property string MUUID_Staff_PhotoCardID - * @property boolean MUUID_Staff_IsPhotoCardIDDisplayPermitted - * @property integer MUUID_Staff_GMCReferenceNumber - * @property string MUUID_Staff_CreatedBy - * @property date MUUID_Staff_CreatedDate - * @property string EPR_MedicalDegrees - * @property string EPR_JobType - * @property string EPR_JobDescription - * @property string EPR_LetterSignoff - * @property integer EPR_MedicalGrade - * @property integer EPR_Service_CodeID - * @property integer EPR_Firm_CodeID - * @property string EPR_ConsultantNameText - * @property string EPR_ConsultantCode - * @property integer EPR_DefaultWardID - * @property integer EPR_RoleID - */ - -class StaffDB_User extends MultiActiveRecord -{ - /** - * Returns the static model of the specified AR class. - * @return StaffDB_User the static model class - */ - public static function model($className=__CLASS__) - { - return parent::model($className); - } - - /** - * @return string the associated db connection name - */ - public function connectionId() - { - return 'db_staff'; - } - - /** - * @return string the associated database table name - */ - public function tableName() - { - return 'MUUID_Staff_Table'; - } - - /** - * @return array primary key for the table - */ - public function primaryKey() - { - return array('MUUID_Staff_UniqueID'); - } - - /** - * @return array validation rules for model attributes. - */ - public function rules() - { - return array( - ); - } - - /** - * @return array relational rules. - */ - public function relations() - { - return array( - ); - } - - /** - * @return array customized attribute labels (name=>label) - */ - public function attributeLabels() - { - return array( - ); - } - - /** - * Retrieves a list of models based on the current search/filter conditions. - * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. - */ - public function search() - { - $criteria=new CDbCriteria; - - $criteria->compare('MUUID_Staff_UniqueID',$this->MUUID_Staff_UniqueID,true); - $criteria->compare('MUUID_Staff_MUUID',$this->MUUID_Staff_MUUID,true); - $criteria->compare('MUUID_Staff_Title',$this->MUUID_Staff_Title,true); - $criteria->compare('MUUID_Staff_NameFirst',$this->MUUID_Staff_NameFirst,true); - $criteria->compare('MUUID_Staff_NameMiddle',$this->MUUID_Staff_NameMiddle,true); - $criteria->compare('MUUID_Staff_NameLast',$this->MUUID_Staff_NameLast,true); - $criteria->compare('MUUID_Staff_KnownAs_NameFirst',$this->MUUID_Staff_KnownAs_NameFirst,true); - $criteria->compare('MUUID_Staff_Gender',$this->MUUID_Staff_Gender,true); - $criteria->compare('MUUID_Staff_JobTitle',$this->MUUID_Staff_JobTitle,true); - $criteria->compare('MUUID_Staff_NHSOrganisationNameID',$this->MUUID_Staff_NHSOrganisationNameID,true); - $criteria->compare('MUUID_Staff_MoorfieldsSiteID',$this->MUUID_Staff_MoorfieldsSiteID,true); - $criteria->compare('MUUID_Staff_DepartmentSduID',$this->MUUID_Staff_DepartmentSduID,true); - $criteria->compare('MUUID_Staff_DepartmentID',$this->MUUID_Staff_DepartmentID,true); - $criteria->compare('MUUID_Staff_DepartmentSub',$this->MUUID_Staff_DepartmentSub,true); - $criteria->compare('MUUID_Staff_Location',$this->MUUID_Staff_Location,true); - $criteria->compare('MUUID_Staff_Comments',$this->MUUID_Staff_Comments,true); - $criteria->compare('MUUID_Staff_Home_Phone',$this->MUUID_Staff_Home_Phone,true); - $criteria->compare('MUUID_Staff_Mobile_Phone',$this->MUUID_Staff_Mobile_Phone,true); - $criteria->compare('MUUID_Staff_Internal_Phone',$this->MUUID_Staff_Internal_Phone,true); - $criteria->compare('MUUID_Staff_Internal_Bleep',$this->MUUID_Staff_Internal_Bleep,true); - $criteria->compare('MUUID_Staff_PPsecretary_Phone',$this->MUUID_Staff_PPsecretary_Phone,true); - $criteria->compare('MUUID_Staff_NHSsecretary_Phone',$this->MUUID_Staff_NHSsecretary_Phone,true); - $criteria->compare('MUUID_Staff_Clerk1_Phone',$this->MUUID_Staff_Clerk1_Phone,true); - $criteria->compare('MUUID_Staff_Clerk2_Phone',$this->MUUID_Staff_Clerk2_Phone,true); - $criteria->compare('MUUID_Staff_IsVoiceMailRequired',$this->MUUID_Staff_IsVoiceMailRequired,true); - $criteria->compare('MUUID_Staff_Notes_Phone',$this->MUUID_Staff_Notes_Phone,true); - $criteria->compare('MUUID_Staff_MyBossMUUID',$this->MUUID_Staff_MyBossMUUID,true); - $criteria->compare('MUUID_Staff_MyAppraiserMUUID',$this->MUUID_Staff_MyAppraiserMUUID,true); - $criteria->compare('MUUID_Staff_MyCoordinatorMUUID',$this->MUUID_Staff_MyCoordinatorMUUID,true); - $criteria->compare('MUUID_Staff_EmailAddress',$this->MUUID_Staff_EmailAddress,true); - $criteria->compare('MUUID_Staff_EmailAddressDelegate',$this->MUUID_Staff_EmailAddressDelegate,true); - $criteria->compare('MUUID_Staff_DomainUsername',$this->MUUID_Staff_DomainUsername,true); - $criteria->compare('MUUID_Staff_EthnicityID',$this->MUUID_Staff_EthnicityID,true); - $criteria->compare('MUUID_Staff_EmployeeID',$this->MUUID_Staff_EmployeeID,true); - $criteria->compare('MUUID_Staff_DateOfStarting',$this->MUUID_Staff_DateOfStarting,true); - $criteria->compare('MUUID_Staff_LeftMEH',$this->MUUID_Staff_LeftMEH,true); - $criteria->compare('MUUID_Staff_DateOfLeaving',$this->MUUID_Staff_DateOfLeaving,true); - $criteria->compare('MUUID_Staff_PersonnelID',$this->MUUID_Staff_PersonnelID,true); - $criteria->compare('MUUID_Staff_PhotoCardID',$this->MUUID_Staff_PhotoCardID,true); - $criteria->compare('MUUID_Staff_IsPhotoCardIDDisplayPermitted',$this->MUUID_Staff_IsPhotoCardIDDisplayPermitted,true); - $criteria->compare('MUUID_Staff_GMCReferenceNumber',$this->MUUID_Staff_GMCReferenceNumber,true); - $criteria->compare('MUUID_Staff_CreatedBy',$this->MUUID_Staff_CreatedBy,true); - $criteria->compare('MUUID_Staff_CreatedDate',$this->MUUID_Staff_CreatedDate,true); - $criteria->compare('EPR_MedicalDegrees',$this->EPR_MedicalDegrees,true); - $criteria->compare('EPR_JobType',$this->EPR_JobType,true); - $criteria->compare('EPR_JobDescription',$this->EPR_JobDescription,true); - $criteria->compare('EPR_LetterSignoff',$this->EPR_LetterSignoff,true); - $criteria->compare('EPR_MedicalGrade',$this->EPR_MedicalGrade,true); - $criteria->compare('EPR_Service_CodeID',$this->EPR_Service_CodeID,true); - $criteria->compare('EPR_Firm_CodeID',$this->EPR_Firm_CodeID,true); - $criteria->compare('EPR_ConsultantNameText',$this->EPR_ConsultantNameText,true); - $criteria->compare('EPR_ConsultantCode',$this->EPR_ConsultantCode,true); - $criteria->compare('EPR_DefaultWardID',$this->EPR_DefaultWardID,true); - $criteria->compare('EPR_RoleID',$this->EPR_RoleID,true); - - return new CActiveDataProvider(get_class($this), array( - 'criteria'=>$criteria, - )); - } -} diff --git a/tests/unit/components/CSDClient/GetUserDataRequestTest.php b/tests/unit/components/CSDClient/GetUserDataRequestTest.php new file mode 100644 index 0000000..be1e224 --- /dev/null +++ b/tests/unit/components/CSDClient/GetUserDataRequestTest.php @@ -0,0 +1,45 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\CSDClient\tests\unit\components\CSDClient; + +class GetUserDataRequestTest extends \CTestCase +{ + /** + * @return GetUserDataRequest + */ + private function getInstance() + { + //\Yii::app()->params["pre_assessment_api_base_url"] = "http://example.com"; + return new GetUserDataRequest(CSDClient::get()); + } + + /** @test */ + public function testGetActionName() + { + $this->assertEquals("CSDAPI/api/staff?DomainUsername=WILLIAMSS", + $this->getInstance() + ->setUsername("WILLIAMSS") + ->getActionName()); + } + + /** @test */ + public function testGetTimeout() + { + $this->assertIsNumeric($this->getInstance()->getTimeout()); + } +} \ No newline at end of file diff --git a/tests/unit/components/SingletonTest.php b/tests/unit/components/SingletonTest.php new file mode 100644 index 0000000..560186e --- /dev/null +++ b/tests/unit/components/SingletonTest.php @@ -0,0 +1,36 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\CSDClient\tests\unit\components; + +use OEModule\CSDClient\components\Singleton; + +class SingletonTest extends \CTestCase +{ + public function testGet() + { + $stub1 = $this->getMockClass(Singleton::class, ["get"], [], "class1"); + $stub2 = $this->getMockClass(Singleton::class, ["get"], [], "class2"); + $obj1 = $stub1::get(); + $obj2 = $stub2::get(); + $this->assertNotSame($obj1, $obj2); + $this->assertInstanceOf($stub1, $obj1); + $this->assertInstanceOf($stub2, $obj2); + $obj3 = $stub1::get(); + $this->assertSame($obj1, $obj3); + } +} \ No newline at end of file diff --git a/tests/unit/components/UserObserverTest.php b/tests/unit/components/UserObserverTest.php new file mode 100644 index 0000000..7c670ab --- /dev/null +++ b/tests/unit/components/UserObserverTest.php @@ -0,0 +1,69 @@ +. + * + * @package OpenEyes + * @link http://www.openeyes.org.uk + * @author OpenEyes + * @copyright Copyright (c) 2021, OpenEyes Foundation + * @license http://www.gnu.org/licenses/agpl-3.0.html The GNU Affero General Public License V3.0 + */ + +namespace OEModule\CSDClient\tests\unit\components; + +use OEModule\CSDClient\components\UserObserver; +use OEModule\CSDClient\components\CSDClient\CSDClient; + +class UserObserverTest extends \OEDbTestCase +{ + public $fixtures = [ + "user" => \User::class, + ]; + + /** @var \CDbTransaction */ + private $transaction; + /** @var CSD */ + private $api; + + /*public function testClassCanBeFound() + { + $api = \Yii::app()->moduleAPI->get("CSDClient"); + $this->assertInstanceOf(UserObserver::class, $api); + }*/ + + public function setUp() + { + $this->transaction = \Yii::app()->db->beginTransaction(); + parent::setUp(); + } + + public function tearDown() + { + parent::tearDown(); + $this->transaction->rollback(); + } + + + /** @test */ + public function testUpdateUser() + { + $this->api = \Yii::app()->moduleAPI->get("CSDClient"); + $this->api = $this->getMockBuilder(UserObserver::class) + ->setMethods(["getCSDClient"]) + ->getMock(); + $mockCSDClient = $this->getMockBuilder(CSDClient::class) + ->disableOriginalConstructor() + ->getMock(); + $mockCSDClient->method("getUserData")->willReturn(json_encode(["hello" => "world"])); + $this->api->method("getCSDClient")->willReturn($mockCSDClient); + + //$mock_user = $this->user("user1"); + //$this->assertEquals($this->api->updateUser($mock_user), $mock_user); + } +} \ No newline at end of file From 2858abcb127887bdc4e90d47208e36f2203345e2 Mon Sep 17 00:00:00 2001 From: gabordikan <77804349+gabordikan@users.noreply.github.com> Date: Tue, 17 Aug 2021 01:51:35 +0200 Subject: [PATCH 2/4] OE-11663: add commands directory (#21) Co-authored-by: gabordikan --- commands/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 commands/.gitkeep diff --git a/commands/.gitkeep b/commands/.gitkeep new file mode 100644 index 0000000..e69de29 From d64bccb12dcb7301c50da5cec24e05356b9ec55d Mon Sep 17 00:00:00 2001 From: gabordikan <77804349+gabordikan@users.noreply.github.com> Date: Wed, 18 Aug 2021 14:27:16 +0200 Subject: [PATCH 3/4] Feature/oe 11669 (#22) * OE-11669: bugfix in IserObserver * OE-11669: bugfix in IserObserver * OE-11669: bugfix in UserObserver * OE-11669: bugfix in UserObserver * OE-11669: fixed user saving issue * OE-11669: fixed code issues Co-authored-by: gabordikan Co-authored-by: noone --- components/UserObserver.php | 68 +++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/components/UserObserver.php b/components/UserObserver.php index 75a1531..6ffdf34 100644 --- a/components/UserObserver.php +++ b/components/UserObserver.php @@ -44,11 +44,16 @@ public function updateUser($params) if (in_array($params['username'],Yii::app()->params['local_users'])) { return; } - - if (Yii::app()->params['mehstaffdb_always_refresh'] || $this->isStale($params['username'], $params['institution_authentication_id'])) { + if (!isset($params['institution_authentication_id'])) { + $institutionId = Institution::model()->find('remote_id = ?',array(Yii::app()->params['institution_code']))->id; + $institution_authentication_id = InstitutionAuthentication::model()->find('institution_id= ?',array($institutionId))->id; + } else { + $institution_authentication_id = $params['institution_authentication_id']; + } + + if (Yii::app()->params['mehstaffdb_always_refresh'] || $this->isStale($params['username'], $institution_authentication_id)) { try { $username = $params['username']; - $institution_authentication_id = $params['institution_authentication_id']; $remote_user = $this->getCSDClient()->getUserData($username); if ($remote_user = $this->getCSDClient()->getUserData($username)) { $remote_user = json_decode($remote_user, true); @@ -58,6 +63,11 @@ public function updateUser($params) if (!$user) { $user = new User(); + $user_authentication = new UserAuthentication(); + $user_authentication->password = $this->genPassword(); + $user_authentication->password_repeat = $user_authentication->password; + $user_authentication->institution_authentication_id = $institution_authentication_id; + $preexists = false; } else { $preexists = true; @@ -92,7 +102,7 @@ public function updateUser($params) } } catch (Exception $e) { \Yii::log("Unable to update user. Error: ". $e->getMessage(), \CLogger::LEVEL_ERROR); - throw new Exception("Unable to save user contact: ".print_r($user->getErrors(),true)); + throw new Exception("Unable to save user contact: ".print_r($e->getMessage(),true)); } } } @@ -102,9 +112,9 @@ public function updateUser($params) * Finds User by username * * @param string $username - * @return User + * @return User|null */ - private function getUser(string $username, int $institution_authentication_id): User + private function getUser(string $username, int $institution_authentication_id) { $criteria = new \CDbCriteria(); $criteria->join = 'JOIN user_authentication ua ON t.id = ua.user_id'; @@ -120,9 +130,9 @@ private function getUser(string $username, int $institution_authentication_id): * Finds UserAuthentication by username * * @param string $username - * @return UserAuthentication + * @return UserAuthentication|null */ - private function getUserAuthentication(string $username, int $institution_authentication_id): UserAuthentication + private function getUserAuthentication(string $username, int $institution_authentication_id) { $criteria = new \CDbCriteria(); $criteria->join = 'JOIN institution_authentication ia ON t.institution_authentication_id = ia.id'; @@ -155,6 +165,7 @@ private function saveUser(User $user, UserAuthentication $user_authentication, a $user->title = $remote_user['title']; $user->qualifications = $remote_user['qualifications']; $user->role = $remote_user['role']; + $user->setdefaultSSORights(); $user->doctor_grade_id = $this->getDoctorGradeFromJobTitle($remote_user['role']); if(isset($remote_user['registration_code']) && isset($remote_user['registration_code'][0]['ProfessionalRegistration'])) { $user->registration_code = $this->getGMCRegistrationNumber($remote_user['registration_code'][0]['ProfessionalRegistration']); @@ -169,9 +180,11 @@ private function saveUser(User $user, UserAuthentication $user_authentication, a throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); } + $user_authentication->user_id = $user->id; + if (!$user_authentication->save()) { - \Yii::log('Unable to save user: '.print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); - throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); + \Yii::log('Unable to save user: '.print_r($user_authentication->getErrors(),true), \CLogger::LEVEL_ERROR); + throw new Exception('Unable to save user: '.print_r($user_authentication->getErrors(),true)); } return $user; @@ -258,7 +271,7 @@ private function getDoctorGradeFromJobTitle(string $jobTitle): int return 33; // default value is Other } - private function getGMCRegistrationNumber($professional_registration){ + private function getGMCRegistrationNumber($professional_registration) { if($professional_registration) { $GMC = explode(" - ", $professional_registration); if (is_array($GMC) && count($GMC) > 0) { @@ -270,4 +283,37 @@ private function getGMCRegistrationNumber($professional_registration){ return ""; } } + + private function genPassword() { + $consonants = 'bcdfghjklmnpqrstvz'; + $vowels = 'aeiou'; + $special_characters = '_!*@'; + + $l = 8; + $code = ""; + + for ($i=0; $i<$l; $i++) { + if ($i%2===0) { + $r = rand(0,strlen($consonants)-1); + $letter = substr($consonants,$r,1); + } elseif ($i%4===3) { + $r = rand(0,9); + $letter = $r; + } else { + $r = rand(0,strlen($vowels)-1); + $letter = substr($vowels,$r,1); + } + + if ($i%4===0) { + $letter = strtoupper($letter); + } + $code .= $letter; + } + + $r = rand(0,strlen($special_characters)-1); + $letter = substr($special_characters,$r,1); + $code .= $letter; + + return $code; + } } From 63fce4266c437aaa971cbfe4a545586f2fce0406 Mon Sep 17 00:00:00 2001 From: kerdorina <50053502+kerdorina@users.noreply.github.com> Date: Mon, 11 Oct 2021 03:16:29 +0200 Subject: [PATCH 4/4] Feature/oe 11162 hotfix (#23) * OE-11162-hotfix: Fix validation error and user not found exception * OE-11162-hotfix: Fix active flag Co-authored-by: kerdorina --- components/UserObserver.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/UserObserver.php b/components/UserObserver.php index 6ffdf34..4ee91f0 100644 --- a/components/UserObserver.php +++ b/components/UserObserver.php @@ -90,7 +90,7 @@ public function updateUser($params) if ($user->contact_id !== $contact->id) { $user->contact_id = $contact->id; - if (!$user->save()) { + if (!$user->save(false)) { \Yii::log("Unable to save user contact: ".print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); throw new Exception("Unable to save user contact: ".print_r($user->getErrors(),true)); } @@ -98,7 +98,7 @@ public function updateUser($params) return $user; } else { \Yii::log("User " . $username . " not found in the CSD database.", \CLogger::LEVEL_ERROR); - throw new Exception("Unable to save user contact: ".$username); + return; } } catch (Exception $e) { \Yii::log("Unable to update user. Error: ". $e->getMessage(), \CLogger::LEVEL_ERROR); @@ -172,10 +172,10 @@ private function saveUser(User $user, UserAuthentication $user_authentication, a } $user->is_consultant = $remote_user['is_consultant']; $user->is_surgeon = $remote_user['is_surgeon']; - $user_authentication->active = !$remote_user['active']; + $user_authentication->active = $remote_user['active']; $user->global_firm_rights = 1; - if (!$user->save()) { + if (!$user->save(false)) { \Yii::log('Unable to save user: '.print_r($user->getErrors(),true), \CLogger::LEVEL_ERROR); throw new Exception('Unable to save user: '.print_r($user->getErrors(),true)); } @@ -206,7 +206,7 @@ private function saveContact(User $user, Contact $contact): Contact $contact->last_name = $user->last_name; $contact->qualifications = $user->qualifications; - if (!$contact->save()) { + if (!$contact->save(false)) { \Yii::log('Unable to save contact: '.print_r($contact->getErrors(),true), \CLogger::LEVEL_ERROR); throw new Exception('Unable to save contact: '.print_r($contact->getErrors(),true)); }