PDA

View Full Version : Overriding all components' creation/render and disable some on some conditions



drunkmoose
16 Jul 2014, 12:11 PM
Hello

I'm trying to have a bit of a security system where I want certain components (buttons, menu items, tabs, etc.) disabled depending on some condition.

What's the best approach for this ?

I tried



Ext.define('Override.AbstractComponent', {
override: 'Ext.AbstractComponent', render: function (container, position) {
console.log(this.id);
this.callOverridden(arguments);
}
});


But this is not catching every component's render (namely buttons) even though button does seem to have a render method as per 4.2.1

Maybe it's because ExtJS does not call this method when displaying one in the browser.

Any idea what I could harness here ?

Thanks!

drunkmoose
16 Jul 2014, 1:37 PM
For the moment, this seems to do it:



Ext.define('Override.button.Button', {
override: 'Ext.Component',
constructor: function (config) {
this.callOverridden(arguments);
if (this.roleCategory != null && this.roleCategory != '') {
//some code here
}
}});


But unfortunately and obviously, this runs before the application is done polling the server for the user's information (such as roles and access level).Is there any to have the application wait until a certain Ajax call is done before kicking off the component's creation ?

drunkmoose
17 Jul 2014, 5:50 AM
I think I will use Ext.ComponentManager.each() after I'm done loading all the user's security profile properties.

drunkmoose
17 Jul 2014, 7:09 AM
Here what I have so far, maybe it can be of used to others:



Ext.define('Utils.Security', {
statics: {
setAllComponentStatus: function () {
Ext.ComponentManager.each(function (key, component, length) {
if (component.roleCategory != null) {
console.log(component.id);
this.setComponentStatus(component);
}
}, this);
},
setComponentStatus: function (component) {

var max = "0";
var category = component.roleCategory;
var level = component.roleLevel;
var categoryMatches = $.grep(SessionInfo.userRoles, function (e) {
if (e.RoleCategoryWebKey == category)
return e;
});

var isAdmin = $.grep(SessionInfo.userRoles, function (e) { return e.RoleWebKey == 'PortalAdmin'; }).length > 0;

if (categoryMatches != null) {
if (categoryMatches.length > 0) {

max = categoryMatches[0].RoleLevel;

for (var i = 0; i < categoryMatches.length; i++) {
if (categoryMatches[i].RoleLevel > max) {
max = categoryMatches[i].RoleLevel;
}
}
}
}

if (parseInt(max) >= parseInt(level) || isAdmin)
component.setDisabled(false);
else
component.setDisabled(true);

}
}
});



So any component with a roleCategory and roleLevel will be analyzed. Obviously, if you create a widget and show it (such as a window) you need to call this again as the window and its component were not present in ComponentManager previously.

BTW, the forum is very buggy. I have to edit my posts very often as what I see in the post is defaced VS what I was typing in the WYSIWYG editor...