ProfileService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // ScriptHandlerFactory.cs
  3. //
  4. // Author:
  5. // Konstantin Triger <[email protected]>
  6. //
  7. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Web.Services;
  33. using System.Configuration;
  34. using System.Web.Configuration;
  35. using System.Web.Script.Serialization;
  36. using System.Web.Profile;
  37. namespace System.Web.Script.Services
  38. {
  39. sealed class ProfileService
  40. {
  41. public const string DefaultWebServicePath = "/Profile_JSON_AppService.axd";
  42. readonly ScriptingProfileServiceSection _section;
  43. public ProfileService () {
  44. _section = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
  45. }
  46. ScriptingProfileServiceSection ScriptingProfileServiceSection {
  47. get {
  48. if (_section == null || !_section.Enabled)
  49. throw new InvalidOperationException ("Profile service is disabled.");
  50. return _section;
  51. }
  52. }
  53. public IDictionary <string, object> GetProfileDictionary (string[] properties)
  54. {
  55. var ret = new Dictionary <string, object> ();
  56. int len = properties != null ? properties.Length : 0;
  57. if (len <= 0)
  58. return ret;
  59. ProfileBase profile = HttpContext.Current.Profile;
  60. string name;
  61. int dot;
  62. object value;
  63. for (int i = 0; i < len; i++) {
  64. name = properties [i];
  65. dot = name.IndexOf ('.');
  66. value = (dot > 0) ? profile.GetProfileGroup (name.Substring (0, dot)).GetPropertyValue (name.Substring (dot + 1)) : profile.GetPropertyValue (name);
  67. ret.Add (name, value);
  68. }
  69. return ret;
  70. }
  71. [WebMethod()]
  72. public IDictionary<string, object> GetAllPropertiesForCurrentUser (bool authenticatedUserOnly) {
  73. return GetProfileDictionary (ScriptingProfileServiceSection.ReadAccessProperties);
  74. }
  75. [WebMethod ()]
  76. public IDictionary<string, object> GetPropertiesForCurrentUser (string [] properties, bool authenticatedUserOnly) {
  77. if (properties == null)
  78. return GetAllPropertiesForCurrentUser (authenticatedUserOnly);
  79. string [] raProps = ScriptingProfileServiceSection.ReadAccessPropertiesNoCopy;
  80. List<string> list = null;
  81. for (int i = 0; i < properties.Length; i++) {
  82. string prop = properties [i];
  83. if (prop == null)
  84. throw new ArgumentNullException ("properties[" + i + "]");
  85. if (IsPropertyConfigured(raProps, prop)) {
  86. if (list != null)
  87. list.Add(prop);
  88. }
  89. else if (list == null) {
  90. list = new List<string> (properties.Length - 1);
  91. for (int k = 0; k < i; k++)
  92. list.Add (properties [k]);
  93. }
  94. }
  95. return GetProfileDictionary (list != null ? list.ToArray () : properties);
  96. }
  97. [WebMethod ()]
  98. public string [] SetPropertiesForCurrentUser (Dictionary<string, object> values, bool authenticatedUserOnly) {
  99. if (values == null)
  100. return new string [] { };
  101. string [] waProps = ScriptingProfileServiceSection.WriteAccessPropertiesNoCopy;
  102. List<string> list = new List<string> ();
  103. ProfileBase profile = HttpContext.Current.Profile;
  104. foreach (KeyValuePair<string, object> pair in values) {
  105. try {
  106. string name = pair.Key;
  107. if (!IsPropertyConfigured (waProps, name))
  108. continue;
  109. int dot = name.IndexOf ('.');
  110. if (dot > 0)
  111. profile.GetProfileGroup (name.Substring (0, dot))
  112. .SetPropertyValue (name.Substring (dot + 1), pair.Value);
  113. else
  114. profile.SetPropertyValue (name, pair.Value);
  115. }
  116. catch {
  117. list.Add (pair.Key);
  118. }
  119. }
  120. return list.ToArray ();
  121. }
  122. static bool IsPropertyConfigured (string [] configuredProperties, string propertyToCheck) {
  123. if (configuredProperties == null)
  124. return false;
  125. bool found = false;
  126. for (int i = 0; !found && i < configuredProperties.Length; i++)
  127. found = configuredProperties [i].Equals (propertyToCheck, StringComparison.OrdinalIgnoreCase);
  128. return found;
  129. }
  130. }
  131. }