How to hide inline edit button in datagrid for a specific role

Hi JMIX team,

We are using version 2.6.1 and also the addon UI Constraint to hide or disable specific buttons, fields, tabs depending on the user role. This addon combine with the standard authorization is very powerful and allows to make almost any change without coding.

I would like to ask if there is any option to hide the standard edit button in a inline editor datagrid without coding, or coding is the only option, if this is the case would be nice to included in future releases.

image

Thank you

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().