This JavaScript returns an array of user roles (different from Liferay Roles) depends on the UserGroups the user is in.
/*
* Object fusionUI.getUserRoles(callback) returns user role. The getUserRoles function will pass the user
* roles in an array to the callback function.
*
* Usage: fusionUI.getUserRoles(callback);
* Example:
*
* function testCallback(comboItems) {
* for (var i in comboItems) {
* console.log(i + "=" + comboItems[i]);
* }
* }
*
* fusionUI.getUserRoles(testCallback);
*
*/
var fusionUI = {
agents: ['AGTUserGroup', 'HOUserGroup'],
admins: ['BOAUserGroup', 'ROAUserGroup', 'TOAUserGroup', 'RDUserGroup', 'HOAUserGroup'],
mangrs: ['SVPUserGroup', 'TVPUserGroup', 'BSMUserGroup', 'USMUserGroup', 'USVUserGroup', 'UFTUserGroup'],
getUserRoles: function (callback) {
Liferay.Service(
'/usergroup/get-user-user-groups', {
userId: themeDisplay.getUserId()
}, function (obj) {
var comboItems = [];
for (var i in obj) {
if ($.inArray(obj[i].name, fusionUI.agents) > -1) {
comboItems.push("Agents");
} else if ($.inArray(obj[i].name, fusionUI.mangrs) > -1) {
comboItems.push("Managers");
} else if ($.inArray(obj[i].name, fusionUI.admins) > -1) {
comboItems.push("Administrators");
}
}
callback(comboItems);
});
}
};
function testCallback(comboItems) {
for (var i in comboItems) {
console.log(i + "=" + comboItems[i]);
}
}
fusionUI.getUserRoles(testCallback);
Filed under: JavaScript, Liferay | Leave a Comment »