Skip to content

Commit

Permalink
search_param: refactor SharedOrMyDraftsParam
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Mar 4, 2025
1 parent 13cff1f commit cd9e6ab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class CommunityHeaderComponent extends Component {
} = this.props;
const { modalOpen } = this.state;

// record is coming from the Jinja template and it is refreshed on page reload
const isNewUpload = !record.id;
// Check if the user can manage the record only if it is not a new upload
const isCommunitySelectionDisabled =
(!isNewUpload && !userCanManageRecord) || disableCommunitySelectionButton;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ class SubmitReviewButtonComponent extends Component {
};

isDisabled = (disableSubmitForReviewButton, filesState) => {
const { formik, userCanManageRecord } = this.props;
const { formik, userCanManageRecord, record } = this.props;
const { values, isSubmitting } = formik;

if (!userCanManageRecord || disableSubmitForReviewButton || isSubmitting) {
const isNewUpload = !record.id;
// Check if the user can manage the record only if it is not a new upload
if (
(!isNewUpload && !userCanManageRecord) ||
disableSubmitForReviewButton ||
isSubmitting
) {
return true;
}

Expand Down Expand Up @@ -132,6 +138,7 @@ SubmitReviewButtonComponent.propTypes = {
publishModalExtraContent: PropTypes.string,
filesState: PropTypes.object,
userCanManageRecord: PropTypes.bool.isRequired,
record: PropTypes.object.isRequired,
};

SubmitReviewButtonComponent.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SubmitReviewOrPublishComponent extends Component {
{...ui}
fluid
className="mb-10"
record={record}
/>
);
} else if (showChangeCommunityButton) {
Expand Down
41 changes: 8 additions & 33 deletions invenio_rdm_records/services/search_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from invenio_access.permissions import authenticated_user
from invenio_records_resources.services.records.params.base import ParamInterpreter

from invenio_rdm_records.records.systemfields.access.grants import Grant
from invenio_rdm_records.services.generators import AccessGrant
from invenio_rdm_records.records.systemfields.deletion_status import (
RecordDeletionStatusEnum,
)
Expand Down Expand Up @@ -51,32 +51,12 @@ class SharedOrMyDraftsParam(ParamInterpreter):
Returns only drafts owned by the user or shared with the user via grant subject user or role.
"""

def _make_grant_token(self, subj_type, subj_id, permission):
"""Create a grant token from the specified parts."""
# NOTE: `Grant.to_token()` doesn't need the actual subject to be set
return Grant(
subject=None,
origin=None,
permission=permission,
subject_type=subj_type,
subject_id=subj_id,
).to_token()

def _grant_tokens(self, identity, permissions):
"""Parse a list of grant tokens provided by the given identity."""
def _generate_shared_with_me_query(self, identity):
"""Generate the shared_with_me query."""
tokens = []
for _permission in permissions:
for need in identity.provides:
token = None
if need.method == "id":
token = self._make_grant_token("user", need.value, _permission)
elif need.method == "role":
token = self._make_grant_token("role", need.value, _permission)

if token is not None:
tokens.append(token)

return tokens
for _permission in ["preview", "edit", "manage"]:
tokens.extend(AccessGrant(_permission)._grant_tokens(identity))
return dsl.Q("terms", **{"parent.access.grant_tokens": tokens})

def apply(self, identity, search, params):
"""Evaluate the include_deleted parameter on the search."""
Expand All @@ -90,13 +70,8 @@ def is_user_authenticated():
if value is None and is_user_authenticated():
if params.get("shared_with_me") is True:
# Shared with me
tokens = self._grant_tokens(
identity, permissions=["preview", "edit", "manage"]
)
shared_with_me = dsl.Q(
"terms", **{"parent.access.grant_tokens": tokens}
)
return search.filter(shared_with_me)
shared_with_me = self._generate_shared_with_me_query(identity)
search = search.filter(shared_with_me)
elif params.get("shared_with_me") is False:
# My uploads
my_uploads = dsl.Q(
Expand Down

0 comments on commit cd9e6ab

Please sign in to comment.