/* ----------------------------------------------- */ /* -- Disable Hardware Asset Fields by AD Group -- */ /* ----------------------------------------------- */ // v1.0.1 // Tested for: Cireson Portal v4.0.4 // Contributors: Joivan Hedrick // Description: This module will allow you to only allow certain AD groups within the Cireson Asset Management group to make changes to // hardware assets. if a user is not in a specified allowed AD group within this module, then all hardware asset forms that // they are allowed to access will be read-only. $(function(){ if (document.URL.indexOf("/AssetManagement/HardwareAsset") === -1) { //Only worry about the hardware asset form. return; } if (session.user.IsAdmin === true) { //If this is an admin, then return with an unmodified form. return; } //These AD groups, which must exist in the ServiceManagement DB, are allowed to use an unmodified form var targetAdGroups = [ "fnSystemCenterTeam", "fnBuildTeam" //Add more AD groups here! ]; fn_DisableFormForInvalidUsers(pageForm.viewModel, targetAdGroups, session.user.Id); function fn_DisableFormForInvalidUsers(thisViewModel, thisTargetAdGroups, userId) { $.ajax({ url: "/api/V3/User/GetUsersGroups/" + userId, type: "GET", success: function (data) { var userGroupsFromServer = data; var found = false; for(var i=0; i < userGroupsFromServer.length; i++){ for(var j=0; j < thisTargetAdGroups.length; j++){ if(userGroupsFromServer[i].UserName === thisTargetAdGroups[j] ){ //console.log("User is a member of '" + userGroupsFromServer[i].UserName + "'. Form is unmodified."); found = true; break; } } if(found){ break; } } if(!found){ //console.log("Member was not a member found in the list of AD groups. Form will be read-only."); fn_DisableInputFields(); } } }); function fn_DisableInputFields () { //change the CSS rules globally (before page load) so that our tasks don't show since they load by js after the page loads. //This allows us to not use _delay here. var customJsStyleSheet; for(var i=0; i < document.styleSheets.length; i++){ if (document.styleSheets[i].href.indexOf("CustomSpace/custom.css") > -1) { customJsStyleSheet = document.styleSheets[i]; break; } } if (customJsStyleSheet !== undefined) { //Our custom.js exists. So add a rule to it. fn_AddCssRule(customJsStyleSheet, ".task-panel", "display: none "); fn_AddCssRule(customJsStyleSheet, ".dropdowntree-button", "display: none "); fn_AddCssRule(customJsStyleSheet, ".k-numeric-wrap", "padding: 0 "); fn_AddCssRule(customJsStyleSheet, ".k-grid-delete", "display: none "); fn_AddCssRule(customJsStyleSheet, "button", "padding: 0 "); } //lastly, disable the buttons manually. We can't just change the global css because we need to keep some buttons enabled. _.delay(function () { //Disable the input fields. $('input').prop('disabled', true); //Disable all textareas. $('textarea').prop('disabled', true); //Some controls need element-style modifications. $('.k-select').css('display', "none"); $('.searchIcon').css('display', "none"); $('.k-input').css('padding', "0"); $('.k-dropdown-wrap').css('padding', "0"); $('.input-userpicker').css('padding', "0"); $('.k-picker-wrap').css('padding', "0"); var allButtons = document.querySelectorAll('[data-role=button]'); for(var i=0; i < allButtons.length; i++){ var buttonTextContent = allButtons[i].textContent; if (buttonTextContent === "Show History") { continue; } $(allButtons[i]).prop('disabled', true); $(allButtons[i]).css("display", "none"); } }, 1000); } function fn_AddCssRule(styleSheet, elementObject, cssStyle) { if (styleSheet.rules) { //customJsStyleSheet.addRule(".task-panel", "display: none"); styleSheet.addRule(elementObject, cssStyle); //For Internet Explorer } else { var ruleIndex = styleSheet.cssRules.length; //styleSheet.insertRule('.taskmenu { display: none ; }', ruleIndex); styleSheet.insertRule(elementObject + ' { ' + cssStyle + '; }', ruleIndex); // For Firefox, Chrome, etc. } } } }); /* ----------------------------------------------- */ /* -End Disable Hardware Asset Fields by AD Group- */ /* ----------------------------------------------- */