SamplesLogon.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  10. using System;
  11. using System.Drawing;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Windows.Forms;
  15. using System.Xml;
  16. using System.IO;
  17. using System.Web.Services.Protocols;
  18. using System.Text;
  19. using cognosdotnet_10_2;
  20. namespace SamplesCommon
  21. {
  22. /// <summary>
  23. /// A standard logon dialog for the Samples.
  24. /// </summary>
  25. public class SamplesLogon : System.Windows.Forms.Form
  26. {
  27. private System.Windows.Forms.Label label1;
  28. private System.Windows.Forms.Label label2;
  29. private System.Windows.Forms.Label label3;
  30. private System.Windows.Forms.ComboBox namespaceBox;
  31. private System.Windows.Forms.TextBox userNameText;
  32. private System.Windows.Forms.TextBox passwordText;
  33. private System.Windows.Forms.Button logonButton;
  34. private System.Windows.Forms.Button cancelButton;
  35. /// <summary>
  36. /// Required designer variable.
  37. /// </summary>
  38. private System.ComponentModel.Container components = null;
  39. public SamplesLogon( SamplesConnect connection )
  40. {
  41. //
  42. // Required for Windows Form Designer support
  43. //
  44. InitializeComponent();
  45. // Save our contentManagerService1 service object.
  46. cBICMS = connection.CBICMS;
  47. }
  48. public SamplesLogon(contentManagerService1 cmService)
  49. {
  50. //
  51. // Required for Windows Form Designer support
  52. //
  53. InitializeComponent();
  54. // Save our contentManagerService1 object.
  55. cBICMS = cmService;
  56. }
  57. /// <summary>
  58. /// Clean up any resources being used.
  59. /// </summary>
  60. protected override void Dispose( bool disposing )
  61. {
  62. if( disposing )
  63. {
  64. if(components != null)
  65. {
  66. components.Dispose();
  67. }
  68. }
  69. base.Dispose( disposing );
  70. }
  71. #region Windows Form Designer generated code
  72. /// <summary>
  73. /// Required method for Designer support - do not modify
  74. /// the contents of this method with the code editor.
  75. /// </summary>
  76. private void InitializeComponent()
  77. {
  78. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SamplesLogon));
  79. this.label1 = new System.Windows.Forms.Label();
  80. this.label2 = new System.Windows.Forms.Label();
  81. this.label3 = new System.Windows.Forms.Label();
  82. this.namespaceBox = new System.Windows.Forms.ComboBox();
  83. this.userNameText = new System.Windows.Forms.TextBox();
  84. this.passwordText = new System.Windows.Forms.TextBox();
  85. this.logonButton = new System.Windows.Forms.Button();
  86. this.cancelButton = new System.Windows.Forms.Button();
  87. this.SuspendLayout();
  88. //
  89. // label1
  90. //
  91. this.label1.Location = new System.Drawing.Point(16, 16);
  92. this.label1.Name = "label1";
  93. this.label1.Size = new System.Drawing.Size(64, 24);
  94. this.label1.TabIndex = 0;
  95. this.label1.Text = "User Name:";
  96. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  97. //
  98. // label2
  99. //
  100. this.label2.Location = new System.Drawing.Point(16, 48);
  101. this.label2.Name = "label2";
  102. this.label2.Size = new System.Drawing.Size(64, 23);
  103. this.label2.TabIndex = 1;
  104. this.label2.Text = "Password:";
  105. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  106. //
  107. // label3
  108. //
  109. this.label3.Location = new System.Drawing.Point(16, 80);
  110. this.label3.Name = "label3";
  111. this.label3.Size = new System.Drawing.Size(72, 24);
  112. this.label3.TabIndex = 2;
  113. this.label3.Text = "Namespace:";
  114. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  115. //
  116. // namespaceBox
  117. //
  118. this.namespaceBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  119. | System.Windows.Forms.AnchorStyles.Right)));
  120. this.namespaceBox.BackColor = System.Drawing.SystemColors.Control;
  121. this.namespaceBox.Location = new System.Drawing.Point(88, 80);
  122. this.namespaceBox.Name = "namespaceBox";
  123. this.namespaceBox.Size = new System.Drawing.Size(184, 21);
  124. this.namespaceBox.TabIndex = 5;
  125. //
  126. // userNameText
  127. //
  128. this.userNameText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  129. | System.Windows.Forms.AnchorStyles.Right)));
  130. this.userNameText.Location = new System.Drawing.Point(88, 16);
  131. this.userNameText.Name = "userNameText";
  132. this.userNameText.Size = new System.Drawing.Size(184, 20);
  133. this.userNameText.TabIndex = 3;
  134. this.userNameText.Text = "";
  135. //
  136. // passwordText
  137. //
  138. this.passwordText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  139. | System.Windows.Forms.AnchorStyles.Right)));
  140. this.passwordText.Location = new System.Drawing.Point(88, 48);
  141. this.passwordText.Name = "passwordText";
  142. this.passwordText.PasswordChar = '*';
  143. this.passwordText.Size = new System.Drawing.Size(184, 20);
  144. this.passwordText.TabIndex = 4;
  145. this.passwordText.Text = "";
  146. this.passwordText.KeyDown += new System.Windows.Forms.KeyEventHandler(this.passwordText_OnKeyPress);
  147. //
  148. // logonButton
  149. //
  150. this.logonButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  151. this.logonButton.Location = new System.Drawing.Point(112, 128);
  152. this.logonButton.Name = "logonButton";
  153. this.logonButton.TabIndex = 6;
  154. this.logonButton.Text = "Logon";
  155. this.logonButton.Click += new System.EventHandler(this.logonButton_Click);
  156. //
  157. // cancelButton
  158. //
  159. this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  160. this.cancelButton.Location = new System.Drawing.Point(200, 128);
  161. this.cancelButton.Name = "cancelButton";
  162. this.cancelButton.TabIndex = 7;
  163. this.cancelButton.Text = "Cancel";
  164. this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
  165. //
  166. // SamplesLogon
  167. //
  168. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  169. this.ClientSize = new System.Drawing.Size(292, 166);
  170. this.Controls.Add(this.cancelButton);
  171. this.Controls.Add(this.logonButton);
  172. this.Controls.Add(this.passwordText);
  173. this.Controls.Add(this.userNameText);
  174. this.Controls.Add(this.namespaceBox);
  175. this.Controls.Add(this.label3);
  176. this.Controls.Add(this.label2);
  177. this.Controls.Add(this.label1);
  178. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  179. this.Name = "SamplesLogon";
  180. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  181. this.Text = "Logon";
  182. this.Load += new System.EventHandler(this.SamplesLogon_Load);
  183. this.ResumeLayout(false);
  184. }
  185. #endregion
  186. /// <summary>
  187. /// service object; we need to have one of these so we can
  188. /// access the logon() method.
  189. /// </summary>
  190. private contentManagerService1 cBICMS = null;
  191. /// <summary>
  192. /// Have we successfully logged on? Note that we've got a read-only
  193. /// propery if you want to look at this.
  194. /// </summary>
  195. private bool isLoggedOn = false;
  196. /// <summary>
  197. /// This is the default user name that shows up in the logon dialog box
  198. /// When it is re-opened.
  199. /// </summary>
  200. private string defaultUserName = "";
  201. private string defaultPassword = "";
  202. private string defaultNamespace = "";
  203. private bool switchUserMode = false;
  204. /// <summary>
  205. /// Have we successfully logged on?
  206. /// </summary>
  207. public bool loggedOn
  208. {
  209. get {
  210. return isLoggedOn;
  211. }
  212. }
  213. private string[] getNamespaces()
  214. {
  215. // Initialize the return values container
  216. String[] namespaces = new String[] {};
  217. try
  218. {
  219. searchPathMultipleObject rootSearchPath = new searchPathMultipleObject();
  220. rootSearchPath.Value = "~";
  221. cBICMS.query(rootSearchPath, null, new sort[] {},new queryOptions());
  222. }
  223. catch (System.Web.Services.Protocols.SoapException ex)
  224. {
  225. if (ex == null) { }
  226. //Ignore this exception, it is expected
  227. }
  228. if (switchUserMode)
  229. {
  230. // directly query for the namespace
  231. searchPathMultipleObject directorySearchPath = new searchPathMultipleObject();
  232. directorySearchPath.Value = "/directory/*";
  233. propEnum[] props =
  234. new propEnum[] { propEnum.searchPath, propEnum.defaultName };
  235. baseClass[] bcNamespaces = cBICMS.query(directorySearchPath, props, new sort[] {}, new queryOptions());
  236. if ( (bcNamespaces != null) && (bcNamespaces.GetLength(0) > 0) )
  237. {
  238. namespaces = new string[(bcNamespaces.GetLength(0)-1) * 2]; // skip "Cognos"
  239. for (int i=1; i < bcNamespaces.GetLength(0); i++)
  240. {
  241. namespaces[i-1] = bcNamespaces[i].defaultName.value;
  242. namespaces[i] = bcNamespaces[i].searchPath.value;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. // Look in the displayObjects for namespace prompt options and capture
  249. // all the namespaces defined there.
  250. displayObject[] dob =
  251. cBICMS.biBusHeaderValue.CAM.exception.promptInfo.displayObjects;
  252. for (int i = 0; i < dob.Length; i++)
  253. {
  254. if (dob[i].name.Equals("CAMNamespace"))
  255. {
  256. promptOption[] pop = dob[i].promptOptions;
  257. // Check to see how many namespaces exist.
  258. // If there is an array, there are many namespaces.
  259. // Otherwise there is only one namespace.
  260. if (pop != null)
  261. {
  262. namespaces = new string[pop.Length * 2];
  263. for (int j = 0, k = 0; k < pop.Length; j++, k++)
  264. {
  265. namespaces[j] = pop[k].value;
  266. namespaces[++j] = pop[k].id;
  267. }
  268. }
  269. else // There is only one namespace.
  270. {
  271. namespaces = new string[2];
  272. namespaces[0] = dob[i].value;
  273. namespaces[1] = dob[i].value;
  274. }
  275. }
  276. }
  277. }
  278. return namespaces;
  279. }
  280. /// <summary>
  281. /// The user has clicked on the Cancel button. Indicate that we
  282. /// haven't logged on, and close the Logon dialog.
  283. /// </summary>
  284. /// <param name="sender">What object sent this event? (not used)</param>
  285. /// <param name="e">Event arguments (not used)</param>
  286. private void cancelButton_Click( object sender, System.EventArgs e )
  287. {
  288. isLoggedOn = false;
  289. this.Close();
  290. }
  291. private void passwordText_OnKeyPress(object sender, KeyEventArgs e)
  292. {
  293. if (e.KeyCode == Keys.Enter)
  294. {
  295. logonButton_Click(sender, e);
  296. }
  297. }
  298. /// <summary>
  299. /// Attempt to log on to the server using the supplied
  300. /// credentials.
  301. /// </summary>
  302. /// <param name="sender">What object sent this event? (not used)</param>
  303. /// <param name="e">Event arguments (not used)</param>
  304. private void logonButton_Click( object sender, System.EventArgs e )
  305. {
  306. // scrub the incoming values
  307. if( namespaceBox.Text == null ) namespaceBox.Text = "";
  308. if( userNameText.Text == null ) userNameText.Text = "";
  309. if( passwordText.Text == null ) passwordText.Text = "";
  310. specificUserLogon(true, userNameText.Text, passwordText.Text, namespaceBox.Text);
  311. this.Close();
  312. }
  313. private void SamplesLogon_Load(object sender, System.EventArgs e)
  314. {
  315. string userName = getUserName();
  316. if ((userName != null) && (0 != userName.CompareTo("")) )
  317. {
  318. userNameText.Text = userName;
  319. }
  320. // Attempt to populate the namespace combobox. In theory, the
  321. // user can type a namespace ID if they know one.
  322. namespaceBox.Items.Clear();
  323. string[] namespaceInfo = getNamespaces();
  324. if ( (namespaceInfo == null) || (namespaceInfo.GetLength(0)<=0) )
  325. {
  326. System.Windows.Forms.MessageBox.Show(
  327. "Unable to connect to server",
  328. "Connect Failed");
  329. this.Close();
  330. return;
  331. }
  332. //namespaceInfo is name/ID pairs -- always even
  333. string[] namespaces = new string[namespaceInfo.Length / 2];
  334. //for each value of j, jump to the second next value of k
  335. for (int j = 0, k = 0; k < namespaceInfo.Length; j++, k++)
  336. {
  337. namespaces[j] = namespaceInfo[k++];
  338. namespaceBox.Items.Add(namespaceInfo[k]);
  339. }
  340. namespaceBox.SelectedIndex = 0;
  341. }
  342. public void specificUserLogon(bool guiMode, string userName, string userPassword, string userNamespace)
  343. {
  344. try
  345. {
  346. // sn_dg_prm_sdk_method_contentManagerService_logon_start_0
  347. System.Text.StringBuilder credentialXML = new System.Text.StringBuilder("<credential>" );
  348. credentialXML.AppendFormat( "<namespace>{0}</namespace>", userNamespace );
  349. credentialXML.AppendFormat( "<username>{0}</username>", userName );
  350. credentialXML.AppendFormat( "<password>{0}</password>", userPassword );
  351. credentialXML.Append( "</credential>" );
  352. //The csharp toolkit encodes the credentials
  353. string encodedCredentials = credentialXML.ToString ();
  354. xmlEncodedXML xmlEncodedCredentials = new xmlEncodedXML();
  355. xmlEncodedCredentials.Value = encodedCredentials;
  356. searchPathSingleObject[] emptyRoleSearchPathList = new searchPathSingleObject[0];
  357. cBICMS.logon(xmlEncodedCredentials, null);
  358. // sn_dg_prm_sdk_method_contentManagerService_logon_end_0
  359. //hang on to the user data locally for use elsewhere
  360. setUserName(userName);
  361. setUserPassword(userPassword);
  362. setNamespace(userNamespace);
  363. isLoggedOn = true;
  364. }
  365. catch( SoapException ex )
  366. {
  367. isLoggedOn = false;
  368. SamplesException.ShowExceptionMessage( ex, guiMode, "Unable To Logon" );
  369. }
  370. catch (System.Exception ex)
  371. {
  372. isLoggedOn = false;
  373. SamplesException.ShowExceptionMessage( ex.Message, guiMode, "Unable To Logon" );
  374. }
  375. }
  376. public string getUserName()
  377. {
  378. if (defaultUserName == null)
  379. {
  380. return "";
  381. }
  382. return defaultUserName;
  383. }
  384. public void setUserName(string userName)
  385. {
  386. defaultUserName = userName;
  387. }
  388. public void setUserPassword(string password)
  389. {
  390. defaultPassword = password;
  391. }
  392. public string getUserPassword()
  393. {
  394. if (defaultPassword == null)
  395. {
  396. return "";
  397. }
  398. return defaultPassword;
  399. }
  400. public string getNamespace()
  401. {
  402. if (defaultNamespace == null)
  403. {
  404. return "";
  405. }
  406. return defaultNamespace;
  407. }
  408. public void setNamespace(string nmspace)
  409. {
  410. defaultNamespace = nmspace;
  411. }
  412. public void setSwitchUserMode(bool switchUser)
  413. {
  414. switchUserMode = true;
  415. }
  416. }
  417. }