Capabilities.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. // *
  9. // * Capabilities.sln
  10. // *
  11. // * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. // * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. // *
  14. // * Description: This sample illustrates how to manipulate capabilities.
  15. // *
  16. // * Input: Search Path to a capabilities object. Possible values are:
  17. // * Name Search Path
  18. // * Administration /capability/securedFunction[@name='Administration']
  19. // * Query Studio /capability/securedFunction[@name='Query Studio']
  20. // * Report Studio /capability/securedFunction[@name='Report Studio']
  21. // * SDK /capability/securedFunction[@name='SDK']
  22. // *
  23. // *
  24. // * Outcome: Adds or removes the current user from the capabilities/Administration user list.
  25. // *
  26. // *
  27. using System;
  28. using System.Web.Services.Protocols;
  29. using SamplesCommon;
  30. using cognosdotnet_10_2;
  31. namespace Capabilities
  32. {
  33. /// <summary>
  34. /// Summary description for Capabilities.
  35. /// </summary>
  36. class Capabilities
  37. {
  38. /// <summary>
  39. /// The main entry point for the application.
  40. /// </summary>
  41. ///
  42. public Capabilities(){}
  43. static void Main(string[] args)
  44. {
  45. string cBIUrl = "";
  46. contentManagerService1 cmService = null;
  47. CapabilitiesDlg capabilitiesDlgObject = null;
  48. SamplesConnect connectDlg = new SamplesConnect();
  49. if (args.GetLength(0) == 0 )
  50. {
  51. // GUI mode
  52. connectDlg.ShowDialog();
  53. if (connectDlg.IsConnectedToCBI() == true)
  54. {
  55. cmService = connectDlg.CBICMS;
  56. cBIUrl = connectDlg.CBIURL;
  57. capabilitiesDlgObject = new CapabilitiesDlg();
  58. capabilitiesDlgObject.setConnection(cmService, cBIUrl);
  59. capabilitiesDlgObject.ShowDialog();
  60. }
  61. }
  62. }
  63. public bool setCapabilities(contentManagerService1 cBICMS, ref string resultMessage)
  64. {
  65. string secFuncPath = "/capability/securedFunction[@name='Administration']";
  66. account myAccount = getLogonAccount(cBICMS);
  67. baseClass[] results = new baseClass[0];
  68. securedFunction securedFunction = null;
  69. searchPathMultipleObject cmTargetPath = new searchPathMultipleObject();
  70. cmTargetPath.Value = secFuncPath;
  71. results =
  72. cBICMS.query(
  73. cmTargetPath,
  74. new propEnum[] {
  75. propEnum.searchPath,
  76. propEnum.policies,
  77. propEnum.defaultName },
  78. new sort[] {},
  79. new queryOptions());
  80. securedFunction = (securedFunction)results[0];
  81. policy pol;
  82. int numPolicies = securedFunction.policies.value.GetLength(0);
  83. policy[] tmpPolicies = new policy[numPolicies + 1];
  84. policy[] newPolicies;
  85. int j = 0;
  86. for (int i = 0; i < numPolicies; i++)
  87. {
  88. pol = securedFunction.policies.value[i];
  89. String polSecPath =
  90. pol.securityObject.searchPath.value;
  91. if (0 != polSecPath.CompareTo(myAccount.searchPath.value))
  92. {
  93. tmpPolicies[j++] = pol;
  94. }
  95. }
  96. if (j < numPolicies)
  97. {
  98. newPolicies = new policy[j];
  99. for (int i = 0; i < j; i++)
  100. {
  101. newPolicies[i] = tmpPolicies[i];
  102. }
  103. }
  104. else
  105. {
  106. policy newPolicy = null;
  107. for (int i = 0; i < myAccount.policies.value.GetLength(0); i++)
  108. {
  109. if (-1 != myAccount.policies.value[i].securityObject.ToString().IndexOf("account"))
  110. {
  111. newPolicy = myAccount.policies.value[i];
  112. }
  113. }
  114. newPolicies = tmpPolicies;
  115. newPolicies[j] = newPolicy;
  116. }
  117. policyArrayProp policyPropForUpdate = new policyArrayProp();
  118. policyPropForUpdate.value = newPolicies;
  119. securedFunction.policies = policyPropForUpdate;
  120. cBICMS.update(new baseClass[] { securedFunction }, new updateOptions());
  121. resultMessage = "The \"" + securedFunction.defaultName.value + "\" secured function " +
  122. "has been successfully updated.";
  123. return true;
  124. }
  125. public account getLogonAccount(contentManagerService1 cBICMS)
  126. {
  127. propEnum[] props =
  128. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.policies, propEnum.objectClass };
  129. account myAccount = new account();
  130. try
  131. {
  132. searchPathMultipleObject cmQueryPath = new searchPathMultipleObject();
  133. cmQueryPath.Value = "~";
  134. baseClass[] bc = cBICMS.query(cmQueryPath, props, new sort[] {}, new queryOptions());
  135. if ((bc != null) && (bc.GetLength(0) > 0))
  136. {
  137. for (int i = 0; i < bc.GetLength(0); i++)
  138. {
  139. myAccount = (account)bc[i];
  140. }
  141. }
  142. }
  143. catch (System.Exception ex)
  144. {
  145. //An exception here likely indicates the client is not currently
  146. //logged in, so the query fails.
  147. return null;
  148. }
  149. return myAccount;
  150. }
  151. }
  152. }