Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes:Gives access of consultation details to users for only allowed facilities. #8727

Conversation

Sulochan-khadka
Copy link
Contributor

@Sulochan-khadka Sulochan-khadka commented Oct 6, 2024

Proposed Changes

@ohcnetwork/care-fe-code-reviewers

Merge Checklist

  • Add specs that demonstrate bug / test a new feature.
  • Update product documentation.
  • Ensure that UI text is kept in I18n files.
  • Prep screenshot or demo video for changelog entry, and attach it to issue.
  • Request for Peer Reviews
  • Completion of QA

Summary by CodeRabbit

  • New Features

    • Enhanced button functionality in the ConsultationCard to conditionally disable actions based on facility permissions.
    • Integrated facility permission checks for improved user interaction during consultations.
  • Bug Fixes

    • Retained error handling for the "Add Consultation Updates" button to ensure proper notifications for bed assignments.

@Sulochan-khadka Sulochan-khadka requested a review from a team as a code owner October 6, 2024 14:44
Copy link

netlify bot commented Oct 6, 2024

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 5a91875
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/671636bb29699100088aa5de
😎 Deploy Preview https://deploy-preview-8727--care-ohc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@nihal467
Copy link
Member

nihal467 commented Oct 8, 2024

image

  • buttons are disabled for the consultation card to which the users have access to, which is not the expected behavior, they should be allowed to access it, check the comment and issues once again @Sulochan-khadka

@Sulochan-khadka
Copy link
Contributor Author

image

  • buttons are disabled for the consultation card to which the users have access to, which is not the expected behavior, they should be allowed to access it, check the comment and issues once again @Sulochan-khadka

Apologies for the incorrect changes.
The issue is not very clear , which is why i asked there but was no reply, therefore i went ahead and implemented whatever i understood. It would be great of you if you kindly provide the steps to reproduce the issue.

@nihal467
Copy link
Member

nihal467 commented Oct 10, 2024

  • Any non-admin users, should be able to access only the consultation card from facility there are linked, if the patient has any consultation history from facility where they are not linked, then the button on that particular card should be disabled

step 1: Admit Patient A to Facility A, then discharge them from Facility A. Repeat this process for Facility B and Facility C.
Step 2: After these admissions and discharges, Patient A will have a consultation history from Facilities A, B, and C.
Step 3: Log in with a nurse's user ID that is linked only to Facilities A and B.
Step 4: Now re-admit Patient A to one of the facilities (either A or B) where the nurse is linked.
Step 5: Access Patient A’s consultation history. In the consultation cards, the button in the consultation card related to Facility C should be disabled, as the nurse is not linked to Facility C.

@Sulochan-khadka
Copy link
Contributor Author

  • Any non-admin users, should be able to access only the consultation card from facility there are linked, if the patient has any consultation history from facility where they are not linked, then the button on that particular card should be disabled

step 1: Admit Patient A to Facility A, then discharge them from Facility A. Repeat this process for Facility B and Facility C. Step 2: After these admissions and discharges, Patient A will have a consultation history from Facilities A, B, and C. Step 3: Log in with a nurse's user ID that is linked only to Facilities A and B. Step 4: Now re-admit Patient A to one of the facilities (either A or B) where the nurse is linked. Step 5: Access Patient A’s consultation history. In the consultation cards, the button in the consultation card related to Facility C should be disabled, as the nurse is not linked to Facility C.

Thankyou very much for the much needed explaination. I have made some changes, kindly review them. And let me know in case of modifications.

@nihal467
Copy link
Member

@Sulochan-khadka
image

  • Now, the district admin itself is blocked from accessing the button in consultation card, before requesting another review, make sure admins (district and state admin) have no restriction at all and non-admins (doctor, nurse, all other users) have restrictions are per issue

@Sulochan-khadka
Copy link
Contributor Author

Sulochan-khadka commented Oct 14, 2024

@Sulochan-khadka image

  • Now, the district admin itself is blocked from accessing the button in consultation card, before requesting another review, make sure admins (district and state admin) have no restriction at all and non-admins (doctor, nurse, all other users) have restrictions are per issue

The logic seems pretty convincing that the consultation facilities are checked whether it matches with the allowed facilities of the user.

Screenshot 2024-10-15 at 1 54 11 AM

However, the problem you stated occurs when the facility is no more linked with the district admin and i think that is correct since it may happen that some facilities get linked to different district admins and therefore, the previous admin should not have the access to it. Right?!
Screenshot 2024-10-15 at 2 08 02 AM

Also "View / Upload Consultation Files" should also have same added condition right? That if the user is not allowed a facility, it should not be able to interact with the consultation records. Right?! @nihal467

@nihal467 nihal467 added the question Further information is requested label Oct 15, 2024
@bodhish
Copy link
Member

