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

fix: get_doctypes_with_custom_docperms and get_doctypes_with_read #26018

Closed
wants to merge 1 commit into from

Conversation

casesolved-co-uk
Copy link
Contributor

@casesolved-co-uk casesolved-co-uk commented Apr 17, 2024

fix: #26017 and #26015. Remove get_doctypes_with_custom_docperms because unused in frappe and erpnext

fixes: #26017 and #26015

version-14-hotfix
version-15-hotfix

…ocperms because unused in frappe and erpnext
@casesolved-co-uk casesolved-co-uk requested review from a team and ankush and removed request for a team April 17, 2024 15:44
@github-actions github-actions bot added the add-test-cases Add test case to validate fix or enhancement label Apr 17, 2024
@ankush ankush changed the title fix: #26017 and #26015. Remove get_doctypes_with_custom_docperms beca… fix: get_doctypes_with_custom_docperms and get_doctypes_with_read Apr 18, 2024
frappe/permissions.py Show resolved Hide resolved
@@ -461,8 +461,8 @@ def get_valid_perms(doctype=None, user=None):
perms = get_perms_for(roles)
custom_perms = get_perms_for(roles, "Custom DocPerm")

doctypes_with_custom_perms = get_doctypes_with_custom_docperms()
for p in perms:
doctypes_with_custom_perms = list(set(p.parent for p in custom_perms if p.parent))
Copy link
Member

Choose a reason for hiding this comment

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

This isn't correct.

If ANY custom perms exist then only custom perms should be used. The code is bit convoluted here because it's supposed to work with specific doctype or all doctypes.

This change will break use case like this:

  • Standard role perms allow read to role X but not Y.
  • Custom role perms don't allow X to read.
  • Now custom docperms will be empty for role X.

As per your change standard role perms will be appended and user has no way to remove it?

Copy link
Contributor Author

@casesolved-co-uk casesolved-co-uk Apr 19, 2024

Choose a reason for hiding this comment

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

I disagree.

Consider this set of permissions:

Type DocType Role Perm
Standard Project Projects User proj1
Standard Project Projects Manager proj2
Standard Timesheet Projects Manager timesheet1
Custom Project Projects User proj3
Custom Project Projects Manager proj4
Custom Timesheet Employee timesheet2

If User only has Roles [Projects User, Projects Manager]:

Existing permissions results:

perms = proj1, proj2, timesheet1
initial custom_perms = proj3, proj4
doctypes_with_custom_perms = Project, Timesheet
result custom_perms = proj3, proj4

In other words standard permissions for Timesheet for all Users are removed simply by adding a Custom DocPerm to Timesheet for any role

My permission results:

perms = proj1, proj2, timesheet1
initial custom_perms = proj3, proj4
doctypes_with_custom_perms = Project
result custom_perms = proj3, proj4, timesheet1

Standard timesheet permissions are preserved.

The only way the current code works is if all permissions for a doctype are copied from standard when a custom docperm for that doctype is created.

My code also saves a database query.

Copy link
Member

Choose a reason for hiding this comment

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

This is the intended behaviour.

ankush added a commit to ankush/frappe that referenced this pull request Apr 18, 2024
ankush added a commit to ankush/frappe that referenced this pull request Apr 18, 2024
@ankush
Copy link
Member

ankush commented Apr 18, 2024

Added failing test case here: #26037

@ankush ankush added backport version-14-hotfix backport to version 14 backport version-15-hotfix Backport the PR to v15 and removed add-test-cases Add test case to validate fix or enhancement backport version-14-hotfix backport to version 14 backport version-15-hotfix Backport the PR to v15 labels Apr 18, 2024
@ankush ankush closed this Apr 18, 2024
mergify bot pushed a commit that referenced this pull request Apr 18, 2024
closes #26015

Extracted from #26018

(cherry picked from commit a1bb734)

# Conflicts:
#	frappe/tests/test_permissions.py
mergify bot pushed a commit that referenced this pull request Apr 18, 2024
closes #26015

Extracted from #26018

(cherry picked from commit a1bb734)
@casesolved-co-uk
Copy link
Contributor Author

Added failing test case here: #26037

Your added test case doesn't answer the docstring statement, because at no point does it add a Custom DocPerm

	def test_overrides_work_as_expected(self):
		"""custom docperms should completely override standard ones"""

@ankush
Copy link
Member

ankush commented Apr 21, 2024

Please read the code again.

ankush added a commit that referenced this pull request Apr 22, 2024
…26040)

* fix: filter select perm in get_doctypes_with_read

closes #26015

Extracted from #26018

(cherry picked from commit a1bb734)

* ci: ruff only fix imports

(cherry picked from commit 229fc71)

# Conflicts:
#	.pre-commit-config.yaml

* test: add test for custom docperm behaviour

(cherry picked from commit 3f707f1)

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
ankush added a commit that referenced this pull request Apr 30, 2024
…26039)

* fix: filter select perm in get_doctypes_with_read

closes #26015

Extracted from #26018

(cherry picked from commit a1bb734)

# Conflicts:
#	frappe/tests/test_permissions.py

* test: add test for custom docperm behaviour

(cherry picked from commit 3f707f1)

# Conflicts:
#	frappe/tests/test_permissions.py

* chore: conflicts

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

get_doctypes_with_custom_docperms: misuse in get_valid_perms due to no role check
2 participants