update signutare
[CPE_learningsite] / CPE / CPE.App / CPE.App.Web / Connect / Session.cs
1 using System;
2 using System.IO;
3 using System.Xml.Serialization;
4 using System.Web.Script.Serialization;
5
6 namespace CPE.App.Web.Connect {
7
8         /// <summary>
9         /// Holds the various parameters for connecting to the API including the hostname and session key.
10         /// </summary>
11         [Serializable]
12         public class Session {
13
14                 /// <summary>
15                 /// The host to access for connecting to the API.
16                 /// </summary>
17                 /// <example>https://admin.acrobat.com</example>
18                 public string Hostname { get; set; }
19
20                 /// <summary>
21                 /// The unique encrypted key that identifies this login and session to Connect.
22                 /// </summary>
23                 public string SessionKey { get; set; }
24
25                 /// <summary>
26                 /// Specifies the hostname of the proxy to route all requests through.
27                 /// </summary>
28                 public string Proxy { get; set; }
29
30                 /// <summary>
31                 /// Initializes a new session.
32                 /// </summary>
33                 public Session() { }
34
35                 /// <summary>
36                 /// Checks whether the current session is working and valid.
37                 /// </summary>
38                 public bool IsValid() {
39
40                         // Only logged in users may view shortcuts
41                         var request = new Request(this, "sco-shortcuts");
42                         return (request.Execute() && request.Status == Status.OK);
43                 }
44
45                 /// <summary>
46                 /// Checks whether the current session's login has administrative permissions.
47                 /// </summary>
48                 public bool IsAdministrator() {
49
50                         // Call admin-only report-quotas action
51                         var request = new Request(this, "report-quotas");
52                         return (request.Execute() && request.Status == Status.OK);
53                 }
54
55
56             private CommonInfo _commonInfo;
57         public CommonInfo CommonInfo
58             {
59                 get
60                 {
61                 if (_commonInfo == null)
62                 {
63                     // Query common-info for user information
64                     var request = new Request(this, "common-info");
65
66                     if (request.Execute() && request.Status == Status.OK)
67                         _commonInfo = new CommonInfo(request.XmlResults);
68                 }
69                     return _commonInfo;
70                 }
71             }
72
73                 /// <summary>
74                 /// Returns the user information tied to this session.
75                 /// </summary>
76         public CommonInfo.UserInfo UserInfo {
77                         get {
78
79                             var common = CommonInfo;
80                 if (common != null)
81                     return common.User;
82                                 return null;
83                         }
84                 }
85
86
87             private PrincipalInfo _principalInfo = null;
88                 /// <summary>
89                 /// Returns the detailed principal information for the user tied to this session.
90                 /// </summary>
91         public PrincipalInfo PrincipalInfo {
92                         get {
93                 if (_principalInfo == null)
94                 {
95                     var request = new Request(this, "principal-info");
96                     request.Parameters.Add("principal-id", UserInfo.UserId.ToString());
97
98                     if (request.Execute() && request.Status == Status.OK)
99                     {
100                         _principalInfo = new PrincipalInfo(request.XmlResults);
101                     }
102                 }
103
104                 return _principalInfo;
105                         }
106                 }
107         }
108 }