Skip to content

Fix a check for ancestor editor permissions at create#1629

Merged
jochenklar merged 14 commits into
2.4.5/releasefrom
2.4.5/fix/check-ancestor-editor-permissions
Jun 8, 2026
Merged

Fix a check for ancestor editor permissions at create#1629
jochenklar merged 14 commits into
2.4.5/releasefrom
2.4.5/fix/check-ancestor-editor-permissions

Conversation

@MyPyDavid

Copy link
Copy Markdown
Member

Description

Related issue: #1628

@MyPyDavid MyPyDavid self-assigned this May 29, 2026
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
@MyPyDavid MyPyDavid force-pushed the 2.4.5/fix/check-ancestor-editor-permissions branch from d6b4bd6 to 0e33c71 Compare May 29, 2026 10:35
@MyPyDavid MyPyDavid changed the base branch from main to 2.4.5/release May 29, 2026 10:35
…1628

Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>

@jochenklar jochenklar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please review and refactor before creating an PR, I will not review purely generated PRs. Also please provice an explanation how the change works and remove the placeholders.

}
response = client.post(url, data, content_type='application/json')
assert response.status_code == status_map['create'][username], response.json()
expected_status_code = status_map['create'][username]

@jochenklar jochenklar May 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pleas do not autogenerate tests without proper review. This check should be based on the user as in every other test and not inferred from the request. This clutters out tests and hinders maintainability.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it looked so nice for a second response.wsgi_request.user.has_perm(...) 🤤
Now I've added a new test cases to the status maps, 'create-with-parent'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use a better name, do not autogenerate tests without proper review.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, this one is removed now and a specific test test_*_rejects_foreign_site_parent is added to each viewset test module

Comment thread rdmo/core/serializers.py Outdated
if request is None:
return

for field_name, _source_name, _target_name, _through_name in parent_fields:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this not using get_parent_fields?

@MyPyDavid MyPyDavid Jun 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it needed to be refactored, and the permissions check should only check and not pop any fields, right?!
I'm now using self.get_parent_field_data(attrs) which is also re-used by self.get_parent_fields(validated_data)..

@coveralls

coveralls commented May 29, 2026

Copy link
Copy Markdown

Coverage Status

Coverage is 95.102%2.4.5/fix/check-ancestor-editor-permissions into 2.4.5/release. No base build found for 2.4.5/release.

@MyPyDavid

Copy link
Copy Markdown
Member Author

Thanks for the review! Yes, this PR was mostly AI-Engineered, it was still in Draft though.

@jochenklar

Copy link
Copy Markdown
Member

Ah sorry, good point. I need to check that more carefully.

@MyPyDavid MyPyDavid changed the title 2.4.5/fix/check ancestor editor permissions Draft: 2.4.5/fix/check ancestor editor permissions Jun 1, 2026
MyPyDavid added 6 commits June 1, 2026 13:18
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
…uery counts

Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
…parent test functions

Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Comment thread rdmo/core/tests/utils.py Outdated


def get_obj_perms_status_code(instance, username, method, editors=None):
def get_obj_perms_status_code(instance, username, action, editors=None):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this one was refactored just to make it clearer and more explicit what happens in there (since I could not follow it anymore) , this is actually a test "helper" and not a "util"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This whole file is much to complex for just setting up something to check against. Why not just prepare list or dicts or even inline the status_code in the tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, now, in the age (and with the help of) LLMs, I'll try to make it more explicit..

Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
@MyPyDavid MyPyDavid marked this pull request as ready for review June 2, 2026 08:36
@MyPyDavid MyPyDavid changed the title Draft: 2.4.5/fix/check ancestor editor permissions Fix a check for ancestor editor permissions at create Jun 2, 2026
@MyPyDavid

Copy link
Copy Markdown
Member Author

Did some human-in-the-loop and some necessary refactoring and now it's ready for review! 👓

