Privileges.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cogadmin
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2011
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  11. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  12. //----------------------------------------------------------
  13. com.cognos.admin.ObjectFactory("com.cognos.admin.extension");
  14. /* This is a implementation of the extensive event handler. The impl need to have a method named "doAction" to
  15. * work as a mapping.
  16. * @param: env, context of the behaviour controller framework.
  17. *
  18. */
  19. com.cognos.admin.extension.Privileges = function (env) {
  20. this.env = env;
  21. this.meta = this.env.context.getPublicVariables("meta");
  22. this.state = this.env.state;
  23. this.privileges = this.state.getState("privileges");
  24. this.fragment = this.env.fragment;
  25. this.tools = com.cognos.admin.util.Tools;
  26. if (this.meta && this.privileges){
  27. this.populatePrivileges();
  28. }
  29. this.isDirty = false;
  30. };
  31. com.cognos.admin.extension.Privileges.VERSION = "0.1.0";
  32. com.cognos.admin.extension.Privileges.GRANT_ICON_NAME = "g";
  33. com.cognos.admin.extension.Privileges.DENY_ICON_NAME = "deny";
  34. com.cognos.admin.extension.Privileges.MIXED_ICON_NAME = "mixed";
  35. com.cognos.admin.extension.Privileges.DISPLAY_ICON_NAME = "grant";
  36. com.cognos.admin.extension.Privileges.NONE_ICON_NAME = "none";
  37. com.cognos.admin.extension.Privileges.prototype = {
  38. /*
  39. * init the privileges section, if any missing privilege, fill the gap with unset privilege
  40. */
  41. populatePrivileges : function () {
  42. for (var i=0; i<this.privileges.length; i++){
  43. var nameList = this.getItemList("name");
  44. if (this.privileges[i]["privilege"]){
  45. for (var j=0; j<this.privileges[i]["privilege"].length; j++){
  46. _F_Array.remove(nameList,this.privileges[i]["privilege"][j].name);
  47. }
  48. } else {
  49. this.privileges[i]["privilege"] = [];
  50. }
  51. this.privileges[i]["privilege"] = this.privileges[i]["privilege"].concat(this.getEmptyPrivilegeItem(nameList));
  52. }
  53. },
  54. getEmptyPrivilegeItem : function (name) {
  55. var ret = [];
  56. for (var i=0; i<name.length; i++){
  57. var obj = {};
  58. obj.name = name[i];
  59. obj.flag = "";
  60. ret.push(obj);
  61. }
  62. return ret;
  63. },
  64. /*
  65. * if lists is empty, execute the func anyway,
  66. * if lists is not empty, and the member is found in priviliges array, execute the func.
  67. */
  68. walkPrivileges : function (func,level,lists) {
  69. var ret = [];
  70. if (this.privileges){
  71. var level = level || 'privilege';
  72. for (var i=0; i<this.privileges.length; i++){
  73. if (lists && _F_Array.indexOf(lists,this.privileges[i]["key"]) == -1)
  74. continue;
  75. var key = this.privileges[i]["key"];
  76. if (level == 'key'){
  77. ret.push(func(key,i));
  78. } else {
  79. if (this.privileges[i]["privilege"]) {
  80. for (var j=0; j<this.privileges[i]["privilege"].length; j++){
  81. var current = this.privileges[i]["privilege"][j];
  82. if (level == 'privilege'){
  83. if (typeof func == "function"){
  84. ret.push(func(key,current));
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return ret;
  93. },
  94. getNumbers : function () {
  95. var nums = [];
  96. function getNum (key,current){
  97. if (!nums[current.name]){
  98. nums[current.name]={};
  99. nums[current.name][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y] = 0;
  100. nums[current.name][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N] = 0;
  101. }
  102. if (current.flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y){
  103. nums[current.name][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y]++;
  104. } else if (current.flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N){
  105. nums[current.name][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N]++;
  106. }
  107. };
  108. this.walkPrivileges(getNum,'privilege',this.lists);
  109. return nums;
  110. },
  111. show : function (tag,param){
  112. var fragId = this.fragment.id;
  113. var names = this.getItemList("name");
  114. var nums = this.getNumbers();
  115. for (var k=0; k<names.length; k++){
  116. var nId = this.tools.getNormalizedId (names[k],fragId);
  117. var elt = $(nId + "_" + com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y);
  118. var partner = $(nId + "_" + com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N);
  119. if (elt && partner){
  120. var cb = new com.cognos.admin.util.Tools.ThreeCheckbox(elt,fragId);
  121. var cbp = new com.cognos.admin.util.Tools.ThreeCheckbox(partner,fragId);
  122. if (this.lists.length == 0){
  123. cb.setState(com.cognos.admin.util.Tools.ThreeCheckbox.DISABLED);
  124. cbp.setState(com.cognos.admin.util.Tools.ThreeCheckbox.DISABLED);
  125. } else {
  126. cb.setState(com.cognos.admin.util.Tools.ThreeCheckbox.UNCHECKED);
  127. cbp.setState(com.cognos.admin.util.Tools.ThreeCheckbox.UNCHECKED);
  128. if (nums[names[k]]){
  129. if (nums[names[k]][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y] == this.lists.length){
  130. cb.setState(com.cognos.admin.util.Tools.ThreeCheckbox.CHECKED);
  131. } else if (nums[names[k]][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N] == this.lists.length){
  132. cbp.setState(com.cognos.admin.util.Tools.ThreeCheckbox.CHECKED);
  133. } else {
  134. if (nums[names[k]][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y] > 0){
  135. cb.setState(com.cognos.admin.util.Tools.ThreeCheckbox.MIXED);
  136. }
  137. if (nums[names[k]][com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N] > 0){
  138. cbp.setState(com.cognos.admin.util.Tools.ThreeCheckbox.MIXED);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. },
  146. /*
  147. * the payload will trace the route from root till you find the destination(s), and return all the items along this route.
  148. */
  149. walkItems : function (func,items,lists,payload,scope){
  150. var payl = [];
  151. if (items){
  152. for (var j=0; j<items.length; j++){
  153. if ((!scope || (items[j]["scope"] == scope)) && (!payload)){
  154. payl[j]=items[j];
  155. } else {
  156. payl[j]=payload;
  157. }
  158. if ((!scope || (items[j]["scope"] == scope)) && (!lists || (lists.length == 0) || (_F_Array.indexOf(lists,items[j]["name"]) != -1))){
  159. if (typeof func == "function"){
  160. func([payl[j]]);
  161. }
  162. } else if (items[j].meta){
  163. this.walkItems (func,items[j].meta,lists,payl[j],scope);
  164. }
  165. }
  166. }
  167. },
  168. getItemList : function (name,lists,scope) {
  169. var ret = [];
  170. if (this.meta){
  171. var items = this.meta;
  172. function getList (item){
  173. if (item){
  174. for (var i=0; i<item.length; i++){
  175. if (!name){
  176. ret.push (item[i]);
  177. } else {
  178. ret.push (item[i][name]);
  179. }
  180. if (item[i].meta){
  181. getList(item[i].meta);
  182. }
  183. }
  184. }
  185. };
  186. this.walkItems(getList,items,lists,"",scope);
  187. }
  188. return ret;
  189. },
  190. /*
  191. * lookfor: {key:xxx, value:xxx}
  192. */
  193. lookupItem : function (lookfor,name,lists,scope) {
  194. var ret = "";
  195. if (this.meta){
  196. var items = this.meta;
  197. function getItem (item){
  198. if (item){
  199. for (var i=0; i<item.length; i++){
  200. if (item[i][lookfor.key] == lookfor.value){
  201. ret = item[i][name];
  202. }
  203. if (item[i].meta){
  204. getItem(item[i].meta);
  205. }
  206. }
  207. }
  208. };
  209. this.walkItems(getItem,items,lists,"",scope);
  210. }
  211. return ret;
  212. },
  213. update : function (tag){
  214. if (!this.lists || this.lists.length == 0) return false;
  215. var fragId = this.fragment.id;
  216. var nId = this.tools.getNormalizedId(tag.id,fragId,true);
  217. var name = nId.substring(0,(nId.length-2));
  218. var flag = this.tools.getFlag(nId);
  219. var names = this.getItemList("name",[name]);
  220. var fullNames = this.getItemList("name","","package");
  221. if (name == "toggle_all"){
  222. this.updateEx(fullNames,flag,name,true);
  223. if (partner = this.tools.getPartner(tag,com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y,com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N)){
  224. if (tag.checked) partner.checked = !tag.checked;
  225. }
  226. } else {
  227. //privilige specific toggle rules #1 deny check toggler
  228. if (_F_Array.indexOf(names,name) == 0 && flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N && tag.getAttribute("state") != com.cognos.admin.util.Tools.ThreeCheckbox.CHECKED){
  229. this.updateEx(names,flag,names[0],true);
  230. } else {
  231. this.updateEx([name],flag,names[0],false);
  232. }
  233. }
  234. },
  235. updateEx : function (names,flag,toggleName,isToggled) {
  236. var self = this;
  237. var fragId = this.fragment.id;
  238. for (var i = 0; i < names.length; i++){
  239. var currentElt = $(this.tools.getNormalizedId(names[i] + "_" + flag,fragId));
  240. var isSelf = (names[i] == toggleName);
  241. var toggled = isToggled && !isSelf;
  242. this.updateRHS(names[i],flag,toggleName,toggled);
  243. function updateFlag (key,current){
  244. var isUpdated = false;
  245. if (current.name == names[i]){
  246. current.flag = (currentElt.getAttribute("state") == com.cognos.admin.util.Tools.ThreeCheckbox.CHECKED) ? flag : "";
  247. isUpdated = true;
  248. }
  249. //privilige specific toggle rules #2 deselect deny toggler, when children's stage changed
  250. if (!toggled && !isSelf && current.name == toggleName && current.flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N && !(flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N && currentElt.getAttribute("state") == com.cognos.admin.util.Tools.ThreeCheckbox.CHECKED)){
  251. current.flag = "";
  252. isUpdated = true;
  253. }
  254. if (isUpdated){
  255. self.updateLHS(key,names[i]);
  256. }
  257. };
  258. this.walkPrivileges (updateFlag,'privilege',this.lists);
  259. }
  260. },
  261. updateRHS : function (name,flag,togglerName,isToggled) {
  262. var fragId = this.fragment.id;
  263. var currentElt = $(this.tools.getNormalizedId(name + "_" + flag,fragId));
  264. var toggler = $(this.tools.getNormalizedId(togglerName + "_" + flag,fragId));
  265. var cb = new this.tools.ThreeCheckbox(currentElt,fragId,toggler);
  266. cb.setIsToggled(isToggled);
  267. cb.click();
  268. },
  269. updateLHS : function (key,name){
  270. var self = this;
  271. var fragId = this.fragment.id;
  272. var iconList = this.getItemList("icon",[name]);
  273. var nameList = this.getItemList("name",[name]);
  274. var scopeList = this.getItemList("scope",[name]);
  275. var flagList = [];
  276. var altText = "";
  277. var elt = $(fragId + "_" + key + "_" + nameList[0]);
  278. if (elt) {
  279. function getFlag (insideKey,current){
  280. if (key == insideKey && _F_Array.indexOf(nameList,current.name) > -1 && scopeList[_F_Array.indexOf(nameList,current.name)] == 'package'){
  281. flagList.push(current.flag);
  282. altText += self.getAltText(current.name,current.flag);
  283. }
  284. }
  285. this.walkPrivileges (getFlag,'privilege');
  286. elt.src = com.cognos.admin.WEBCONTENT + this.switchIcon(iconList[0],this.adjustFlag(flagList));
  287. dojo.attr(elt,"alt",altText);
  288. this.setIsDirty(true);
  289. }
  290. },
  291. getAltText: function(name,flag){
  292. var returnText = "";
  293. var caption = this.lookupItem({"key":"name","value":name},"caption",[name]);
  294. if (flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y){
  295. returnText = ADM.formatString(ADM.CAP.IDS_ADMNEW_CAP_GRANT,[caption+" "])+",";
  296. } else if (flag == com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N){
  297. returnText = ADM.formatString(ADM.CAP.IDS_ADMNEW_CAP_DENY,[caption+" "])+",";
  298. }
  299. return returnText;
  300. },
  301. switchIcon : function (initSrc,flag){
  302. if (!initSrc) throw new Error ("Cogadmin/Privilege: Icon's initial src cannot be empty.");
  303. var src = initSrc;
  304. var regOrigIcon = new RegExp("_" + com.cognos.admin.extension.Privileges.DISPLAY_ICON_NAME + ".gif","i");
  305. switch (flag){
  306. case com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y:
  307. src = src.replace(regOrigIcon, "_" + com.cognos.admin.extension.Privileges.GRANT_ICON_NAME + ".gif");
  308. break;
  309. case com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N:
  310. src = src.replace(regOrigIcon,"_" + com.cognos.admin.extension.Privileges.DENY_ICON_NAME + ".gif");
  311. break;
  312. case com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_M:
  313. src = src.replace(regOrigIcon,"_" + com.cognos.admin.extension.Privileges.MIXED_ICON_NAME + ".gif");
  314. break;
  315. default:
  316. var reg = new RegExp("access_(.)*_" + com.cognos.admin.extension.Privileges.DISPLAY_ICON_NAME + ".gif","i");
  317. src = src.replace(reg,"access_" + com.cognos.admin.extension.Privileges.NONE_ICON_NAME + ".gif");
  318. }
  319. return src;
  320. },
  321. getIsDirty : function () {return this.isDirty;},
  322. setIsDirty : function (flag) {
  323. this.isDirty = flag;
  324. this.state.setState("isDirty",this.isDirty);
  325. },
  326. adjustFlag : function (flags){
  327. var flag;
  328. if (flags) {
  329. for (var i=0; i<flags.length; i++){
  330. if (i==0){
  331. flag = flags[0];
  332. } else if (flags[i] != flags[i-1]){
  333. flag = com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_M;
  334. break;
  335. }
  336. }
  337. }
  338. return flag;
  339. },
  340. addEntries : function (entries){
  341. if (entries) {
  342. var isExist = false;
  343. for (var k=0; k<entries.length; k++){
  344. var isNew = true;
  345. function updateEntry (key,position){
  346. if (entries[k].key == key){
  347. isExist = true;
  348. isNew = false;
  349. }
  350. };
  351. this.walkPrivileges(updateEntry,'key');
  352. if (isNew){
  353. this.privileges.push(entries[k]);
  354. }
  355. }
  356. if (isExist){
  357. alert(ADM.CAP.IDS_ADMNEW_CAP_ALREADY_EXIST);
  358. }
  359. this.fragment.raiseEvent("fragment.refresh");
  360. }
  361. },
  362. getUnsetEntries : function (){
  363. var isUnset = [];
  364. var self = this;
  365. function checkUnset(key,position){
  366. isUnset[position] = true;
  367. if (privilege = self.privileges[position].privilege){
  368. for (var i=0; i<privilege.length; i++){
  369. if (privilege[i].flag){
  370. isUnset[position] = false;
  371. }
  372. }
  373. }
  374. }
  375. this.walkPrivileges(checkUnset,"key");
  376. if (com.cognos.admin.config.debug && _F_Array.indexOf(isUnset,true) > -1){
  377. _F_log("D","Unset Entries:");
  378. _F_log("D",this.privileges[_F_Array.indexOf(isUnset,true)].key)
  379. }
  380. return _F_Array.indexOf(isUnset,true) > -1;
  381. },
  382. removeUnsetEntries : function (){
  383. this.lists = [];
  384. var self = this;
  385. function checkUnset(key,position){
  386. if (privilege = self.privileges[position].privilege){
  387. var isUnset = true;
  388. for (var i=0; i<privilege.length; i++){
  389. if (privilege[i].flag){
  390. isUnset = false;
  391. break;
  392. }
  393. }
  394. if (isUnset) {
  395. self.lists[self.lists.length]=self.privileges[position].key;
  396. }
  397. }
  398. }
  399. this.walkPrivileges(checkUnset,"key");
  400. //remove
  401. var positions = [];
  402. function removeEntry (key,position){
  403. positions.push (position);
  404. }
  405. this.walkPrivileges(removeEntry,'key',this.lists);
  406. function numOrder (a,b){
  407. return b-a;
  408. }
  409. positions.sort(numOrder);
  410. for (var i=0; i<positions.length; i++){
  411. _F_Array.removeAt(this.privileges,positions[i]);
  412. }
  413. },
  414. removeEntries : function (){
  415. if (this.lists){
  416. var positions = [];
  417. function removeEntry (key,position){
  418. positions.push (position);
  419. }
  420. this.walkPrivileges(removeEntry,'key',this.lists);
  421. function numOrder (a,b){
  422. return b-a;
  423. }
  424. positions.sort(numOrder);
  425. for (var i=0; i<positions.length; i++){
  426. _F_Array.removeAt(this.privileges,positions[i]);
  427. }
  428. //remove the deleted items from the state "grpActionRows"
  429. while (this.lists.length>0){
  430. this.state.removeState(com.cognos.admin.util.ClientState.GROUP_ACTION_ROWS,this.lists[0],true);
  431. }
  432. this.fragment.raiseEvent("fragment.refresh");
  433. }
  434. },
  435. getTooltipParam : function (tag,currentName){
  436. if (tag.src.indexOf("access_none.gif") != -1) return;
  437. var grant = [];
  438. var deny = [];
  439. var blank = [];
  440. var ret = [];
  441. var list = this.getItemList("",[currentName]);
  442. var currentKey = this.tools.getKey(tag);
  443. for (var i=0; i<list.length; i++){
  444. var name = list[i].name;
  445. var self = this;
  446. function getP(key,current) {
  447. if (currentKey == key && current.name == name) {
  448. var obj = {};
  449. obj.text = list[i].caption;
  450. obj.image = self.switchIcon(list[i].icon,current.flag);
  451. if (list[i].scope == 'package') {
  452. switch (current.flag){
  453. case com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_Y:
  454. obj.caption = ADM.formatString(ADM.CAP.IDS_ADMNEW_CAP_GRANT,[""]);
  455. grant.push(obj);
  456. break;
  457. case com.cognos.admin.util.Tools.ThreeCheckbox.FLAG_N:
  458. obj.caption = ADM.formatString(ADM.CAP.IDS_ADMNEW_CAP_DENY,[""]);
  459. deny.push(obj);
  460. break;
  461. default:
  462. obj.caption = ADM.CAP.IDS_ADMNEW_CAP_NOT_SET;
  463. //in tooltips, we have to display the grant icon for NOT SET.
  464. obj.image = list[i].icon;
  465. blank.push(obj);
  466. }
  467. }
  468. }
  469. };
  470. this.walkPrivileges(getP,'privilege');
  471. }
  472. ret = ret.concat(grant,deny,blank);
  473. if (com.cognos.admin.config.debug){
  474. _F_log ("D","Get tool tip for privileges:");
  475. _F_log ("D",ret);
  476. }
  477. return ret;
  478. },
  479. /* behaviour controller framework interface implementation
  480. * @param: e, the event trigger this action
  481. * @param: tag, the target of the event
  482. * @param: param, "cogParam" passing from the markup.
  483. */
  484. doAction : function (e,tag,param) {
  485. this.lists = this.state.getState("grpActionRows",true);
  486. switch (param.actionName){
  487. case "update":
  488. return this.update(tag);
  489. case "removeEntries":
  490. return this.removeEntries();
  491. case "show":
  492. return this.show(tag,param);
  493. case "getTooltipParam":
  494. return this.getTooltipParam(tag,param.name);
  495. default:
  496. throw new Error ("Unknown action name, check your cogParam -> actionName.");
  497. return false; //not handled here
  498. }
  499. }
  500. };