Hello Luis,
This is a complicated case. The button is created inside the column’s renderer for each row. The UI constraint cannot get access to the component to make it hidden/visible.
I’ve create a GiHub issue to check permission for the button: Check role permissions for editButton in editorActionsColumn · Issue #4732 · jmix-framework/jmix · GitHub
You can try to set custom renderer to the column and check permission. For instance, for a resource role check:
@ViewComponent
private DataGrid<User> usersDataGrid;
@Autowired
private UiComponents uiComponents;
@Autowired
private AccessManager accessManager;
@Autowired
private Metadata metadata;
@Subscribe
public void onInit(final InitEvent event) {
DataGridEditor<User> editor = usersDataGrid.getEditor();
DataGridColumn<User> editorColumn = usersDataGrid.getColumnByKey("editorColumn");
if (editorColumn != null) {
editorColumn.setRenderer(new ComponentRenderer<>((item) -> {
if (!isPermittedByResourceRole(item)) {
return new Span();
}
Button editButton = uiComponents.create(JmixButton.class);
editButton.setIcon(VaadinIcon.EDIT.create());
editButton.addClickListener(__ -> {
if (editor.isOpen()) {
editor.cancel();
}
editor.editItem(item);
});
return editButton;
}));
}
}
private boolean isPermittedByResourceRole(User item) {
MetaClass metaClass = metadata.getClass(item);
UiEntityContext entityContext = new UiEntityContext(metaClass);
accessManager.applyRegisteredConstraints(entityContext);
return entityContext.isEditPermitted();
}
Upd.
Please note that you should also consider RowLevel roles if you have any in the application.
If you want to apply these changes globally, you should override the component and its loader DataGridLoader
. Then override the method that adds the column: AbstractGridLoader#createEditColumn()
.