Add view-all-projects check and use in controller - #580
iftakharul-islam merged 3 commits into
Conversation
Introduce wedevs_pm_can_view_all_projects() in libs/functions.php (wrapping existing admin capability check) to express intent that only PM Admins and WP Admins can view all projects. Update Project_Controller to use this helper so non-admin users (PM Managers) are limited to viewing only their assigned projects.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughTwo capability checks are updated from manage to admin level across the project codebase. In the Project controller's fetch_projects_by_category method and the Project helper's where_users method, wedevs_pm_has_manage_capability is replaced with wedevs_pm_has_admin_capability, affecting project visibility filtering logic. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~4 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/Project/Controllers/Project_Controller.php (1)
155-159:⚠️ Potential issue | 🟠 MajorUpdate Search_Controller permission checks to match Project_Controller.
The permission function was changed from
wedevs_pm_has_manage_capability(includes both admins and managers) towedevs_pm_can_view_all_projects(admins only) in Project_Controller at line 155, but this change was not applied to Search_Controller. This creates a permission inconsistency: PM Managers will see all projects in search queries but only assigned projects in the project listing, depending on which endpoint they access.Update the three permission checks in Search_Controller (lines 139, 159, and 422) to use
wedevs_pm_can_view_all_projectsinstead ofwedevs_pm_has_manage_capabilityto ensure consistent behavior across all project-fetching endpoints.
|
PM_Manager still can see all projects @arifulhoque7 vai Screen.Recording.2026-02-20.at.2.12.24.PM.mov |
Replace wedevs_pm_has_manage_capability with wedevs_pm_can_view_all_projects when $inUsers is empty so users granted the 'view all projects' permission can proceed without needing the broader manage capability.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/Project/Helper/Project.php (2)
1415-1427:⚠️ Potential issue | 🟡 MinorUnreachable
return 10at Line 1426.The
returnon Line 1424 always exits the function; Line 1426 is dead code.🧹 Proposed fix
$per_page = wedevs_pm_get_setting( 'project_per_page' ); - return empty( $per_page ) ? 10 : (int) $per_page; - - return 10; + return empty( $per_page ) ? 10 : (int) $per_page; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Project/Helper/Project.php` around lines 1415 - 1427, The get_per_page() function contains an unreachable final "return 10" after an earlier return; remove the dead code and keep a single return path. Specifically, in Project::get_per_page() remove the trailing "return 10;" and ensure the method returns (int) $per_page when provided, falls back to wedevs_pm_get_setting('project_per_page') and finally returns 10 if that setting is empty—no extra return after those branches.
138-198:⚠️ Potential issue | 🟠 Major
count_project_by_type()is not user-aware — exposes all-project counts to restricted users and breaks pagination.
count_project_by_type()queries the full projects table with no user join or filter:SELECT DISTINCT COUNT(id) FROM {pm_projects} WHERE status = %dThese unfiltered counts populate
total_incomplete,total_complete,total_pending,total_archived, and criticallypagination.total(Line 155) in the response metadata. Meanwhile,total_projectsandtotal_pagesare derived from$this->found_rows(Line 147/157), which is filtered by the user-aware SQL query inget().The practical result after this PR's access-control change: a PM Manager restricted to, say, 5 assigned projects will receive a
pagination.totalof 50 (all system projects), causing broken/phantom pagination pages and leaking the aggregate project count.
count_project_by_typeneeds to be rewritten to join againstpm_role_userand filter by$current_user_idwhen the user is not an admin — mirroring the filtering logic inwhere_users().🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Project/Helper/Project.php` around lines 138 - 198, count_project_by_type currently counts across the whole pm_projects table and must be made user-aware; change count_project_by_type($type) to apply the same user filtering used in where_users() (join pm_role_user and filter by current user id when the current user is not an admin) so the counts (used by get_pagination_total and set_projects_meta) match $this->found_rows and the get() results; specifically, reuse or mirror where_users() logic (join wedevs_pm_tb_prefix() . 'pm_role_user', filter by $this->current_user_id or get_current_user_id(), and any capability checks) and update the SQL in count_project_by_type to include that JOIN/WHERE, returning the filtered COUNT(id).
🧹 Nitpick comments (1)
src/Project/Helper/Project.php (1)
118-132: Remove commented-out dead code blocks.There are several large commented-out code blocks across the file (Lines 118–132, 260–284, 304–314, 333–399). These should be deleted — version history covers their retrieval if ever needed.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Project/Helper/Project.php` around lines 118 - 132, Remove the large commented-out dead code blocks in src/Project/Helper/Project.php (the blocks around lines 118–132, 260–284, 304–314, 333–399) so the file contains only active logic; specifically delete the commented wrappers that reference methods like fromat_project and set_projects_meta (and any duplicated commented foreach/if branches) rather than uncommenting or changing them—rely on VCS history if the old code is needed.
🤖 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 `@src/Project/Helper/Project.php`:
- Around line 1415-1427: The get_per_page() function contains an unreachable
final "return 10" after an earlier return; remove the dead code and keep a
single return path. Specifically, in Project::get_per_page() remove the trailing
"return 10;" and ensure the method returns (int) $per_page when provided, falls
back to wedevs_pm_get_setting('project_per_page') and finally returns 10 if that
setting is empty—no extra return after those branches.
- Around line 138-198: count_project_by_type currently counts across the whole
pm_projects table and must be made user-aware; change
count_project_by_type($type) to apply the same user filtering used in
where_users() (join pm_role_user and filter by current user id when the current
user is not an admin) so the counts (used by get_pagination_total and
set_projects_meta) match $this->found_rows and the get() results; specifically,
reuse or mirror where_users() logic (join wedevs_pm_tb_prefix() .
'pm_role_user', filter by $this->current_user_id or get_current_user_id(), and
any capability checks) and update the SQL in count_project_by_type to include
that JOIN/WHERE, returning the filtered COUNT(id).
---
Nitpick comments:
In `@src/Project/Helper/Project.php`:
- Around line 118-132: Remove the large commented-out dead code blocks in
src/Project/Helper/Project.php (the blocks around lines 118–132, 260–284,
304–314, 333–399) so the file contains only active logic; specifically delete
the commented wrappers that reference methods like fromat_project and
set_projects_meta (and any duplicated commented foreach/if branches) rather than
uncommenting or changing them—rely on VCS history if the old code is needed.
Remove the deprecated wedevs_pm_can_view_all_projects wrapper from libs/functions.php and update calls to use wedevs_pm_has_admin_capability in Project_Controller and Project helper. This consolidates permission logic so only users with the admin capability can view all projects, removing the obsolete function.
iftakharul-islam
left a comment
There was a problem hiding this comment.
Please call existing
| * @param int|false $user_id | ||
| * @return bool | ||
| */ | ||
| function wedevs_pm_can_view_all_projects( $user_id = false ) { |
There was a problem hiding this comment.
Please change this and call the existing function, as it is reflecting the purpose of checks.
* Add view-all-projects check and use in controller Introduce wedevs_pm_can_view_all_projects() in libs/functions.php (wrapping existing admin capability check) to express intent that only PM Admins and WP Admins can view all projects. Update Project_Controller to use this helper so non-admin users (PM Managers) are limited to viewing only their assigned projects. * Use view-all-projects capability check Replace wedevs_pm_has_manage_capability with wedevs_pm_can_view_all_projects when $inUsers is empty so users granted the 'view all projects' permission can proceed without needing the broader manage capability. * Use admin capability check instead of view-all fn Remove the deprecated wedevs_pm_can_view_all_projects wrapper from libs/functions.php and update calls to use wedevs_pm_has_admin_capability in Project_Controller and Project helper. This consolidates permission logic so only users with the admin capability can view all projects, removing the obsolete function.
Introduce wedevs_pm_can_view_all_projects() in libs/functions.php (wrapping existing admin capability check) to express intent that only PM Admins and WP Admins can view all projects. Update Project_Controller to use this helper so non-admin users (PM Managers) are limited to viewing only their assigned projects.
Close 272
Summary by CodeRabbit