initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / Connect / CommonInfo.cs
1 using System.Xml;
2
3 namespace CPE.App.Web.Connect {
4
5         /// <summary>
6         /// Represents a common-info object returned from Connect.
7         /// </summary>
8         public class CommonInfo {
9
10                 /// <summary>
11                 /// The hostname of the Connect service.
12                 /// </summary>
13                 public string Host { get; private set; }
14
15                 /// <summary>
16                 /// The AccountID of the host of the Connect service.
17                 /// </summary>
18                 public int AccountId { get; private set; }
19
20                 /// <summary>
21                 /// The session cookie value for the current request.
22                 /// </summary>
23                 public string Cookie { get; private set; }
24
25                 /// <summary>
26                 /// Returns whether the request is logged in and authenticated with Connect.
27                 /// </summary>
28                 public bool IsAuthenticated { get { return User.UserId.HasValue; } }
29
30                 /// <summary>
31                 /// Contains the user account information if a user is logged into Connect.
32                 /// </summary>
33                 public UserInfo User { get; private set; }
34
35                 /// <summary>
36                 /// Initializes the object by parsing the values from the XmlDocument.
37                 /// </summary>
38                 /// <param name="document">The XmlDocument returned from Connect for this object.</param>
39                 public CommonInfo(XmlNode document) {
40
41                         // Find main node
42                         XmlNode common = document.SelectSingleNode("/results/common");
43
44                         // Parse values 
45                         Host = common["host"].InnerText;
46                         AccountId = int.Parse(common["account"].Attributes["account-id"].Value);
47                         Cookie = common["cookie"].InnerText;
48
49                         // Blank user
50                         User = new UserInfo();
51                 
52                         if(common["user"] != null) {
53                                 User.UserId = int.Parse(common["user"].Attributes["user-id"].Value);
54                                 User.Name = common["user"].SelectSingleNode("name").InnerText;
55                                 User.Login = common["user"].SelectSingleNode("login").InnerText;
56                         }
57                 }
58
59                 /// <summary>
60                 /// Contains the current user information if logged into the service.
61                 /// </summary>
62                 public class UserInfo {
63
64                         /// <summary>
65                         /// The UserID of the user.
66                         /// </summary>
67                         public int? UserId { get; internal set; }
68
69                         /// <summary>
70                         /// The full name of the user.
71                         /// </summary>
72                         public string Name { get; internal set; }
73
74                         /// <summary>
75                         /// The login of the user.
76                         /// </summary>
77                         public string Login { get; internal set; }
78                 }
79         }
80 }