implement Profile service
authorKonstantin Triger <kostat@mono-cvs.ximian.com>
Thu, 2 Aug 2007 09:10:50 +0000 (09:10 -0000)
committerKonstantin Triger <kostat@mono-cvs.ximian.com>
Thu, 2 Aug 2007 09:10:50 +0000 (09:10 -0000)
svn path=/trunk/mcs/; revision=83270

mcs/class/System.Web.Extensions/System.Web.Extensions.csproj
mcs/class/System.Web.Extensions/System.Web.Script.Services/ProfileService.cs [new file with mode: 0644]
mcs/class/System.Web.Extensions/System.Web.Script.Services/ScriptHandlerFactory.cs
mcs/class/System.Web.Extensions/System.Web.UI/ScriptManager.cs

index 237f02791eebcda64b2bcf3f5a51fcdd4b409062..2a5157c7a6bbd9040639b53df407cbd533d1f8d0 100644 (file)
@@ -86,6 +86,7 @@
     <Compile Include="System.Web.Script.Services\ClientProxyHandler.cs" />\r
     <Compile Include="System.Web.Script.Services\GenerateScriptTypeAttribute.cs" />\r
     <Compile Include="System.Web.Script.Services\LogicalTypeInfo.cs" />\r
+    <Compile Include="System.Web.Script.Services\ProfileService.cs" />\r
     <Compile Include="System.Web.Script.Services\ResponseFormat.cs" />\r
     <Compile Include="System.Web.Script.Services\RestHandler.cs" />\r
     <Compile Include="System.Web.Script.Services\ScriptHandlerFactory.cs" />\r
diff --git a/mcs/class/System.Web.Extensions/System.Web.Script.Services/ProfileService.cs b/mcs/class/System.Web.Extensions/System.Web.Script.Services/ProfileService.cs
new file mode 100644 (file)
index 0000000..a8bbf75
--- /dev/null
@@ -0,0 +1,161 @@
+//
+// ScriptHandlerFactory.cs
+//
+// Author:
+//   Konstantin Triger <kostat@mainsoft.com>
+//
+// (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
+//
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Web.Services;
+using System.Configuration;
+using System.Web.Configuration;
+using System.Web.Script.Serialization;
+using System.Web.Profile;
+
+namespace System.Web.Script.Services
+{
+       sealed class ProfileService
+       {
+               #region ProfileSerializer
+
+               sealed class ProfileSerializer : JavaScriptSerializer.LazyDictionary
+               {
+                       readonly string [] _properties;
+                       public ProfileSerializer (string [] properties) {
+                               _properties = properties;
+                       }
+                       protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
+                               if (_properties == null)
+                                       yield break;
+
+                               ProfileBase profile = HttpContext.Current.Profile;
+                               for (int i = 0; i < _properties.Length; i++) {
+                                       string name = _properties [i];
+                                       int dot = name.IndexOf ('.');
+                                       object value = (dot > 0) ? profile.GetProfileGroup (name.Substring (0, dot))
+                                               .GetPropertyValue (name.Substring (dot + 1)) :
+                                               profile.GetPropertyValue (name);
+                                       yield return new KeyValuePair<string, object> (name, value);
+                               }
+                       }
+               }
+
+               #endregion
+
+               public const string DefaultWebServicePath = "/Profile_JSON_AppService.axd";
+
+               readonly ScriptingProfileServiceSection _section;
+
+               public ProfileService () {
+                       _section = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
+               }
+
+               ScriptingProfileServiceSection ScriptingProfileServiceSection {
+                       get {
+                               if (_section == null || !_section.Enabled)
+                                       throw new InvalidOperationException ("Profile service is disabled.");
+
+                               return _section;
+                       }
+               }
+
+               [WebMethod()]
+               public IDictionary<string,object> GetAllPropertiesForCurrentUser () {
+                       return new ProfileSerializer (ScriptingProfileServiceSection.ReadAccessProperties);
+               }
+
+               [WebMethod ()]
+               public IDictionary<string, object> GetPropertiesForCurrentUser (string [] properties) {
+                       if (properties == null)
+                               return GetAllPropertiesForCurrentUser ();
+
+                       string [] raProps = ScriptingProfileServiceSection.ReadAccessPropertiesNoCopy;
+
+                       List<string> list = null;
+                       for (int i = 0; i < properties.Length; i++) {
+                               string prop = properties [i];
+                               if (prop == null)
+                                       throw new ArgumentNullException ("properties[" + i + "]");
+
+                               if (IsPropertyConfigured(raProps, prop)) {
+                                       if (list != null)
+                                               list.Add(prop);
+                               }
+                               else if (list == null) {
+                                       list = new List<string> (properties.Length - 1);
+                                       for (int k = 0; k < i; k++)
+                                               list.Add (properties [k]);
+                               }
+                       }
+
+                       return new ProfileSerializer (list != null ? list.ToArray () : properties);
+               }
+
+               [WebMethod ()]
+               public int SetPropertiesForCurrentUser (Dictionary<string, object> values) {
+                       if (values == null)
+                               return 0;
+
+                       string [] waProps = ScriptingProfileServiceSection.WriteAccessPropertiesNoCopy;
+                       
+                       int counter = 0;
+                       ProfileBase profile = HttpContext.Current.Profile;
+                       foreach (KeyValuePair<string, object> pair in values) {
+                               try {
+                                       string name = pair.Key;
+                                       if (!IsPropertyConfigured (waProps, name))
+                                               continue;
+
+                                       int dot = name.IndexOf ('.');
+                                       if (dot > 0)
+                                               profile.GetProfileGroup (name.Substring (0, dot))
+                                                       .SetPropertyValue (name.Substring (dot + 1), pair.Value);
+                                       else
+                                               profile.SetPropertyValue (name, pair.Value);
+                               }
+                               catch {
+                                       continue; //MS seems to ignore errors...
+                               }
+
+                               counter++;
+                       }
+
+                       return counter;
+               }
+
+               static bool IsPropertyConfigured (string [] configuredProperties, string propertyToCheck) {
+                       if (configuredProperties == null)
+                               return false;
+
+                       bool found = false;
+                       for (int i = 0; !found && i < configuredProperties.Length; i++)
+                               found = configuredProperties [i].Equals (propertyToCheck, StringComparison.OrdinalIgnoreCase);
+
+                       return found;
+               }
+       }
+}
index ff0d03f1fe70ce141f3cb0f7b0173a691eab6d96..70d0693134c715cca758df4435d8dd10b328e64b 100644 (file)
@@ -43,11 +43,18 @@ namespace System.Web.Script.Services
                }
                #region IHttpHandlerFactory Members
 
