ProfileService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #region ProfileSerializer
  42. sealed class ProfileSerializer : JavaScriptSerializer.LazyDictionary
  43. {
  44. readonly string [] _properties;
  45. public ProfileSerializer (string [] properties) {
  46. _properties = properties;
  47. }
  48. protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
  49. if (_properties == null)
  50. yield break;
  51. ProfileBase profile = HttpContext.Current.Profile;
  52. for (int i = 0; i < _properties.Length; i++) {
  53. string name = _properties [i];
  54. int dot = name.IndexOf ('.');
  55. object value = (dot > 0) ? profile.GetProfileGroup (name.Substring (0, dot))
  56. .GetPropertyValue (name.Substring (dot + 1)) :
  57. profile.GetPropertyValue (name);
  58. yield return new KeyValuePair<string, object> (name, value);
  59. }
  60. }
  61. }
  62. #endregion
  63. public const string DefaultWebServicePath = "/Profile_JSON_AppService.axd";
  64. readonly ScriptingProfileServiceSection _section;
  65. public ProfileService () {
  66. _section = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
  67. }
  68. ScriptingProfileServiceSection ScriptingProfileServiceSection {
  69. get {
  70. if (_section == null || !_section.Enabled)
  71. throw new InvalidOperationException ("Profile service is disabled.");
  72. return _section;
  73. }
  74. }
  75. [WebMethod()]
  76. public IDictionary<string,object> GetAllPropertiesForCurrentUser () {
  77. return new ProfileSerializer (ScriptingProfileServiceSection.ReadAccessProperties);
  78. }
  79. [WebMethod ()]
  80. public IDictionary<string, object> GetPropertiesForCurrentUser (string [] properties) {
  81. if (properties == null)
  82. return GetAllPropertiesForCurrentUser ();
  83. string [] raProps = ScriptingProfileServiceSection.ReadAccessPropertiesNoCopy;
  84. List<string> list = null;
  85. for (int i = 0; i < properties.Length; i++) {
  86. string prop = properties [i];
  87. if (prop == null)
  88. throw new ArgumentNullException ("properties[" + i + "]");
  89. if (IsPropertyConfigured(raProps, prop)) {
  90. if (list != null)
  91. list.Add(prop);
  92. }
  93. else if (list == null) {
  94. list = new List<string> (properties.Length - 1);
  95. for (int k = 0; k < i; k++)
  96. list.Add (properties [k]);
  97. }
  98. }
  99. return new ProfileSerializer (list != null ? list.ToArray () : properties);
  100. }
  101. [WebMethod ()]
  102. public int SetPropertiesForCurrentUser (Dictionary<string, object> values) {
  103. if (values == null)
  104. return 0;
  105. string [] waProps = ScriptingProfileServiceSection.WriteAccessPropertiesNoCopy;
  106. int counter = 0;
  107. ProfileBase profile = HttpContext.Current.Profile;
  108. foreach (KeyValuePair<string, object> pair in values) {
  109. try {
  110. string name = pair.Key;
  111. if (!IsPropertyConfigured (waProps, name))
  112. continue;
  113. int dot = name.IndexOf ('.');
  114. if (dot > 0)
  115. profile.GetProfileGroup (name.Substring (0, dot))
  116. .SetPropertyValue (name.Substring (dot + 1), pair.Value);
  117. else
  118. profile.SetPropertyValue (name, pair.Value);
  119. }
  120. catch {
  121. continue; //MS seems to ignore errors...
  122. }
  123. counter++;
  124. }
  125. return counter;
  126. }
  127. static bool IsPropertyConfigured (string [] configuredProperties, string propertyToCheck) {
  128. if (configuredProperties == null)
  129. return false;
  130. bool found = false;
  131. for (int i = 0; !found && i < configuredProperties.Length; i++)
  132. found = configuredProperties [i].Equals (propertyToCheck, StringComparison.OrdinalIgnoreCase);
  133. return found;
  134. }
  135. }
  136. }