-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
has_permission check for link creation and class for combining multip… (
#227) * has_permission check for link creation and class for combining multiple conditions * has published record * has published record cjecl * allowing use of limited set of links for search results * links template class comes from service config * deprecated calling link condtions from .records * moved link conditions to services.config using & and | model of combining * file_service for file_record_class getter * list_files in presets * format * version bump * format and exception checking * type checking and correct exception message * Version bump --------- Co-authored-by: Ronald Krist <[email protected]> Co-authored-by: Mirek Simek <[email protected]>
- Loading branch information
1 parent
608713f
commit 69fc7a5
Showing
7 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from abc import abstractmethod | ||
|
||
from invenio_pidstore.errors import PIDDoesNotExistError, PIDUnregistered | ||
from invenio_records_resources.records.api import FileRecord | ||
from invenio_records.api import RecordBase | ||
from invenio_records_resources.records.api import Record | ||
from logging import getLogger | ||
from ...datastreams.utils import ( | ||
get_file_service_for_file_record_class, | ||
get_file_service_for_record_class, | ||
get_record_service_for_record, | ||
) | ||
log = getLogger(__name__) | ||
|
||
class Condition: | ||
|
||
@abstractmethod | ||
def __call__(self, obj, ctx: dict): | ||
raise NotImplementedError | ||
|
||
def __and__(self, other): | ||
return type( | ||
"CompositeCondition", | ||
(Condition,), | ||
{"__call__": lambda _, obj, ctx: self(obj, ctx) and other(obj, ctx)}, | ||
)() | ||
|
||
def __or__(self, other): | ||
return type( | ||
"CompositeCondition", | ||
(Condition,), | ||
{"__call__": lambda _, obj, ctx: self(obj, ctx) or other(obj, ctx)}, | ||
)() | ||
|
||
|
||
class is_published_record(Condition): | ||
"""Shortcut for links to determine if record is a published record.""" | ||
|
||
def __call__(self, obj: Record, ctx: dict): | ||
return not getattr(obj, "is_draft", False) | ||
|
||
|
||
class is_draft_record(Condition): | ||
"""Shortcut for links to determine if record is a draft record.""" | ||
|
||
def __call__(self, obj: Record, ctx: dict): | ||
return getattr(obj, "is_draft", False) | ||
|
||
|
||
class has_draft(Condition): | ||
"""Shortcut for links to determine if record is either a draft or a published one with a draft associated.""" | ||
|
||
def __call__(self, obj: Record, ctx: dict): | ||
if getattr(obj, "is_draft", False): | ||
return True | ||
if getattr(obj, "has_draft", False): | ||
return True | ||
return False | ||
|
||
|
||
class has_permission(Condition): | ||
def __init__(self, action_name): | ||
self.action_name = action_name | ||
|
||
def __call__(self, obj: RecordBase, ctx: dict): | ||
if isinstance(obj, FileRecord): | ||
obj = obj.record | ||
service = get_record_service_for_record(obj) | ||
try: | ||
return service.check_permission( | ||
action_name=self.action_name, record=obj, **ctx | ||
) | ||
except Exception as e: | ||
log.exception(f"Unexpected exception {e}.") | ||
|
||
|
||
|
||
class has_permission_file_service(has_permission): | ||
|
||
def __call__(self, obj: RecordBase, ctx: dict): | ||
if isinstance(obj, FileRecord): | ||
service = get_file_service_for_file_record_class(type(obj)) | ||
else: | ||
service = get_file_service_for_record_class(type(obj)) | ||
try: | ||
return service.check_permission( | ||
action_name=self.action_name, record=obj, **ctx | ||
) | ||
except Exception as e: | ||
log.exception(f"Unexpected exception {e}.") | ||
|
||
|
||
class has_published_record(Condition): | ||
|
||
def __call__(self, obj: Record, ctx: dict): | ||
service = get_record_service_for_record(obj) | ||
try: | ||
service.record_cls.pid.resolve(obj["id"]) | ||
except (PIDUnregistered, PIDDoesNotExistError): | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters