Hide button in section schema based on User Role

Hi Everyone,

I need to Hide button based on organizational role. I've used the following code but am not able to achieve the objective.

 

attributes:{
             "IsUserX": {
                    dataValueType: Terrasoft.DataValueType.BOOLEAN,
                    type: Terrasoft.ViewModelColumnType.VIRTUAL_COLUMN,
                    value: true
                },
        },

        onEntityInitialized: function() {
                    // Calling the parent implementation of the onEntityInitialized method.
                    this.callParent(arguments);
                    this.fnIsUserX();

            },
            getSectionActions:function(){
                var actionMenuItems = this.callParent(arguments);
                actionMenuItems.addItem(this.getButtonMenuItem({
                    Type:"Terrasoft.MenuSeparator",
                    Caption:""
                }));
                //actionMenuItems.addItem(this.getButtonMenuSeparator());
                actionMenuItems.addItem(this.getButtonMenuItem({
                                        "Tag":"XYZ",
                                        "Caption":"XYZ",
                                        "Click":{"bindTo":"ABC"},
                                        "Visible": {"bindTo": "IsUserX"},
                                        "Enabled": {"bindTo": "isCustomActionEnabled"},
                                        }));
                return actionMenuItems;
            },
            isCustomActionEnabled: function() {
                var selectedRows = this.get("SelectedRows");
                console.log(selectedRows);
                return selectedRows ? (selectedRows.length > 0 && selectedRows.length <= 50) : false;
            },
            fnIsUserX:function(){
                var currentuser = Terrasoft.SysValue.CURRENT_USER.value;
                var esqAdmin = this.Ext.create("Terrasoft.EntitySchemaQuery", {
                    rootSchemaName: "SysUserInRole"
                });
                esqAdmin.addColumn("SysRole");
                esqAdmin.addColumn("SysUser");
                var grpFilters = this.Ext.create("Terrasoft.FilterGroup");
                var filterUser = this.Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.EQUAL, "SysUser", currentuser);
                var filterRole = this.Terrasoft.createColumnFilterWithParameter(this.Terrasoft.ComparisonType.EQUAL, "SysRole.Name", "XYZ");
                grpFilters.addItem(filterUser);
                grpFilters.logicalOperation = this.Terrasoft.LogicalOperatorType.AND;
                grpFilters.addItem(filterRole);
                esqAdmin.filters.add(grpFilters);
                esqAdmin.getEntityCollection(function(result) {
                    if (!result.success) {
                        this.set("IsUserX",false);
                        return;
                    }
                    else {
                        var length = result.collection.collection.length;
                        if (length>0){
                            this.set("IsUserX", true);
                        }
                    }
                }, this);
            },

Like 0

Like

1 comments

Hi,

 

There is no onEntityInitialized method in the section page (it's available in the edit page only). If you need the button to be visible based on the user role you need to set the value for the "Visible" attribute in the button config (inside the actionMenuItems.addItem this.getButtonMenuItem function). You can use the example of the "Change log" button from the BaseDataView module (start seeking an example using this part of code - actionMenuItems.addItem(this.getObjectChangeLogSettingsMenuItem());) where operation permission is checked to display the button (and operation permissions are assigned to roles and this is what is needed in your task).

 

Best regards,

Oscar

Show all comments