File: PersonalizationProvider.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 (311 lines) | stat: -rw-r--r-- 13,072 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//------------------------------------------------------------------------------
// <copyright file="PersonalizationProvider.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls.WebParts {

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Configuration;
    using System.Configuration.Provider;
    using System.Security.Principal;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.Hosting;
    using System.Web.Util;

    /// <devdoc>
    /// The provider used to access the personalization store for WebPart pages.
    /// </devdoc>
    public abstract class PersonalizationProvider : ProviderBase {

        private const string scopeFieldName = "__WPPS";
        private const string sharedScopeFieldValue = "s";
        private const string userScopeFieldValue = "u";

        private ICollection _supportedUserCapabilities;

        /// <devdoc>
        /// Initializes an instance of PersonalizationProvider.
        /// </devdoc>
        protected PersonalizationProvider() {
        }

        /// <devdoc>
        /// The name of the application that this provider should use to store
        /// and retrieve personalization data from.
        /// </devdoc>
        public abstract string ApplicationName { get; set; }

        /// <devdoc>
        /// </devdoc>
        protected virtual IList CreateSupportedUserCapabilities() {
            ArrayList list = new ArrayList();

            list.Add(WebPartPersonalization.EnterSharedScopeUserCapability);
            list.Add(WebPartPersonalization.ModifyStateUserCapability);

            return list;
        }

        /// <devdoc>
        /// </devdoc>
        public virtual PersonalizationScope DetermineInitialScope(WebPartManager webPartManager, PersonalizationState loadedState) {
            if (webPartManager == null) {
                throw new ArgumentNullException("webPartManager");
            }

            Page page = webPartManager.Page;
            if (page == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page"),
                                            "webPartManager");
            }

            HttpRequest request = page.RequestInternal;
            if (request == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page.Request"),
                                            "webPartManager");
            }

            PersonalizationScope scope = webPartManager.Personalization.InitialScope;

            IPrincipal user = null;
            if (request.IsAuthenticated) {
                user = page.User;
            }

            if (user == null) {
                // if no user has been authenticated, then just load all user data
                scope = PersonalizationScope.Shared;
            }
            else {
                if (page.IsPostBack) {
                    string postedMode = page.Request[scopeFieldName];
                    if (postedMode == sharedScopeFieldValue) {
                        scope = PersonalizationScope.Shared;
                    }
                    else if (postedMode == userScopeFieldValue) {
                        scope = PersonalizationScope.User;
                    }
                }
                else if ((page.PreviousPage != null) &&
                         (page.PreviousPage.IsCrossPagePostBack == false)) {
                    WebPartManager previousWebPartManager = WebPartManager.GetCurrentWebPartManager(page.PreviousPage);

                    if (previousWebPartManager != null) {
                        // Note that we check the types of the page, so we don't
                        // look the at the PreviousPage in a cross-page posting scenario
                        scope = previousWebPartManager.Personalization.Scope;
                    }
                }
                // Special-case Web Part Export so it executes in the same security context as the page itself (VSWhidbey 426574)
                // Setting the initial scope from what's been asked for in the export parameters
                else if (page.IsExportingWebPart) {
                    scope = (page.IsExportingWebPartShared ? PersonalizationScope.Shared : PersonalizationScope.User);
                }

                if ((scope == PersonalizationScope.Shared) &&
                    (webPartManager.Personalization.CanEnterSharedScope == false)) {
                    scope = PersonalizationScope.User;
                }
            }

            string fieldValue = (scope == PersonalizationScope.Shared) ? sharedScopeFieldValue : userScopeFieldValue;
            page.ClientScript.RegisterHiddenField(scopeFieldName, fieldValue);

            return scope;
        }

        /// <devdoc>
        /// </devdoc>
        public virtual IDictionary DetermineUserCapabilities(WebPartManager webPartManager) {
            if (webPartManager == null) {
                throw new ArgumentNullException("webPartManager");
            }

            Page page = webPartManager.Page;
            if (page == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page"),
                                            "webPartManager");
            }

            HttpRequest request = page.RequestInternal;
            if (request == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page.Request"),
                                            "webPartManager");
            }

            IPrincipal user = null;
            if (request.IsAuthenticated) {
                user = page.User;
            }

            if (user != null) {
                if (_supportedUserCapabilities == null) {
                    _supportedUserCapabilities = CreateSupportedUserCapabilities();
                }

                if ((_supportedUserCapabilities != null) && (_supportedUserCapabilities.Count != 0)) {
                    WebPartsSection configSection = RuntimeConfig.GetConfig().WebParts;
                    if (configSection != null) {
                        WebPartsPersonalizationAuthorization authConfig = configSection.Personalization.Authorization;
                        if (authConfig != null) {
                            IDictionary capabilities = new HybridDictionary();

                            foreach (WebPartUserCapability capability in _supportedUserCapabilities) {
                                if (authConfig.IsUserAllowed(user, capability.Name)) {
                                    capabilities[capability] = capability;
                                }
                            }
                            return capabilities;
                        }
                    }
                }
            }

            return new HybridDictionary();
        }

        public abstract PersonalizationStateInfoCollection FindState(PersonalizationScope scope,
                                                                     PersonalizationStateQuery query,
                                                                     int pageIndex, int pageSize,
                                                                     out int totalRecords);

        public abstract int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query);

        /// <devdoc>
        /// </devdoc>
        private void GetParameters(WebPartManager webPartManager, out string path, out string userName) {
            if (webPartManager == null) {
                throw new ArgumentNullException("webPartManager");
            }

            Page page = webPartManager.Page;
            if (page == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page"),
                                            "webPartManager");
            }

            HttpRequest request = page.RequestInternal;
            if (request == null) {
                throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "Page.Request"),
                                            "webPartManager");
            }

            path = request.AppRelativeCurrentExecutionFilePath;
            userName = null;

            if ((webPartManager.Personalization.Scope == PersonalizationScope.User) && page.Request.IsAuthenticated) {
                userName = page.User.Identity.Name;
            }
        }

        /// <devdoc>
        /// Loads the data from the data store for the specified path and user.
        /// If only shared data is to be loaded, userName is null or empty.
        /// </devdoc>
        protected abstract void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob);

        /// <devdoc>
        /// Allows the provider to load personalization data. The specified
        /// WebPartManager is used to access the current page, which can be used
        /// to retrieve the path and user information.
        /// </devdoc>
        public virtual PersonalizationState LoadPersonalizationState(WebPartManager webPartManager, bool ignoreCurrentUser) {
            if (webPartManager == null) {
                throw new ArgumentNullException("webPartManager");
            }

            string path;
            string userName;
            GetParameters(webPartManager, out path, out userName);

            if (ignoreCurrentUser) {
                userName = null;
            }

            byte[] sharedDataBlob = null;
            byte[] userDataBlob = null;
            LoadPersonalizationBlobs(webPartManager, path, userName, ref sharedDataBlob, ref userDataBlob);

            BlobPersonalizationState blobState = new BlobPersonalizationState(webPartManager);
            blobState.LoadDataBlobs(sharedDataBlob, userDataBlob);

            return blobState;
        }

        /// <devdoc>
        /// Removes the data from the data store for the specified path and user.
        /// If userName is null or empty, the shared data is to be reset.
        /// </devdoc>
        protected abstract void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName);

        /// <devdoc>
        /// Allows the provider to reset personalization data. The specified
        /// WebPartManager is used to access the current page, which can be used
        /// to retrieve the path and user information.
        /// </devdoc>
        public virtual void ResetPersonalizationState(WebPartManager webPartManager) {
            if (webPartManager == null) {
                throw new ArgumentNullException("webPartManager");
            }

            string path;
            string userName;
            GetParameters(webPartManager, out path, out userName);

            ResetPersonalizationBlob(webPartManager, path, userName);
        }

        public abstract int ResetState(PersonalizationScope scope, string[] paths, string[] usernames);

        public abstract int ResetUserState(string path, DateTime userInactiveSinceDate);

        /// <devdoc>
        /// Saves the data into the data store for the specified path and user.
        /// If only shared data is to be saved, userName is null or empty.
        /// </devdoc>
        protected abstract void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob);

        /// <devdoc>
        /// Allows the provider to save personalization data. The specified information
        /// contains a reference to the WebPartManager, which is used to access the
        /// current Page, and its path and user information.
        /// </devdoc>
        public virtual void SavePersonalizationState(PersonalizationState state) {
            if (state == null) {
                throw new ArgumentNullException("state");
            }

            BlobPersonalizationState blobState = state as BlobPersonalizationState;
            if (blobState == null) {
                throw new ArgumentException(SR.GetString(SR.PersonalizationProvider_WrongType), "state");
            }

            WebPartManager webPartManager = blobState.WebPartManager;

            string path;
            string userName;
            GetParameters(webPartManager, out path, out userName);

            byte[] dataBlob = null;
            bool reset = blobState.IsEmpty;

            if (reset == false) {
                dataBlob = blobState.SaveDataBlob();
                reset = (dataBlob == null) || (dataBlob.Length == 0);
            }

            if (reset) {
                ResetPersonalizationBlob(webPartManager, path, userName);
            }
            else {
                SavePersonalizationBlob(webPartManager, path, userName, dataBlob);
            }
        }
    }
}