bodhish commented Oct 18, 2024

@Sulochan-khadka The district admins and state admins currently have a super power that they can view everything that they can see. We will eventually be taking it down.

In this specific case lets keep it blocked, if someone comes up with an issue lets understand what they want. From a privacy perspective its always safe to limit maximum access.

@nihal467 do mark someone to address these questions when you see it.

@bodhish
Copy link
Member

bodhish commented Oct 21, 2024

also @Sulochan-khadka add a proper title for the PR

@Sulochan-khadka Sulochan-khadka changed the title Issues/8029/modify nurse access to consultation page Fixes:Gives access to users for only allowed facilities. Oct 21, 2024
@Sulochan-khadka
Copy link
Contributor Author

@bodhish so my logic is fine right? Also if the user is not allowed a facility, it should not be able to interact with the consultation records. Right?!

Also i have changed the PR title. I hope this solves the objectives

@Sulochan-khadka Sulochan-khadka changed the title Fixes:Gives access to users for only allowed facilities. Fixes:Gives access of consultation details to users for only allowed facilities. Oct 21, 2024
Copy link

👋 Hi, @Sulochan-khadka,
Conflicts have been detected against the base branch. Please rebase your branch against the base branch.


This message is automatically generated by prince-chrismc/label-merge-conflicts-action so don't hesitate to report issues/improvements there.

@github-actions github-actions bot added the merge conflict pull requests with merge conflict label Oct 23, 2024
@github-actions github-actions bot added the stale label Oct 31, 2024
@Sulochan-khadka
Copy link
Contributor Author

@nihal467 @bodhish @rithviknishad is the proposed solution appropriate? Let me know, so that i can solve the merge conflicts.

@github-actions github-actions bot removed the stale label Nov 3, 2024
@bodhish
Copy link
Member

bodhish commented Nov 6, 2024

@nihal467

Copy link
Contributor

coderabbitai bot commented Nov 8, 2024

Walkthrough

The changes in the ConsultationCard component include the integration of a new dependency on the useQuery hook to fetch permitted facilities from the API. This update modifies button states based on facility permissions, disabling specific buttons if the facility is not permitted or if the patient has been discharged. The component's overall structure remains unchanged, with error handling for the "Add Consultation Updates" button retained.

Changes

File Change Summary
src/Components/Facility/ConsultationCard.tsx Added facilities variable for permitted facilities; updated button onClick and disabled states based on facility permissions.

Assessment against linked issues

Objective Addressed Explanation
Modify non-admin users access to the patient consultation record (#8029)

Suggested labels

tested, Deploy-Failed

Poem

🐇 In the card where consultations dwell,
Permissions now cast a clearer spell.
Buttons that guide, now wise and true,
Only for facilities that welcome you.
With each update, the flow is bright,
Ensuring users find their way just right! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
src/Components/Facility/ConsultationCard.tsx (3)

179-181: Enhance permission checks and improve user feedback.

The facility permission checks are consistent but could be improved for better maintainability and user experience.

  1. Extract the permission check to a reusable function:
+const hasPermissionForFacility = (facilities: any[], facilityName: string) => {
+  return facilities?.some((item) => item.name === facilityName);
+};

 // Then use it in the buttons:
 disabled={!hasPermissionForFacility(facilities, itemData.facility_name)}
  1. Add tooltips to explain why buttons are disabled:
 <ButtonV2
   disabled={!hasPermissionForFacility(facilities, itemData.facility_name)}
+  title={!hasPermissionForFacility(facilities, itemData.facility_name) 
+    ? "You don't have permission to access this facility's consultations" 
+    : undefined}
 >

Also applies to: 192-194, 213-218


Line range hint 1-228: Consider caching and memoizing facility permissions.

The current implementation fetches facility permissions for each consultation card, which could impact performance when displaying multiple consultations.

Consider these architectural improvements:

  1. Move facility permissions to a higher-level context or state management
  2. Implement caching for facility permissions
  3. Memoize the permission check function to prevent unnecessary re-renders

This would improve performance while maintaining the security requirements for facility-based access control.


Based on the shell script results, I can now provide a final response:

Consider using facility IDs for permission checks instead of names

The codebase shows that facility IDs are consistently used throughout the application for most operations (routing, API calls, etc.). However, the permission check in ConsultationCard.tsx uses facility names for comparison:

!facilities?.some((item) => item.name === itemData.facility_name)

Key findings that support using IDs instead of names:

  • Facility IDs are used as stable identifiers in API routes (/api/v1/facility/{facility_id}/)
  • The facility model contains both id and facility_name fields
  • Other components like FacilityCard use IDs for routing (/facility/${facility.id})

Consider refactoring the permission check to use IDs:

!facilities?.some((item) => item.id === itemData.facility)
🔗 Analysis chain

Line range hint 179-218: Verify facility name comparison logic.

The current implementation uses facility names for permission checks. This might be fragile if facility names can change or contain special characters.

Let's verify the facility data structure and potential edge cases:

Consider using facility IDs instead of names for more reliable permission checks if the data structure supports it.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check facility name uniqueness and format
# Expected: Facility names should be unique and follow a consistent format

# Check facility name patterns in the codebase
echo "Checking facility name patterns..."
rg -g '!*.test.*' -g '!*.spec.*' 'facility_name' -B 2 -A 2

# Check for facility ID usage
echo "Checking facility ID usage..."
rg -g '!*.test.*' -g '!*.spec.*' 'facility.id|facility_id' -B 2 -A 2

Length of output: 26919

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7ea31d3 and 5a91875.

📒 Files selected for processing (1)
  • src/Components/Facility/ConsultationCard.tsx (5 hunks)

Comment on lines +13 to +14
import useQuery from "../../Utils/request/useQuery";
import routes from "../../Redux/api";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add loading and error handling for facility data fetching.

The facility data fetching implementation could be enhanced to handle edge cases and improve user experience.

Consider these improvements:

 import useQuery from "../../Utils/request/useQuery";
 import routes from "../../Redux/api";
+import { LoadingIndicator } from "../Common/LoadingIndicator";

 export const ConsultationCard = (props: ConsultationProps) => {
-  const facilities = useQuery(routes.getPermittedFacilities).data?.results;
+  const { data: facilitiesData, error: facilitiesError, loading } = useQuery(routes.getPermittedFacilities);
+  const facilities = facilitiesData?.results;
+
+  if (loading) {
+    return <LoadingIndicator />;
+  }
+
+  if (facilitiesError) {
+    Notification.Error({
+      msg: "Failed to fetch facility permissions",
+    });
+  }

Also applies to: 30-30

@Sulochan-khadka
Copy link
Contributor Author

do i need to add these implementations recommended by this bot , or is it optional? @nihal467

@nihal467
Copy link
Member

image
image

@bodhish Currently, admins can access all routes of the disabled button in the patient details page from the patient consultation page, but this approach isn’t ideal for effectively restricting access. Many users with district admin roles rely on this access to manage certain data.

The main issue we’re aiming to resolve is to ensure that non-admin users cannot access consultation files of a facility that isn’t linked to their user ID. If a non-admin user who is not linked to a facility tries to access facility data, either by clicking on a patient card to view consultations from the patient list page or by using the "View Consultation" button on the patient details page, an error message—such as "You don't have access to this file"—should be displayed instead of redirecting them to a 404 page.

Could you review his deploy preview along with my comment, and then provide a response?

@bodhish
Copy link
Member

bodhish commented Nov 13, 2024

Does our backend send data for these requests / pages?

@nihal467
Copy link
Member

nihal467 commented Nov 13, 2024

@bodhish

Does our backend send data for these requests / pages?

backend will not send the data if the user doesn't have access to the consultation record

@bodhish
Copy link
Member

bodhish commented Nov 13, 2024

In that case should we over engineer? The page would render the empty message right 🤔

@nihal467
Copy link
Member

nihal467 commented Nov 13, 2024

In that case should we over engineer? The page would render the empty message right 🤔

Yes, rather than disabling the button, we can simply throw an error on the frontend stating, "You don't have access to this file," replacing the current behavior of redirecting restricted users to a 404 page.

CC: @Sulochan-khadka

@nihal467
Copy link
Member

In that case should we over engineer? The page would render the empty message right 🤔

Yes, rather than disabling the button, we can simply throw an error on the frontend stating, "You don't have access to this file," replacing the current behavior of redirecting restricted users to a 404 page.

CC: @Sulochan-khadka

@bodhish is this approach ok ?

@nihal467 nihal467 removed needs testing needs review merge conflict pull requests with merge conflict labels Nov 19, 2024
@github-actions github-actions bot added the merge conflict pull requests with merge conflict label Nov 20, 2024
Copy link

👋 Hi, @Sulochan-khadka,
Conflicts have been detected against the base branch. Please rebase your branch against the base branch.


This message is automatically generated by prince-chrismc/label-merge-conflicts-action so don't hesitate to report issues/improvements there.

@@ -173,6 +176,9 @@ export const ConsultationCard = (props: ConsultationProps) => {
`/facility/${itemData.facility}/patient/${itemData.patient}/consultation/${itemData.id}`,
)
}
disabled={
!facilities?.some((item) => item.name === itemData.facility_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we have facility id in the api ? 🤔

@bodhish
Copy link
Member

bodhish commented Nov 25, 2024

Closing the issue as don't have any progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge conflict pull requests with merge conflict question Further information is requested test failed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modify non-admin users access to the patient consultation record
4 participants