Skip to content

Commit e9c129b

Browse files
authored
Merge pull request #185 from Chloe070196/DIS-239_evergreen_username_update
DIS-239: evergreen username and email update
2 parents e62b35d + 4db73aa commit e9c129b

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

code/web/Drivers/Evergreen.php

+89-3
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,69 @@ function changeHoldPickupLocation(User $patron, $recordId, $itemToUpdateId, $new
778778
return $result;
779779
}
780780

781-
function updatePatronInfo(User $patron, $canUpdateContactInfo, $fromMasquerade): array {
782-
return [
781+
public function updatePatronInfo(User $patron, $canUpdateContactInfo, $fromMasquerade): array {
782+
$authToken = $this->getAPIAuthToken($patron, true);
783+
$userMessages = [
783784
'success' => false,
784-
'messages' => ['Cannot update patron information with this ILS.'],
785+
'messages' => [],
786+
];
787+
788+
if (!$authToken || !$canUpdateContactInfo) {
789+
$userMessages['messages'][] = 'Your contact information cannot be updated.';
790+
return $userMessages;
791+
}
792+
$propertyName = '';
793+
794+
if (!isset($_REQUEST['email'])) {
795+
return $userMessages;
796+
}
797+
$propertyName = 'email';
798+
$propertyValue = $_REQUEST['email'];
799+
$success = $this->updatePatronProperty($propertyName, $propertyValue, $patron->ils_password, $authToken);
800+
$userMessages['messages'][] = $success ? 'Your email has been updated.' : 'Your email could not be updated. Please contact your library';
801+
$userMessages['success'] = $success;
802+
803+
return $userMessages;
804+
}
805+
806+
private function updatePatronProperty($propertyName, $propertyValue, $patronIlsPassword, $authToken): bool {
807+
$evergreenUrl = $this->accountProfile->patronApiUrl . "/osrf-gateway-v1/$propertyName";
808+
$headers = [
809+
'Content-Type: application/x-www-form-urlencoded',
785810
];
811+
$this->apiCurlWrapper->addCustomHeaders($headers, false);
812+
/**The order of the request parameters is crucial
813+
* This Evergreen API only handles unnamed parameters,
814+
* And so it is only their position within the URL that
815+
* determines which is which
816+
*/
817+
$request = 'service=open-ils.actor';
818+
$request .= "&method=open-ils.actor.user.$propertyName.update";
819+
$request .= '&param=' . json_encode($authToken);
820+
$request .= '&param=' . json_encode($propertyValue);
821+
$request .= '&param=' . json_encode($patronIlsPassword);
822+
823+
$apiResponse = $this->apiCurlWrapper->curlPostPage($evergreenUrl, $request);
824+
ExternalRequestLogEntry::logRequest('evergreen.updatePatronProperty', 'POST', $evergreenUrl, $this->apiCurlWrapper->getHeaders(), $request, $this->apiCurlWrapper->getResponseCode(), $apiResponse, []);
825+
826+
/** It seems the response sent back by the Evergreen API will have a
827+
* status code of 200 even upon failure (eg. if the patron password
828+
* is incorrect.) However, a successful update will always result
829+
* in the "payload" property being set to 1. Check for this instead.
830+
*/
831+
$responseData = json_decode($apiResponse, true);
832+
$success = isset($responseData['payload']) && $responseData['payload'][0] == 1;
833+
return $success;
834+
}
835+
836+
837+
private function isDuplicateUserName($username):bool {
838+
global $aspen_db;
839+
$condition = "ils_username=" . "'" . $username . "'";
840+
$query = "SELECT COUNT(ils_username) FROM user WHERE $condition;";
841+
$queryObject = $aspen_db->query($query, PDO::FETCH_ASSOC);
842+
$results = $queryObject->fetch();
843+
return isset($results["COUNT(ils_username)"]) && $results["COUNT(ils_username)"];
786844
}
787845

788846
public function hasNativeReadingHistory(): bool {
@@ -1102,6 +1160,28 @@ public function getHolds(User $patron): array {
11021160
}
11031161
return $holds;
11041162
}
1163+
1164+
public function updateEditableUsername(User $patron, string $username): array {
1165+
$authToken = $this->getAPIAuthToken($patron, true);
1166+
if ($this->isDuplicateUserName($username)) {
1167+
$userMessages['message'] = 'This username is already in use. Please choose another username.';
1168+
$userMessages['success'] = false;
1169+
return $userMessages;
1170+
}
1171+
$success = $this->updatePatronProperty('username', $username, $patron->ils_password, $authToken);
1172+
$userMessages['message'] = $success ? 'Your username has been updated.' : 'Your username could not be updated. Please contact your library';
1173+
$userMessages['success'] = $success;
1174+
return $userMessages;
1175+
}
1176+
1177+
public function getEditableUsername(User $user) {
1178+
$this->loadContactInformation($user);
1179+
return $user->ils_username;
1180+
}
1181+
1182+
public function hasEditableUsername() {
1183+
return true;
1184+
}
11051185

11061186
/**
11071187
* @inheritDoc
@@ -2530,6 +2610,12 @@ public function loadContactInformation(User $user) {
25302610
if (!empty($mappedPatronData['suffix'])) {
25312611
$user->_fullname .= ' ' . $mappedPatronData['suffix'];
25322612
}
2613+
if (!empty($mappedPatronData['email'])) {
2614+
$user->email = $mappedPatronData['email'];
2615+
}
2616+
if (!empty($mappedPatronData['usrname'])) {
2617+
$user->ils_username = $mappedPatronData['usrname'];
2618+
}
25332619
$user->_fullname = trim($user->_fullname);
25342620

25352621
if (!empty($mappedPatronData['expire_date'])) {

code/web/release_notes/25.03.00.MD

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
// alexander
3131

3232
// chloe
33+
### Evegreen Integration Updates
34+
- Enable patrons to update their Evergreen username through the My Preferences page (DIS-240) (*CZ*)
35+
- Prevent patrons from updating their Evergreen username to a username already in use within the system by carrying out a case-insensitive check (*CZ*)
36+
- Enable patrons to update their email address throught the Contact Information page (DIS-239) (*CZ*)
3337

3438
// lucas
3539

0 commit comments

Comments
 (0)