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

services: configure record access via config #1223

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions invenio_rdm_records/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def always_valid(identifier):
RDM_ALLOW_METADATA_ONLY_RECORDS = True
"""Allow users to publish metadata-only records."""

#
# Record access
#
RDM_ALLOW_RESTRICTED_RECORDS = True
"""Allow users to set restricted/private records."""

#
# Search configuration
#
Expand Down
23 changes: 21 additions & 2 deletions invenio_rdm_records/services/components/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from invenio_access.permissions import system_process
from invenio_drafts_resources.services.records.components import ServiceComponent
from invenio_i18n import gettext as _
from marshmallow import ValidationError


Expand All @@ -19,13 +20,31 @@ class AccessComponent(ServiceComponent):

def _populate_access_and_validate(self, identity, data, record, **kwargs):
"""Populate and validate the record's access field."""
errors = []
if record is not None and "access" in data:
access = data.get("access", {})

# Explicit permission check for modifying record access. This is inflexible,
# but generalizing management permissions per-field is difficult.
new_record_access = access.get("record")
if record.access.protection.record != new_record_access:
can_manage = self.service.check_permission(
identity, "manage_record_access"
)
if not can_manage:
# Set the data to what it was before
if "record" in access:
access["record"] = record.access.protection.record
errors.append(
_("You don't have permissions to manage record access.")
)

# populate the record's access field with the data already
# validated by marshmallow
record.update({"access": data.get("access")})
record.update({"access": access})
record.access.refresh_from_dict(record.get("access"))

errors = record.access.errors
errors.extend(record.access.errors)
if errors:
# filter out duplicate error messages
messages = list({str(e) for e in errors})
Expand Down
4 changes: 4 additions & 0 deletions invenio_rdm_records/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class RDMRecordPermissionPolicy(RecordPermissionPolicy):
can_manage_files = [
IfConfig("RDM_ALLOW_METADATA_ONLY_RECORDS", then_=can_review, else_=[]),
]
# Allow managing record access
can_manage_record_access = [
IfConfig("RDM_ALLOW_RESTRICTED_RECORDS", then_=can_review, else_=[]),
]

#
# PIDs
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def app_config(app_config):
# Communities
app_config["COMMUNITY_SERVICE_COMPONENTS"] = CommunityServiceComponents

# TODO: Remove when https://github.com/inveniosoftware/pytest-invenio/pull/95 is
# merged and released.
# Disable rate-limiting
app_config["RATELIMIT_ENABLED"] = False

return app_config


Expand Down
53 changes: 53 additions & 0 deletions tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,59 @@ def test_multiple_files_record(
assert response.status_code == 202


@pytest.fixture()
def restricted_records_disabled(app):
old_value = app.config.get("RDM_ALLOW_RESTRICTED_RECORDS", True)
app.config["RDM_ALLOW_RESTRICTED_RECORDS"] = False
yield
app.config["RDM_ALLOW_RESTRICTED_RECORDS"] = old_value


def test_restricted_records_disabled(
running_app,
client_with_login,
headers,
minimal_record,
search_clear,
superuser,
restricted_records_disabled,
):
client = client_with_login
response = client.post("/records", json=minimal_record, headers=headers)
recid = response.json["id"]

assert response.status_code == 201
assert response.json["access"]["record"] == "public"

# Trying to change record access to restricted will fail
minimal_record["access"]["record"] = "restricted"
response = client.put(
f"/records/{recid}/draft",
json=minimal_record,
headers=headers,
)
assert response.status_code == 400
assert response.json["errors"][0]["field"] == "access"
assert response.json["errors"][0]["messages"] == [
"You don't have permissions to manage record access.",
]
# Record access should still be "public"
response = client.get(f"/records/{recid}/draft", headers=headers)
assert response.json["access"]["record"] == "public"

# Superuser can change record access
superuser.login(client, logout_first=True)
minimal_record["access"]["record"] = "restricted"
response = client.put(
f"/records/{recid}/draft",
json=minimal_record,
headers=headers,
)
assert response.status_code == 200
assert response.json["access"]["record"] == "restricted"
assert "errors" not in response.json


# TODO
@pytest.mark.skip()
def test_create_publish_new_revision(
Expand Down