Skip to content

Commit

Permalink
allow editing in background threads
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 2, 2017
1 parent 44f08fa commit 7bc0d95
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/main/java/de/metas/ui/web/session/UserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -277,7 +277,7 @@ public <R> R forRootDocumentWritable(final DocumentPath documentPathOrNew, final
.copy(CopyMode.CheckOutWritable, changesCollector)
.refreshFromRepositoryIfStaled();

DocumentPermissionsHelper.assertCanEdit(rootDocument, UserSession.getCurrentPermissions());
DocumentPermissionsHelper.assertCanEdit(rootDocument);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runnable> afterSaveRunnables = new ArrayList<>();
Expand Down

0 comments on commit 7bc0d95

Please sign in to comment.