Skip to content

Commit

Permalink
records: add parent access settings schema
Browse files Browse the repository at this point in the history
  • Loading branch information
max-moser authored and anikachurilova committed Jul 24, 2023
1 parent 6603c4c commit e106cbe
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 2 deletions.
20 changes: 20 additions & 0 deletions invenio_rdm_records/records/jsonschemas/records/parent-v3.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@
}
}
}
},

"settings": {
"description": "Access settings for the record.",
"type": "object",
"additionalProperties": false,
"properties": {
"allow_user_requests": {
"description": "Whether or not access requests are enabled for authenticated users.",
"type": "boolean"
},
"allow_guest_requests": {
"description": "Whether or not access requests are enabled for unauthenticated guests.",
"type": "boolean"
},
"accept_conditions_text": {
"description": "Custom description for access conditions to be displayed rather than the default text.",
"type": ["string", "null"]
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@
"type": "keyword"
}
}
},
"settings": {
"properties": {
"allow_user_requests": {
"type": "boolean"
},
"allow_guest_requests": {
"type": "boolean"
},
"accept_conditions_text": {
"type": "text"
}
}
}
}
},
Expand Down
50 changes: 48 additions & 2 deletions invenio_rdm_records/records/systemfields/access/field/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,59 @@

"""Access system field."""

from flask import current_app
from invenio_records.systemfields import SystemField

from ..grants import Grants
from ..links import Links
from ..owners import Owner


class AccessSettings:
"""Access settings for a parent record."""

def __init__(self, settings_dict):
"""Constructor."""
self.allow_user_requests = settings_dict.get("allow_user_requests", False)
self.allow_guest_requests = settings_dict.get("allow_guest_requests", False)
self.accept_conditions_text = settings_dict.get("accept_conditions_text", None)

def dump(self):
"""Dump the record as dictionary."""
return {
"allow_user_requests": self.allow_user_requests,
"allow_guest_requests": self.allow_guest_requests,
"accept_conditions_text": self.accept_conditions_text,
}

def __repr__(self):
"""Return repr(self)."""
return "<{} requests: {}/{}, text: {}>".format(
type(self).__name__,
self.allow_guest_requests,
self.allow_user_requests,
bool(self.accept_conditions_text),
)


class ParentRecordAccess:
"""Access management for all versions of a record."""

grant_cls = Grants
links_cls = Links
owner_cls = Owner
settings_cls = AccessSettings

def __init__(
self,
owned_by=None,
grants=None,
links=None,
owner_cls=None,
settings=None,
grants_cls=None,
links_cls=None,
settings_cls=None,
):
"""Create a new Access object for a record.
Expand All @@ -45,13 +76,15 @@ def __init__(
owner_cls = owner_cls or ParentRecordAccess.owner_cls
grants_cls = grants_cls or ParentRecordAccess.grant_cls
links_cls = links_cls or ParentRecordAccess.links_cls
settings_cls = settings_cls or ParentRecordAccess.settings_cls

# since owned_by and grants are basically sets and empty sets
# evaluate to False, assigning 'self.x = x or x_cls()' could lead to
# unwanted results
self._owned_by = owned_by if owned_by else owner_cls(None)
self.grants = grants if grants else grants_cls()
self.links = links if links else links_cls()
self.settings = settings if settings else settings_cls({})
self.errors = []

@property
Expand Down Expand Up @@ -80,6 +113,7 @@ def dump(self):
"owned_by": self._owned_by.dump(),
"links": self.links.dump(),
"grants": self.grants.dump(),
"settings": self.settings.dump(),
}

return access
Expand All @@ -91,6 +125,7 @@ def refresh_from_dict(self, access_dict):
self._owned_by = new_access.owned_by
self.grants = new_access.grants
self.links = new_access.links
self.settings = new_access.settings

@classmethod
def from_dict(
Expand All @@ -99,23 +134,27 @@ def from_dict(
owner_cls=None,
grants_cls=None,
links_cls=None,
settings_cls=None,
):
"""Create a new Access object from the specified 'access' property.
The new ``ParentRecordAccess`` object will be populated with new
instances from the configured classes.
If ``access_dict`` is empty, the ``ParentRecordAccess`` object will
be populated with new instances of ``grants_cls``, and ``links_cls``.
be populated with new instances of ``grants_cls``, ``links_cls``, and
``settings_cls``.
"""
grants_cls = grants_cls or cls.grant_cls
links_cls = links_cls or cls.links_cls
owner_cls = owner_cls or cls.owner_cls
settings_cls = settings_cls or cls.settings_cls
errors = []

# provide defaults in case there is no 'access' property
owner = owner_cls(None)
grants = grants_cls()
links = links_cls()
settings = settings_cls({})

if access_dict:
try:
Expand All @@ -135,21 +174,28 @@ def from_dict(
except Exception as e:
errors.append(e)

try:
settings = settings_cls(access_dict.get("settings", {}))
except Exception as e:
errors.append(e)

access = cls(
owned_by=owner,
grants=grants,
links=links,
settings=settings,
)
access.errors = errors
return access

def __repr__(self):
"""Return repr(self)."""
return ("<{} (owner: {}, grants: {}, links: {})>").format(
return "<{} (owner: {}, grants: {}, links: {}, settings: {})>".format(
type(self).__name__,
self.owner,
len(self.grants or []),
len(self.links or []),
self.settings,
)


Expand Down
12 changes: 12 additions & 0 deletions invenio_rdm_records/services/schemas/parent/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,21 @@ class Agent(Schema):
user = fields.Integer(required=True)


class AccessSettingsSchema(Schema):
"""Schema for a record's access settings."""

# enabling/disabling guests or users to send access requests
allow_user_requests = fields.Boolean()
allow_guest_requests = fields.Boolean()

# accept conditions text
accept_conditions_text = fields.String()


class ParentAccessSchema(Schema):
"""Access schema."""

grants = fields.List(fields.Nested(Grant))
owned_by = fields.Nested(Agent)
links = fields.List(fields.Nested(SecretLink))
settings = fields.Nested(AccessSettingsSchema)

0 comments on commit e106cbe

Please sign in to comment.