SecurityDlg.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. using System;
  9. using System.Drawing;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Windows.Forms;
  13. using SamplesCommon;
  14. using cognosdotnet_10_2;
  15. namespace Security
  16. {
  17. /// <summary>
  18. /// Summary description for SecurityDlg.
  19. /// </summary>
  20. public class SecurityDlg : System.Windows.Forms.Form
  21. {
  22. private System.Windows.Forms.Label ServerUrlLBL;
  23. private System.Windows.Forms.Label label1;
  24. private System.Windows.Forms.ComboBox securityOptionCB;
  25. private System.Windows.Forms.Button buttonRunOption;
  26. private System.Windows.Forms.RichTextBox resultsDisplayWindowRTB;
  27. private System.Windows.Forms.Label resultsDisplayWindowLBL;
  28. private System.Windows.Forms.MainMenu mainMenu1;
  29. private System.Windows.Forms.MenuItem menuItemFile;
  30. private System.Windows.Forms.MenuItem menuItemExit;
  31. private System.Windows.Forms.MenuItem menuItemHelp;
  32. private System.Windows.Forms.MenuItem menuItemAbout;
  33. private IContainer components;
  34. private static SamplesConnect CBIConnection = null;
  35. private string savedUserName = "";
  36. private string savedNamespace = "";
  37. private System.Windows.Forms.TextBox serverUrlTB;
  38. private bool loggedIn = false;
  39. public SecurityDlg()
  40. {
  41. //
  42. // Required for Windows Form Designer support
  43. //
  44. InitializeComponent();
  45. //
  46. // TODO: Add any constructor code after InitializeComponent call
  47. //
  48. }
  49. /// <summary>
  50. /// Clean up any resources being used.
  51. /// </summary>
  52. protected override void Dispose( bool disposing )
  53. {
  54. if( disposing )
  55. {
  56. if(components != null)
  57. {
  58. components.Dispose();
  59. }
  60. }
  61. base.Dispose( disposing );
  62. }
  63. #region Windows Form Designer generated code
  64. /// <summary>
  65. /// Required method for Designer support - do not modify
  66. /// the contents of this method with the code editor.
  67. /// </summary>
  68. private void InitializeComponent()
  69. {
  70. this.components = new System.ComponentModel.Container();
  71. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SecurityDlg));
  72. this.ServerUrlLBL = new System.Windows.Forms.Label();
  73. this.serverUrlTB = new System.Windows.Forms.TextBox();
  74. this.label1 = new System.Windows.Forms.Label();
  75. this.securityOptionCB = new System.Windows.Forms.ComboBox();
  76. this.buttonRunOption = new System.Windows.Forms.Button();
  77. this.resultsDisplayWindowRTB = new System.Windows.Forms.RichTextBox();
  78. this.resultsDisplayWindowLBL = new System.Windows.Forms.Label();
  79. this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
  80. this.menuItemFile = new System.Windows.Forms.MenuItem();
  81. this.menuItemExit = new System.Windows.Forms.MenuItem();
  82. this.menuItemHelp = new System.Windows.Forms.MenuItem();
  83. this.menuItemAbout = new System.Windows.Forms.MenuItem();
  84. this.SuspendLayout();
  85. //
  86. // ServerUrlLBL
  87. //
  88. this.ServerUrlLBL.Location = new System.Drawing.Point(16, 13);
  89. this.ServerUrlLBL.Name = "ServerUrlLBL";
  90. this.ServerUrlLBL.Size = new System.Drawing.Size(144, 16);
  91. this.ServerUrlLBL.TabIndex = 0;
  92. this.ServerUrlLBL.Text = "Server URL";
  93. //
  94. // serverUrlTB
  95. //
  96. this.serverUrlTB.BackColor = System.Drawing.SystemColors.Control;
  97. this.serverUrlTB.Location = new System.Drawing.Point(16, 32);
  98. this.serverUrlTB.Name = "serverUrlTB";
  99. this.serverUrlTB.Size = new System.Drawing.Size(600, 20);
  100. this.serverUrlTB.TabIndex = 1;
  101. //
  102. // label1
  103. //
  104. this.label1.Location = new System.Drawing.Point(160, 72);
  105. this.label1.Name = "label1";
  106. this.label1.Size = new System.Drawing.Size(80, 16);
  107. this.label1.TabIndex = 2;
  108. this.label1.Text = "Choose Option";
  109. //
  110. // securityOptionCB
  111. //
  112. this.securityOptionCB.Location = new System.Drawing.Point(160, 88);
  113. this.securityOptionCB.Name = "securityOptionCB";
  114. this.securityOptionCB.Size = new System.Drawing.Size(160, 21);
  115. this.securityOptionCB.TabIndex = 3;
  116. //
  117. // buttonRunOption
  118. //
  119. this.buttonRunOption.Location = new System.Drawing.Point(336, 88);
  120. this.buttonRunOption.Name = "buttonRunOption";
  121. this.buttonRunOption.Size = new System.Drawing.Size(104, 24);
  122. this.buttonRunOption.TabIndex = 4;
  123. this.buttonRunOption.Text = "Run Option";
  124. this.buttonRunOption.Click += new System.EventHandler(this.buttonRunOption_Click);
  125. //
  126. // resultsDisplayWindowRTB
  127. //
  128. this.resultsDisplayWindowRTB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  129. | System.Windows.Forms.AnchorStyles.Left)
  130. | System.Windows.Forms.AnchorStyles.Right)));
  131. this.resultsDisplayWindowRTB.BackColor = System.Drawing.SystemColors.Control;
  132. this.resultsDisplayWindowRTB.Location = new System.Drawing.Point(16, 136);
  133. this.resultsDisplayWindowRTB.Name = "resultsDisplayWindowRTB";
  134. this.resultsDisplayWindowRTB.Size = new System.Drawing.Size(600, 264);
  135. this.resultsDisplayWindowRTB.TabIndex = 5;
  136. this.resultsDisplayWindowRTB.Text = "";
  137. //
  138. // resultsDisplayWindowLBL
  139. //
  140. this.resultsDisplayWindowLBL.Location = new System.Drawing.Point(16, 120);
  141. this.resultsDisplayWindowLBL.Name = "resultsDisplayWindowLBL";
  142. this.resultsDisplayWindowLBL.Size = new System.Drawing.Size(128, 16);
  143. this.resultsDisplayWindowLBL.TabIndex = 6;
  144. this.resultsDisplayWindowLBL.Text = "Results Display Window";
  145. //
  146. // mainMenu1
  147. //
  148. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  149. this.menuItemFile,
  150. this.menuItemHelp});
  151. //
  152. // menuItemFile
  153. //
  154. this.menuItemFile.Index = 0;
  155. this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  156. this.menuItemExit});
  157. this.menuItemFile.Text = "File";
  158. //
  159. // menuItemExit
  160. //
  161. this.menuItemExit.Index = 0;
  162. this.menuItemExit.Text = "Exit";
  163. this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
  164. //
  165. // menuItemHelp
  166. //
  167. this.menuItemHelp.Index = 1;
  168. this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  169. this.menuItemAbout});
  170. this.menuItemHelp.Text = "Help";
  171. //
  172. // menuItemAbout
  173. //
  174. this.menuItemAbout.Index = 0;
  175. this.menuItemAbout.Text = "About";
  176. this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
  177. //
  178. // SecurityDlg
  179. //
  180. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  181. this.ClientSize = new System.Drawing.Size(632, 414);
  182. this.Controls.Add(this.resultsDisplayWindowLBL);
  183. this.Controls.Add(this.resultsDisplayWindowRTB);
  184. this.Controls.Add(this.buttonRunOption);
  185. this.Controls.Add(this.securityOptionCB);
  186. this.Controls.Add(this.label1);
  187. this.Controls.Add(this.serverUrlTB);
  188. this.Controls.Add(this.ServerUrlLBL);
  189. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  190. this.Menu = this.mainMenu1;
  191. this.Name = "SecurityDlg";
  192. this.Text = "Security Sample";
  193. this.Load += new System.EventHandler(this.SecurityDlg_Load);
  194. this.ResumeLayout(false);
  195. this.PerformLayout();
  196. }
  197. #endregion
  198. private void menuItemExit_Click(object sender, System.EventArgs e)
  199. {
  200. this.Close();
  201. }
  202. private void menuItemAbout_Click(object sender, System.EventArgs e)
  203. {
  204. SamplesAbout about = new SamplesAbout();
  205. about.applicationName = "Security Sample";
  206. about.applicationVersion = "1.1";
  207. about.Show();
  208. }
  209. private void SecurityDlg_Load(object sender, System.EventArgs e)
  210. {
  211. // Check if the current user is logged in as Anonymous
  212. // if so, enable the check box.
  213. account myAccount = new account();
  214. baseClass[] bc = new baseClass[1];
  215. propEnum[] props = new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  216. searchPathMultipleObject homeSearchPath = new searchPathMultipleObject();
  217. homeSearchPath.Value = "~";
  218. try
  219. {
  220. bc = CBIConnection.CBICMS.query(homeSearchPath, props, new sort[] {}, new queryOptions());
  221. }
  222. catch(System.Exception ex)
  223. {
  224. // Anonymous is OFF
  225. securityOptionCB.Items.Add("Get Logon Info");
  226. securityOptionCB.Items.Add("Logoff");
  227. securityOptionCB.SelectedIndex = 0;
  228. return;
  229. }
  230. if (bc != null)
  231. {
  232. // Anonymous is ON.
  233. securityOptionCB.Items.Add("Get Logon Info");
  234. securityOptionCB.Items.Add("Logon as a different user");
  235. securityOptionCB.Items.Add("Logoff");
  236. securityOptionCB.SelectedIndex = 0;
  237. return;
  238. }
  239. }
  240. // This function is the main driver for this sample.
  241. private void buttonRunOption_Click(object sender, System.EventArgs e)
  242. {
  243. clearDisplayWindow();
  244. string result = "";
  245. Security securityObj = new Security();
  246. string selectedOption = (string)securityOptionCB.SelectedItem;
  247. if ( (selectedOption == null) || (0 == selectedOption.CompareTo("")) )
  248. {
  249. MessageBox.Show("Please select a valid security option.");
  250. return;
  251. }
  252. if (0 == selectedOption.CompareTo("Get Logon Info"))
  253. {
  254. if (!isLoggedIn(CBIConnection.CBICMS))
  255. {
  256. try
  257. {
  258. if (securityObj.doLogon(CBIConnection, savedUserName, savedNamespace, ref result))
  259. {
  260. loggedIn = true;
  261. }
  262. }
  263. catch (System.Exception ex)
  264. {
  265. result = "logon failure: " + ex.Message + ".\n";
  266. }
  267. }
  268. result = "";
  269. securityObj.getLogonInfo(CBIConnection, ref result);
  270. }
  271. else if (0 == selectedOption.CompareTo("Logon as a different user"))
  272. {
  273. if (securityObj.doLogonAs(CBIConnection, savedUserName, savedNamespace, ref result))
  274. {
  275. loggedIn = true;
  276. }
  277. }
  278. else if (0 == selectedOption.CompareTo("Logoff"))
  279. {
  280. if (securityObj.doLogoff(CBIConnection, ref result))
  281. {
  282. loggedIn = false;
  283. }
  284. }
  285. displayMessage(result);
  286. }
  287. public bool isLoggedIn(contentManagerService1 cBICMS)
  288. {
  289. return ( isAnonymous(cBICMS) || loggedIn );
  290. }
  291. public bool isAnonymous(contentManagerService1 cBICMS)
  292. {
  293. bool doTestForAnonymous = false;
  294. try
  295. {
  296. searchPathMultipleObject homeSearchPath = new searchPathMultipleObject();
  297. homeSearchPath.Value = "~";
  298. baseClass[] bc =
  299. cBICMS.query(
  300. homeSearchPath,
  301. new propEnum[] {},
  302. new sort[] {},
  303. new queryOptions());
  304. if (bc != null)
  305. {
  306. doTestForAnonymous = true;
  307. }
  308. else
  309. {
  310. doTestForAnonymous = false;
  311. }
  312. }
  313. catch (System.Exception ex)
  314. {
  315. //Ignore this, it means that Anonymous access is denied...
  316. }
  317. return doTestForAnonymous;
  318. }
  319. public void setConnection(SamplesConnect cBIConnection, string cBIUrl, string userName, string userNamespace)
  320. {
  321. CBIConnection = cBIConnection;
  322. serverUrlTB.Text = cBIUrl;
  323. savedUserName = userName;
  324. savedNamespace = userNamespace;
  325. }
  326. public void displayMessage(string message)
  327. {
  328. resultsDisplayWindowRTB.AppendText("\n" + message);
  329. }
  330. public void clearDisplayWindow()
  331. {
  332. resultsDisplayWindowRTB.Clear();
  333. }
  334. }
  335. }