From 7bc0d959075dbb986c19e1f93782a6395dd3c029 Mon Sep 17 00:00:00 2001 From: Teo Sarca Date: Thu, 2 Nov 2017 19:44:33 +0200 Subject: [PATCH] allow editing in background threads https://github.com/metasfresh/metasfresh-webui-api/issues/19 --- .../de/metas/ui/web/session/UserSession.java | 8 +++++++- .../controller/DocumentPermissionsHelper.java | 18 ++++++++++++------ .../web/window/model/DocumentCollection.java | 4 ++-- .../model/sql/SqlDocumentsRepository.java | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/metas/ui/web/session/UserSession.java b/src/main/java/de/metas/ui/web/session/UserSession.java index 652d278f4..fb84722de 100644 --- a/src/main/java/de/metas/ui/web/session/UserSession.java +++ b/src/main/java/de/metas/ui/web/session/UserSession.java @@ -70,7 +70,7 @@ public static UserSession getCurrentOrNull() // // Quickly check if the session scoped UserSession bean will be really available // NOTE: it's not about that the object will be null but if it's method calls will be really working - if (RequestContextHolder.getRequestAttributes() == null) + if (!isWebuiThread()) { return null; } @@ -134,6 +134,12 @@ public static IUserRolePermissions getCurrentPermissions() return getCurrent().getUserRolePermissions(); } + /** @return true if we are running in a webui thread (i.e. NOT a background daemon thread) */ + public static boolean isWebuiThread() + { + return RequestContextHolder.getRequestAttributes() != null; + } + // services static final transient Logger logger = LogManager.getLogger(UserSession.class); private final transient ApplicationEventPublisher eventPublisher; diff --git a/src/main/java/de/metas/ui/web/window/controller/DocumentPermissionsHelper.java b/src/main/java/de/metas/ui/web/window/controller/DocumentPermissionsHelper.java index cc762d8ba..70a75b2a6 100644 --- a/src/main/java/de/metas/ui/web/window/controller/DocumentPermissionsHelper.java +++ b/src/main/java/de/metas/ui/web/window/controller/DocumentPermissionsHelper.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import de.metas.logging.LogManager; +import de.metas.ui.web.session.UserSession; import de.metas.ui.web.window.datatypes.DocumentPath; import de.metas.ui.web.window.datatypes.DocumentType; import de.metas.ui.web.window.datatypes.WindowId; @@ -151,12 +152,17 @@ public static void assertCanView(@NonNull final Document document, @NonNull fina } } - /** - * Assets given document can be edited by given permissions - * - * @param document - * @param userSession - */ + public static void assertCanEdit(final Document document) + { + // If running from a background thread, consider it editable + if(!UserSession.isWebuiThread()) + { + return; + } + + assertCanEdit(document, UserSession.getCurrentPermissions()); + } + public static void assertCanEdit(final Document document, final IUserRolePermissions permissions) { final String errmsg = checkCanEdit(document, permissions); diff --git a/src/main/java/de/metas/ui/web/window/model/DocumentCollection.java b/src/main/java/de/metas/ui/web/window/model/DocumentCollection.java index 57f20672c..dab1f1439 100644 --- a/src/main/java/de/metas/ui/web/window/model/DocumentCollection.java +++ b/src/main/java/de/metas/ui/web/window/model/DocumentCollection.java @@ -236,7 +236,7 @@ else if (documentPath.isSingleNewIncludedDocument()) else { document = rootDocument.getIncludedDocument(documentPath.getDetailId(), documentPath.getSingleRowId()); - DocumentPermissionsHelper.assertCanEdit(rootDocument, UserSession.getCurrentPermissions()); + DocumentPermissionsHelper.assertCanEdit(rootDocument); } return documentProcessor.apply(document); @@ -277,7 +277,7 @@ public R forRootDocumentWritable(final DocumentPath documentPathOrNew, final .copy(CopyMode.CheckOutWritable, changesCollector) .refreshFromRepositoryIfStaled(); - DocumentPermissionsHelper.assertCanEdit(rootDocument, UserSession.getCurrentPermissions()); + DocumentPermissionsHelper.assertCanEdit(rootDocument); } // diff --git a/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java b/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java index cb019fbf8..ed0efccec 100644 --- a/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java +++ b/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java @@ -496,7 +496,7 @@ public void save(final Document document) { Services.get(ITrxManager.class).assertThreadInheritedTrxExists(); assertThisRepository(document.getEntityDescriptor()); - DocumentPermissionsHelper.assertCanEdit(document, UserSession.getCurrentPermissions()); + DocumentPermissionsHelper.assertCanEdit(document); // Runnables to be executed after the PO is saved final List afterSaveRunnables = new ArrayList<>();