+               static readonly Type TypeOfProfileService = typeof (ProfileService);
                public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated) {
                        HttpRequest request = context.Request;
                        string contentType = request.ContentType;
-                       if (!String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase))
-                               return RestHandler.GetHandler (context, WebServiceParser.GetCompiledType (url, context), url);
+                       if (!String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {
+                               Type handlerType;
+                               if (url.EndsWith (ProfileService.DefaultWebServicePath, StringComparison.Ordinal))
+                                       handlerType = TypeOfProfileService;
+                               else
+                                       handlerType = WebServiceParser.GetCompiledType (url, context);
+                               return RestHandler.GetHandler (context, handlerType, url);
+                       }
                        if (request.PathInfo.StartsWith ("/js", StringComparison.OrdinalIgnoreCase))
                                return new ClientProxyHandler (WebServiceParser.GetCompiledType (url, context), url);
 
index 8bcba893502d5766c531108c0588aa17d40bda63..e52c957ea67c981d531abf824f5ba6ab65af2eb3 100644 (file)
@@ -499,7 +499,7 @@ namespace System.Web.UI
 
                                ScriptingProfileServiceSection profileService = (ScriptingProfileServiceSection) WebConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/profileService");
                                if (profileService.Enabled)
-                                       sb.AppendLine ("Sys.Services._ProfileService.DefaultWebServicePath = '" + ResolveClientUrl ("~/Profile_JSON_AppService.axd") + "';");
+                                       sb.AppendLine ("Sys.Services._ProfileService.DefaultWebServicePath = '" + ResolveClientUrl ("~" + System.Web.Script.Services.ProfileService.DefaultWebServicePath) + "';");
                                if (_profileService != null && !String.IsNullOrEmpty (_profileService.Path))
                                        sb.AppendLine ("Sys.Services.ProfileService.set_path('" + ResolveUrl (_profileService.Path) + "');");