Skip to content

Commit 26225f3

Browse files
committed
global: make moderation locking configurable
1 parent 4332d26 commit 26225f3

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

invenio_users_resources/config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@
124124
USERS_RESOURCES_ADMINISTRATION_ENABLED = True
125125
"""Enable the user administration"""
126126

127+
USERS_RESOURCES_USE_MODERATION_LOCK = True
128+
"""Enable or disable using a per-user lock for moderation actions."""
129+
127130
USERS_RESOURCES_MODERATION_LOCK_DEFAULT_TIMEOUT = 30
128131
"""Default timeout, in seconds, to lock a user when moderating."""
129132

130133
USERS_RESOURCES_MODERATION_LOCK_RENEWAL_TIMEOUT = 120
131134
"""Renewal timeout, in seconds, to increase the lock time for a user when moderating."""
132135

133-
134136
USERS_RESOURCES_DOMAINS_SEARCH = {
135137
"sort": [
136138
"bestmatch",

invenio_users_resources/services/users/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class UsersServiceConfig(RecordServiceConfig, ConfiguratorMixin):
174174
# leaking account information.
175175
search = UserSearchOptions
176176

177+
# Moderation lock
178+
use_moderation_lock = FromConfig("USERS_RESOURCES_USE_MODERATION_LOCK", True)
179+
177180
# For admin user
178181
search_all = FromConfigSearchOptions(
179182
"USERS_RESOURCES_SEARCH",

invenio_users_resources/services/users/service.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def block(self, identity, id_, uow=None):
142142
raise ValidationError("User is already blocked.")
143143

144144
# Throws if not acquired
145-
ModerationMutex(id_).acquire()
145+
self._acquire_moderation_lock(id_)
146+
146147
user.block()
147148
uow.register(RecordCommitOp(user, indexer=self.indexer, index_refresh=True))
148149

@@ -166,7 +167,8 @@ def restore(self, identity, id_, uow=None):
166167
raise ValidationError("User is not blocked.")
167168

168169
# Throws if not acquired
169-
ModerationMutex(id_).acquire()
170+
self._acquire_moderation_lock(id_)
171+
170172
user.activate()
171173
# User is blocked from now on, "after" actions are executed separately.
172174
uow.register(RecordCommitOp(user, indexer=self.indexer, index_refresh=True))
@@ -177,6 +179,11 @@ def restore(self, identity, id_, uow=None):
177179
)
178180
return True
179181

182+
def _acquire_moderation_lock(self, user_id):
183+
"""Acquire the moderation lock for a specific user."""
184+
if self.config.use_moderation_lock:
185+
ModerationMutex(user_id).acquire()
186+
180187
@unit_of_work()
181188
def approve(self, identity, id_, uow=None):
182189
"""Approves a user."""
@@ -191,7 +198,8 @@ def approve(self, identity, id_, uow=None):
191198
raise ValidationError("User is already verified.")
192199

193200
# Throws if not acquired
194-
ModerationMutex(id_).acquire()
201+
self._acquire_moderation_lock(id_)
202+
195203
user.verify()
196204
uow.register(RecordCommitOp(user, indexer=self.indexer, index_refresh=True))
197205

invenio_users_resources/services/users/tasks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
"""Users service tasks."""
1111

12+
from contextlib import ExitStack
13+
1214
from celery import shared_task
1315
from flask import current_app
1416
from invenio_records_resources.services.uow import UnitOfWork
@@ -78,8 +80,10 @@ def execute_moderation_actions(user_id=None, action=None):
7880
"""
7981
actions = current_actions_registry.get(action, [])
8082

81-
with ModerationMutex(user_id) as lock:
82-
lock.acquire_or_renew(renewal_timeout)
83+
with ExitStack() as stack:
84+
if current_users_service.config.use_moderation_lock:
85+
lock = stack.enter_context(ModerationMutex(user_id))
86+
lock.acquire_or_renew(renewal_timeout)
8387

8488
# Create a uow that is shared by all the callables
8589
uow = UnitOfWork()

0 commit comments

Comments
 (0)