Skip to content

Commit

Permalink
communities_ui: views: Add RecordPermissionDeniedError Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamarora1 authored and zzacharo committed Nov 25, 2024
1 parent fbe030e commit 633ac13
Showing 1 changed file with 12 additions and 41 deletions.
53 changes: 12 additions & 41 deletions invenio_app_rdm/communities_ui/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
UICommunityJSONSerializer,
)
from invenio_communities.errors import CommunityDeletedError
from invenio_communities.views.ui import (
not_found_error,
record_permission_denied_error,
record_tombstone_error,
)
from invenio_i18n import lazy_gettext as _
from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError
from invenio_rdm_records.collections import search_app_context as c_search_app_context
from invenio_records_resources.services.errors import PermissionDeniedError
from invenio_records_resources.services.errors import (
PermissionDeniedError,
RecordPermissionDeniedError,
)

from ..searchapp import search_app_context
from .communities import (
Expand All @@ -30,46 +38,6 @@
)


#
# Error handlers
#
def not_found_error(error):
"""Handler for 'Not Found' errors."""
return render_template(current_app.config["THEME_404_TEMPLATE"]), 404


def record_tombstone_error(error):
"""Tombstone page."""
# the RecordDeletedError will have the following properties,
# while the PIDDeletedError won't
record = getattr(error, "record", None)
if (record_ui := getattr(error, "result_item", None)) is not None:
if record is None:
record = record_ui._record
record_ui = UICommunityJSONSerializer().dump_obj(record_ui.to_dict())

# render a 404 page if the tombstone isn't visible
if not record.tombstone.is_visible:
return not_found_error(error)

# we only render a tombstone page if there is a record with a visible tombstone
return (
render_template(
"invenio_communities/tombstone.html",
record=record_ui,
),
410,
)


def record_permission_denied_error(error):
"""Handle permission denier error on record views."""
if not current_user.is_authenticated:
# trigger the flask-login unauthorized handler
return current_app.login_manager.unauthorized()
return render_template(current_app.config["THEME_403_TEMPLATE"]), 403


def _show_browse_page():
"""Whether the browse page should be visible in the menu."""
return (
Expand Down Expand Up @@ -118,6 +86,9 @@ def create_ui_blueprint(app):
blueprint.register_error_handler(
PermissionDeniedError, record_permission_denied_error
)
blueprint.register_error_handler(
RecordPermissionDeniedError, record_permission_denied_error
)
blueprint.register_error_handler(PIDDeletedError, record_tombstone_error)
blueprint.register_error_handler(CommunityDeletedError, record_tombstone_error)
blueprint.register_error_handler(PIDDoesNotExistError, not_found_error)
Expand Down

0 comments on commit 633ac13

Please sign in to comment.