Skip to content

fix: restrict API for pwa to fetch current logged-in employee only#4207

Merged
iamkhanraheel merged 4 commits intofrappe:developfrom
iamkhanraheel:fix/pwa_perm_check
Mar 11, 2026
Merged

fix: restrict API for pwa to fetch current logged-in employee only#4207
iamkhanraheel merged 4 commits intofrappe:developfrom
iamkhanraheel:fix/pwa_perm_check

Conversation

@iamkhanraheel
Copy link
Collaborator

@iamkhanraheel iamkhanraheel commented Mar 9, 2026

Summary by CodeRabbit

  • Refactor
    • Centralized employee context resolution across attendance, shifts, leave balance, expense claims, and advance features
    • API endpoints now automatically determine the current employee instead of requiring explicit parameters from frontend requests
    • Enhanced consistency and maintainability of employee-scoped data retrieval

@iamkhanraheel iamkhanraheel marked this pull request as ready for review March 10, 2026 10:52
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 10, 2026

Walkthrough

Frontend code removed explicit employee context and employee request parameters from AttendanceCalendar.vue, Dashboard.vue, and data modules (advances.js, claims.js, leaves.js), so API calls no longer include an employee filter. Backend added a new get_current_employee() helper and updated API functions (get_attendance_calendar_events, get_shifts, get_leave_balance_map, get_expense_claim_summary, get_employee_advance_balance) to resolve the employee internally, removing employee parameters from their signatures and updating call sites.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main objective: restricting APIs to fetch only the current logged-in employee's data, which aligns with the changes removing explicit employee parameters and using get_current_employee() instead.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@hrms/api/__init__.py`:
- Around line 81-85: get_current_employee() calls
get_current_employee_info().get("name") directly which raises AttributeError
when get_current_employee_info() returns None; change it to first assign the
result to a variable (e.g., info = get_current_employee_info()), check if info
is falsy and call frappe.throw(_("Employee not found"), frappe.PermissionError)
before accessing info.get("name"), then proceed to extract and validate the
"name" and throw the same PermissionError if missing.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 19736e76-3c56-46fd-8044-4643a0793b67

📥 Commits

Reviewing files that changed from the base of the PR and between c09ae04 and 15c3169.

📒 Files selected for processing (6)
  • frontend/src/components/AttendanceCalendar.vue
  • frontend/src/data/advances.js
  • frontend/src/data/claims.js
  • frontend/src/data/leaves.js
  • frontend/src/views/attendance/Dashboard.vue
  • hrms/api/__init__.py
💤 Files with no reviewable changes (5)
  • frontend/src/data/leaves.js
  • frontend/src/data/advances.js
  • frontend/src/data/claims.js
  • frontend/src/views/attendance/Dashboard.vue
  • frontend/src/components/AttendanceCalendar.vue

@asmitahase
Copy link
Member

rebase and merge

@iamkhanraheel
Copy link
Collaborator Author

@Mergifyio rebase

@mergify
Copy link
Contributor

mergify bot commented Mar 11, 2026

rebase

✅ Branch has been successfully rebased

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
hrms/api/__init__.py (1)

63-78: ⚠️ Potential issue | 🟠 Major

get_all_employees() is now half-way between a directory API and a self API.

