commit 9c239e1b6f406e7fab637657a12e9682c2754646
Author: Andreas Kohlbecker <a.kohlbecker@bgbm.org>
Date:   Fri Apr 20 21:49:08 2018 +0200

    fix #7359 CdmEditorPresenters check user permissions to set the editor to readoly if persission is insufficient
     - also removing bug by which per-entity-permissions where granted to users when opening an editor

diff --git a/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java b/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java
index 78ca14f9..5657d384 100644
--- a/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java
+++ b/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmEditorPresenter.java
@@ -21,6 +21,7 @@ import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
 import eu.etaxonomy.cdm.debug.PersistentContextAnalyzer;
 import eu.etaxonomy.cdm.model.ICdmCacher;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
 import eu.etaxonomy.cdm.service.CdmStore;
@@ -79,7 +80,6 @@ public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase, V extends
             UUID uuidIdentifier = (UUID)identifier;
             // CdmAuthority is needed before the bean is loaded into the session.
             // otherwise adding the authority to the user would cause a flush
-            guaranteePerEntityCRUDPermissions(uuidIdentifier);
             cdmEntitiy = loadCdmEntity(uuidIdentifier);
         } else {
             cdmEntitiy = loadCdmEntity(null);
@@ -87,8 +87,7 @@ public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase, V extends
                 guaranteePerEntityCRUDPermissions(cdmEntitiy);
             }
         }
-
-
+        adaptToUserPermission(cdmEntitiy);
         cache = new CdmTransientEntityCacher(this);
         // need to use load but put see #7214
         cdmEntitiy = cache.load(cdmEntitiy);
@@ -97,6 +96,30 @@ public abstract class AbstractCdmEditorPresenter<DTO extends CdmBase, V extends
         return cdmEntitiy;
     }
 
+    /**
+     * @param cdmEntitiy
+     */
+    private void adaptToUserPermission(DTO cdmEntitiy) {
+        UserHelper userHelper = UserHelper.fromSession();
+        boolean canDelte = userHelper.userHasPermission(cdmEntitiy, CRUD.DELETE);
+        boolean canEdit = userHelper.userHasPermission(cdmEntitiy, CRUD.UPDATE);
+
+        User user = userHelper.user();
+
+        if(AbstractCdmPopupEditor.class.isAssignableFrom(getView().getClass())){
+            AbstractCdmPopupEditor popupView = ((AbstractCdmPopupEditor)getView());
+
+            if(!canEdit){
+                popupView.setReadOnly(true); // never reset true to false here!
+                logger.debug("setting editor to readonly");
+            }
+            if(!canDelte){
+                popupView.withDeleteButton(false);
+                logger.debug("removing delete button");
+            }
+        }
+
+    }
 
     /**
      * @param identifier
diff --git a/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java b/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java
index 89e598a6..7b70b514 100644
--- a/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java
+++ b/src/main/java/eu/etaxonomy/vaadin/mvp/AbstractCdmPopupEditor.java
@@ -15,6 +15,7 @@ import java.util.List;
 
 import com.vaadin.server.FontAwesome;
 import com.vaadin.shared.ui.MarginInfo;
+import com.vaadin.ui.AbstractComponentContainer;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Layout;
@@ -104,7 +105,20 @@ public abstract class AbstractCdmPopupEditor<DTO extends CdmBase, P extends Abst
     @Override
     public void setReadOnly(boolean readOnly) {
         super.setReadOnly(readOnly);
-        getFieldLayout().iterator().forEachRemaining(c -> c.setReadOnly(readOnly));
+        recursiveReadonly(readOnly, (AbstractComponentContainer)getFieldLayout());
+    }
+
+    /**
+     * @param readOnly
+     * @param layout
+     */
+    protected void recursiveReadonly(boolean readOnly, AbstractComponentContainer layout) {
+        for(Component c : layout){
+            c.setReadOnly(readOnly);
+            if(c instanceof AbstractComponentContainer){
+                recursiveReadonly(readOnly, layout);
+            }
+        }
     }
 
 
