File: RoleServiceManager.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (162 lines) | stat: -rw-r--r-- 7,146 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//------------------------------------------------------------------------------
// <copyright file="RoleServiceManager.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
 
namespace System.Web.UI {
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Text;
    using System.Web.Script.Serialization;
    using System.Web;
    using System.Web.ApplicationServices;
    using System.Web.Resources;
    using System.Web.Security;

    [
    DefaultProperty("Path"),
    TypeConverter(typeof(EmptyStringExpandableObjectConverter))
    ]
    public class RoleServiceManager {

        private bool _loadRoles;
        private string _path;

        internal static void ConfigureRoleService(ref StringBuilder sb, HttpContext context, ScriptManager scriptManager, List<ScriptManagerProxy> proxies) {
            string roleServiceUrl = null;
            bool loadRoles = false;
            RoleServiceManager roleManager;

            if(scriptManager.HasRoleServiceManager) {
                roleManager = scriptManager.RoleService;

                // load roles?
                loadRoles = roleManager.LoadRoles;

                // get ScriptManager.Path
                roleServiceUrl = roleManager.Path.Trim();
                if(roleServiceUrl.Length > 0) {
                    roleServiceUrl = scriptManager.ResolveClientUrl(roleServiceUrl);
                }
            }

            // combine proxy ServiceUrls (find the first one that has specified one)
            if(proxies != null) {
                foreach(ScriptManagerProxy proxy in proxies) {
                    if(proxy.HasRoleServiceManager) {
                        roleManager = proxy.RoleService;

                        // combine load roles
                        if (roleManager.LoadRoles) {
                            loadRoles = true;
                        }

                        // combine urls
                        roleServiceUrl = ApplicationServiceManager.MergeServiceUrls(roleManager.Path, roleServiceUrl, proxy);
                    }
                }
            }

            RoleServiceManager.GenerateInitializationScript(ref sb, context, scriptManager, roleServiceUrl, loadRoles);
        }

        private static void GenerateInitializationScript(ref StringBuilder sb, HttpContext context, ScriptManager scriptManager, string serviceUrl, bool loadRoles) {
            bool enabled = ApplicationServiceHelper.RoleServiceEnabled;
            string defaultServicePath = null;

            if (enabled) {
                if (sb == null) {
                    sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
                }

                // The default path points to the built-in service (if it is enabled)
                // Note that the client can't default to this path because it doesn't know what the app root is, we must tell it.
                // We must specify the default path to the proxy even if a custom path is provided, because on the client they could
                // reset the path back to the default if they want.   
                defaultServicePath = scriptManager.ResolveClientUrl("~/" + System.Web.Script.Services.WebServiceData._roleServiceFileName);
                sb.Append("Sys.Services._RoleService.DefaultWebServicePath = '");
                sb.Append(HttpUtility.JavaScriptStringEncode(defaultServicePath));
                sb.Append("';\n");
            }

            bool pathSpecified = !String.IsNullOrEmpty(serviceUrl);
            if (pathSpecified) {
                // DevDiv Bug 71954:When loadRoles="true" and the path is set, we should not load the roles from the default path
                // loadRoles script always retrieves the roles from default role provider, which is not correct if RolesService
                // points to non default path. Hence throw when non default path and loadRoles both are specified.
                if (defaultServicePath == null){
                    defaultServicePath = scriptManager.ResolveClientUrl("~/" + System.Web.Script.Services.WebServiceData._roleServiceFileName);
                }
                if (loadRoles && !String.Equals(serviceUrl, defaultServicePath, StringComparison.OrdinalIgnoreCase)) {
                    throw new InvalidOperationException(AtlasWeb.RoleServiceManager_LoadRolesWithNonDefaultPath);
                }
                if (sb == null) {
                    sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
                }
                sb.Append("Sys.Services.RoleService.set_path('");
                sb.Append(HttpUtility.JavaScriptStringEncode(serviceUrl));
                sb.Append("');\n");
            }

            // Dev10 757178: Do not attempt during design mode. This code isn't important for intellisense anyway.
            if (loadRoles) {
                if (scriptManager.DesignMode) {
                    // Dev10 757178: Do not lookup user roles at design time.
                    // But at DesignTime this method is only important because if it produces any init script,
                    // it prompts AddFrameworkScripts to add the MicrosoftAjaxApplicationServices.js script reference. 
                    // So just append a comment to ensure at least some script is generated.
                    if (sb == null) {
                        sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
                    }
                    sb.Append("// loadRoles\n");                    
                }
                else {
                    string[] roles = Roles.GetRolesForUser();
                    if(roles != null && roles.Length > 0) {
                        if (sb == null) {
                            sb = new StringBuilder(ApplicationServiceManager.StringBuilderCapacity);
                        }
                        sb.Append("Sys.Services.RoleService._roles = ");
                        sb.Append(new JavaScriptSerializer().Serialize(roles, JavaScriptSerializer.SerializationFormat.JavaScript));
                        sb.Append(";\n");
                    }
                }
            }
        }

        [
        DefaultValue(false),
        Category("Behavior"),
        NotifyParentProperty(true),
        ResourceDescription("RoleServiceManager_LoadRoles")
        ]
        public bool LoadRoles {
            get {
                return _loadRoles;
            }
            set {
                _loadRoles = value;
            }
        }

        [
        DefaultValue(""),
        Category("Behavior"),
        NotifyParentProperty(true),
        ResourceDescription("ApplicationServiceManager_Path"),
        UrlProperty()
        ]
        public string Path
        {
            get {
                return _path ?? String.Empty;
            }
            set {
                _path = value;
            }
        }
    }
}