Skip to content

Commit

Permalink
Enforce read-only logic during creation/moving
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiefMaster committed Dec 10, 2024
1 parent 5f8567b commit dbca93b
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions global/indico_global/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from indico.core.errors import NoReportError
from indico.core.plugins import IndicoPlugin
from indico.modules.events import Event
from indico.modules.events.forms import EventCreationFormBase
from indico.modules.events.management.forms import CloneCategorySelectForm
from indico.web.flask.util import url_for
from indico.web.forms.base import IndicoForm
from indico.web.forms.widgets import SwitchWidget
Expand Down Expand Up @@ -115,6 +117,7 @@ def init(self):
super().init()
self.connect(signals.plugin.cli, self._extend_indico_cli)
self.connect(signals.rh.before_process, self._before_rh_process)
self.connect(signals.core.form_validated, self._event_creation_form_validated)
current_app.before_request(self._before_request)

def _extend_indico_cli(self, sender, **kwargs):
Expand All @@ -123,16 +126,38 @@ def _extend_indico_cli(self, sender, **kwargs):
def get_blueprints(self):
return blueprint

def _before_rh_process(self, rh_cls, rh):
def _event_creation_form_validated(self, form, **kwargs):
match form:
case EventCreationFormBase():
if not form.listing.data:
return
case CloneCategorySelectForm():
# cannot clone to unlisted atm, so nothing special to do here yet
pass
case _:
return
if not self.settings.get('read_only') or (global_id := self.settings.get('global_category_id')) is None:
return
if global_id not in form.category.data.chain_ids:
return
form.category.errors.append(self.settings.get('read_only_msg') or 'This category is read-only.')
return False

if event := getattr(rh, 'event', None):
category = event.category
else:
category = getattr(rh, 'category', None)
def _before_rh_process(self, rh_cls, rh, **kwargs):
if not self.settings.get('read_only') or (global_id := self.settings.get('global_category_id')) is None:
return

categories = set()
if (event := getattr(rh, 'event', None)) and event.category:
categories.add(event.category)
elif category := getattr(rh, 'category', None):
categories.add(category)

# moving events/categories
if target_category := getattr(rh, 'target_category', None):
categories.add(target_category)

if not category or global_id not in category.chain_ids:
if not any(global_id in cat.chain_ids for cat in categories):
return

if (msg := self.settings.get('read_only_msg')) and _is_request_likely_seen():
Expand Down

0 comments on commit dbca93b

Please sign in to comment.