@MyPyDavid MyPyDavid requested a review from jochenklar June 2, 2026 08:38
@MyPyDavid MyPyDavid added this to the RDMO 2.4.5 milestone Jun 2, 2026
Comment thread rdmo/core/permissions.py
@MyPyDavid

MyPyDavid commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

From discussion: maybe the permission check should go into a new separate ParentFieldValidator class instead of in the .validate() calls of the mixin..?
And possibly a test for the ('create-with-parent') OptionSets should be added as well..

@jochenklar jochenklar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, I think it works. I have some remarks about the tests, though.

Comment thread rdmo/core/tests/utils.py Outdated


def get_obj_perms_status_code(instance, username, method, editors=None):
def get_obj_perms_status_code(instance, username, action, editors=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This whole file is much to complex for just setting up something to check against. Why not just prepare list or dicts or even inline the status_code in the tests?

Comment thread rdmo/core/tests/utils.py Outdated
Comment thread rdmo/questions/tests/conftest.py Outdated
Comment thread rdmo/questions/tests/test_queries_questionset.py
site_settings('bar.com')
client.login(username='bar-editor', password='bar-editor')

instance = Page.objects.get(uri_path='foo-page')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is faulty. foo-page is read only for bar-editor anyway, so this does not test the implemented behaviour, right?

Also the section line should just be Section.objects.get(uri_path='foo-section')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

via the UI it should seem read only but for the API, bar-editor could post a page to the foo-section. That was the whole point of this fix right?!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I though that the problem was that the bar-editor could post a bar-question to a foo-page (which they don't have permissions on). The bar-editor should not be able to post anything to the foo-question, right? Or maybe I am missing something.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes exactly! Im trying to make the explicit multisite object permissions status code maps like so :

STATUS_CODES = {
    'detail': {
        'http://example.com/terms/questions/catalog': status_map['detail'],
        'http://example.com/terms/questions/catalog2': status_map['detail'],
        'https://foo.com/terms/questions/foo-catalog': {
            'user': 404, 'reviewer': 200, 'editor': 200,
            'example-reviewer': 200, 'example-editor': 200,
            'foo-user': 404, 'foo-reviewer': 200, 'foo-editor': 200,
            'bar-user': 404, 'bar-reviewer': 404, 'bar-editor': 404,
            'anonymous': 401
        },
        'https://bar.com/terms/questions/bar-catalog': {
            'user': 404, 'reviewer': 200, 'editor': 200,
            'example-reviewer': 200, 'example-editor': 200,
            'foo-user': 404, 'foo-reviewer': 404, 'foo-editor': 404,
            'bar-user': 404, 'bar-reviewer': 200, 'bar-editor': 200,
            'anonymous': 401
        },
    },
    ....
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I think this is much better. But then the test was faulty before right? Because bar-question/foo-page was not tested (or I missed it).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

the test before was more like bar-editor posting a bar-page that contained the foo-section

Comment thread rdmo/questions/tests/test_viewset_question_multisite.py Outdated
Comment thread rdmo/core/serializers.py
Comment thread rdmo/core/serializers.py
@jochenklar

Copy link
Copy Markdown
Member

And yes, we should ad a check for Option as well.

The validator is probably not worth it since the whole validation is strongly coupled to the serializer.

MyPyDavid added 3 commits June 3, 2026 11:14
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
@MyPyDavid

Copy link
Copy Markdown
Member Author

I've refactored/improved the tests and added test for the options as well. Would you approve these changes?

@MyPyDavid MyPyDavid requested a review from jochenklar June 3, 2026 11:14

@jochenklar jochenklar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I think I will check again tomorrow and then merge.

@jochenklar jochenklar merged commit 09daae7 into 2.4.5/release Jun 8, 2026
19 checks passed
@jochenklar jochenklar deleted the 2.4.5/fix/check-ancestor-editor-permissions branch June 8, 2026 07:53
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.

multisite: editor permissions do not check if the ancestor elements are editable or not

3 participants