Line 64 switches this to permission-scoped frappe.get_list() with no explicit filter for the session user. This means users with broad Employee read access can still get multiple rows, while frontend/src/data/employees.js:1-35 consumes this endpoint as a directory and already has employeeResource for self data. The PR objective is to restrict PWA APIs to the current logged-in employee. Please make the contract explicit: either add filters={"user": frappe.session.user} here, or keep this as the unrestricted directory API and use get_current_employee_info() for self-only flows.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hrms/api/__init__.py` around lines 63 - 78, The get_all_employees() function
currently uses permission-scoped frappe.get_list() but isn't explicit about
restricting results to the current session user; either make it a directory
endpoint or restrict it to the logged-in employee—add filters={"user":
frappe.session.user} to the frappe.get_list call in get_all_employees() to
return only the current user's employee row, or alternatively leave
get_all_employees() as an unrestricted directory API and update callers to use
get_current_employee_info() (or similar self-only function) for self-only flows;
pick one approach and make the contract explicit in get_all_employees().
♻️ Duplicate comments (1)
hrms/api/__init__.py (1)

81-85: ⚠️ Potential issue | 🔴 Critical

Guard the missing-employee path before calling .get().

Line 82 dereferences .get("name") on the result of get_current_employee_info(), but frappe.db.get_value(..., as_dict=True) returns None when no active Employee matches. That turns the expected PermissionError into a 500 for every endpoint that now depends on this helper.

🛠️ Proposed fix
 def get_current_employee() -> str:
-	employee = get_current_employee_info().get("name")
+	employee_info = get_current_employee_info()
+	employee = employee_info.get("name") if employee_info else None
 	if not employee:
 		frappe.throw(_("Employee not found"), frappe.PermissionError)
 	return employee
In current Frappe, what does frappe.db.get_value(..., as_dict=True) return when no document matches the filters?
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hrms/api/__init__.py` around lines 81 - 85, The helper get_current_employee
dereferences get_current_employee_info().get("name") without checking for None;
since frappe.db.get_value(..., as_dict=True) returns None when no Employee
matches, modify get_current_employee to first assign info =
get_current_employee_info(), check if info is truthy (or is None) and call
frappe.throw(_("Employee not found"), frappe.PermissionError) before accessing
info.get("name"), then return the name; reference get_current_employee and
get_current_employee_info to locate the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@hrms/api/__init__.py`:
- Around line 63-78: The get_all_employees() function currently uses
permission-scoped frappe.get_list() but isn't explicit about restricting results
to the current session user; either make it a directory endpoint or restrict it
to the logged-in employee—add filters={"user": frappe.session.user} to the
frappe.get_list call in get_all_employees() to return only the current user's
employee row, or alternatively leave get_all_employees() as an unrestricted
directory API and update callers to use get_current_employee_info() (or similar
self-only function) for self-only flows; pick one approach and make the contract
explicit in get_all_employees().

---

Duplicate comments:
In `@hrms/api/__init__.py`:
- Around line 81-85: The helper get_current_employee dereferences
get_current_employee_info().get("name") without checking for None; since
frappe.db.get_value(..., as_dict=True) returns None when no Employee matches,
modify get_current_employee to first assign info = get_current_employee_info(),
check if info is truthy (or is None) and call frappe.throw(_("Employee not
found"), frappe.PermissionError) before accessing info.get("name"), then return
the name; reference get_current_employee and get_current_employee_info to locate
the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4e160e14-1c7c-4ad6-b031-11b769aeed29

📥 Commits

Reviewing files that changed from the base of the PR and between 15c3169 and 915abae.

📒 Files selected for processing (6)
  • frontend/src/components/AttendanceCalendar.vue
  • frontend/src/data/advances.js
  • frontend/src/data/claims.js
  • frontend/src/data/leaves.js
  • frontend/src/views/attendance/Dashboard.vue
  • hrms/api/__init__.py
💤 Files with no reviewable changes (5)
  • frontend/src/data/advances.js
  • frontend/src/data/claims.js
  • frontend/src/components/AttendanceCalendar.vue
  • frontend/src/views/attendance/Dashboard.vue
  • frontend/src/data/leaves.js

@iamkhanraheel iamkhanraheel merged commit 1848d34 into frappe:develop Mar 11, 2026
8 checks passed
asmitahase added a commit that referenced this pull request Mar 11, 2026
…4207

fix: restrict API for pwa to fetch current logged-in employee only (backport #4207)
asmitahase added a commit that referenced this pull request Mar 12, 2026
…4207

fix: restrict API for pwa to fetch current logged-in employee only (backport #4207)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants