Permissions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // * Permissions.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 permissions.
  15. // *
  16. // * Input: Search Path to a permissions 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 permissions/Administration user list.
  25. // *
  26. // *
  27. using System;
  28. using System.Web.Services.Protocols;
  29. using SamplesCommon;
  30. using cognosdotnet_10_2;
  31. namespace Permissions
  32. {
  33. /// <summary>
  34. /// Summary description for Permissions.
  35. /// </summary>
  36. class Permissions
  37. {
  38. /// <summary>
  39. /// The main entry point for the application.
  40. /// </summary>
  41. ///
  42. public Permissions(){}
  43. static void Main(string[] args)
  44. {
  45. string cBIUrl = "";
  46. PermissionsDlg permissionsDlgObject = null;
  47. contentManagerService1 cmService = 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. permissionsDlgObject = new PermissionsDlg();
  58. permissionsDlgObject.setConnection(cmService, cBIUrl);
  59. permissionsDlgObject.ShowDialog();
  60. }
  61. }
  62. }
  63. public bool setPermissions(contentManagerService1 CBICMS, ref string resultMessage, String path)
  64. {
  65. baseClass[] results = new baseClass[] {};
  66. folder csFolder = null;
  67. account myAccount = null;
  68. permission newPermission = null;
  69. string permState = "";
  70. myAccount = getLogonAccount(CBICMS);
  71. string folderName = path;
  72. searchPathMultipleObject cmTargetPath = new searchPathMultipleObject ();
  73. cmTargetPath.Value = folderName;
  74. results = CBICMS.query(cmTargetPath,
  75. new propEnum[] { propEnum.searchPath, propEnum.policies },
  76. new sort[] {},
  77. new queryOptions());
  78. if ( (results == null) || (results.GetLength(0) <= 0) )
  79. {
  80. resultMessage = "..expected folder was not found for the current user.\n" +
  81. "Please create this folder before running this sample.";
  82. return false;
  83. }
  84. csFolder = (folder)results[0];
  85. policy pol;
  86. for (int i = 0; i < csFolder.policies.value.GetLength(0); i++)
  87. {
  88. pol = csFolder.policies.value[i];
  89. if (0 == pol.securityObject.searchPath.value.CompareTo(myAccount.searchPath.value))
  90. {
  91. bool found = false;
  92. newPermission = new permission();
  93. newPermission.name = "read";
  94. int k = pol.permissions.GetLength(0) + 1;
  95. permission[] cur = new permission[k];
  96. for (int j = 0; j < pol.permissions.GetLength(0); j++)
  97. {
  98. if (0 == pol.permissions[j].name.CompareTo("read"))
  99. {
  100. if (pol.permissions[j].access == accessEnum.deny)
  101. {
  102. pol.permissions[j].access = accessEnum.grant;
  103. newPermission.access = accessEnum.grant;
  104. permState = "GRANTED";
  105. }
  106. else
  107. {
  108. pol.permissions[j].access = accessEnum.deny;
  109. newPermission.access = accessEnum.deny;
  110. permState = "DENIED";
  111. }
  112. found = true;
  113. }
  114. cur[j] = pol.permissions[j];
  115. }
  116. if (! found)
  117. {
  118. cur[cur.GetLength(0) - 1] = newPermission;
  119. pol.permissions = cur;
  120. }
  121. CBICMS.update(
  122. new baseClass[] { csFolder },
  123. new updateOptions());
  124. resultMessage += "Successfully " + permState + " " + newPermission.name + " permission on folder for " + myAccount.defaultName.value;
  125. return true;
  126. }
  127. }
  128. resultMessage = "Unable to find \"" + folderName + "\" for " + myAccount.defaultName.value;
  129. return false;
  130. }
  131. public account getLogonAccount(contentManagerService1 cBICMS)
  132. {
  133. propEnum[] props =
  134. new propEnum[] { propEnum.searchPath, propEnum.defaultName, propEnum.policies, propEnum.objectClass };
  135. account myAccount = new account();
  136. try
  137. {
  138. searchPathMultipleObject cmSearchPath = new searchPathMultipleObject ();
  139. cmSearchPath.Value = "~";
  140. baseClass[] bc = cBICMS.query(cmSearchPath, props, new sort[] {}, new queryOptions());
  141. if ((bc != null) && (bc.GetLength(0) > 0))
  142. {
  143. for (int i = 0; i < bc.GetLength(0); i++)
  144. {
  145. myAccount = (account)bc[i];
  146. }
  147. }
  148. }
  149. catch (System.Exception ex)
  150. {
  151. //An exception here likely indicates the client is not currently
  152. //logged in, so the query fails.
  153. return null;
  154. }
  155. return myAccount;
  156. }
  157. }
  158. }