Skip to content

Commit

Permalink
fix(#9203): add facility_id backward compatibility in admin app (#9204)
Browse files Browse the repository at this point in the history
Co-authored-by: latin-panda <66472237+latin-panda@users.noreply.github.com>
  • Loading branch information
Benmuiruri and latin-panda authored Jun 25, 2024
1 parent 8ed110d commit 7bcb375
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
21 changes: 19 additions & 2 deletions admin/src/js/controllers/edit-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ angular

const allowTokenLogin = settings => settings.token_login && settings.token_login.enabled;

/**
* Ensures that facility_id is an array for backward compatibility.
* @returns {Array} The normalized facility_id as an array.
*/
const getFacilityId = function () {
if (!$scope.model.facility_id) {
$scope.model.facility_id = [];
}

if (!Array.isArray($scope.model.facility_id)) {
$scope.model.facility_id = [$scope.model.facility_id];
}

return $scope.model.facility_id;
};

const determineEditUserModel = function() {
// Edit a user that's not the current user.
// $scope.model is the user object passed in by controller creating the Modal.
Expand All @@ -75,6 +91,7 @@ angular
return $q.resolve({});
}

const facilityId = getFacilityId();
const tokenLoginData = $scope.model.token_login;
const tokenLoginEnabled = tokenLoginData &&
{
Expand All @@ -92,8 +109,8 @@ angular
phone: $scope.model.phone,
// FacilitySelect is what binds to the select, place is there to
// compare to later to see if it's changed once we've run computeFields();
facilitySelect: $scope.model.facility_id || [],
place: $scope.model.facility_id,
facilitySelect: facilityId,
place: facilityId,
roles: getRoles($scope.model.roles),
// ^ Same with contactSelect vs. contact
contactSelect: $scope.model.contact_id,
Expand Down
22 changes: 22 additions & 0 deletions admin/tests/unit/controllers/edit-user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('EditUserCtrl controller', () => {
let Translate;
let Settings;
let userToEdit;
let user;
let http;

beforeEach(() => {
Expand Down Expand Up @@ -178,6 +179,27 @@ describe('EditUserCtrl controller', () => {
});
});

describe('Initializing existing users', () => {
user = {
_id: 'user.id',
name: 'user.name',
fullname: 'user.fullname',
email: 'user@email.com',
phone: 'user.phone',
facility_id: 'abc',
contact_id: 'xyz',
roles: ['supervisor'],
language: 'zz',
};

it('converts string facility_id to Array ', () => {
return mockEditAUser(user).setupPromise.then(() => {
chai.expect(scope.editUserModel.facilitySelect).to.deep.equal(['abc']);
chai.expect(scope.editUserModel.facilitySelect).to.be.an('array');
});
});
});

describe('$scope.editUser', () => {
it('username must be present', () => {
return mockEditAUser(userToEdit)
Expand Down
1 change: 1 addition & 0 deletions config/default/app_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
"can_view_old_filter_and_search": [],
"can_view_old_action_bar": [],
"can_default_facility_filter": [],
"can_have_multiple_places": [],
"can_export_devices_details": [
"national_admin"
]
Expand Down
4 changes: 3 additions & 1 deletion ddocs/medic-db/medic-client/validate_doc_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ function(newDoc, oldDoc, userCtx, secObj) {
if (isDbAdmin(userCtx, secObj)) {
return;
}
if (userCtx.facility_id === newDoc._id) {
if (userCtx.facility_id === newDoc._id ||
(Array.isArray(userCtx.facility_id) && userCtx.facility_id.includes(newDoc._id ))
) {
_err('You are not authorized to edit your own place');
}
if (newDoc.type === 'form') {
Expand Down
4 changes: 3 additions & 1 deletion ddocs/medic-db/medic/validate_doc_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ function(newDoc, oldDoc, userCtx, secObj) {
_err('You are not authorized to edit admin only docs');
}

if (userCtx.facility_id === newDoc._id) {
if (userCtx.facility_id === newDoc._id ||
(Array.isArray(userCtx.facility_id) && userCtx.facility_id.includes(newDoc._id ))
) {
_err('You are not authorized to edit your own place');
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class ContactsMoreMenuComponent implements OnInit, OnDestroy {
&& !this.loadingContent
&& this.snapshotData?.name === 'contacts.detail'
&& this.hasEditPermission
&& (this.isOnlineOnly || this.userSettings?.facility_id !== this.selectedContactDoc?._id);
&& (this.isOnlineOnly || !this.isUserFacility);
}

displayDeleteOption() {
Expand Down

0 comments on commit 7bcb375

Please sign in to comment.