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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
//------------------------------------------------------------------------------
// <copyright file="WebPartPersonalization.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.UI;
using System.Web.Util;
using System.Web.Hosting;
[TypeConverterAttribute(typeof(EmptyStringExpandableObjectConverter))]
public class WebPartPersonalization {
public static readonly WebPartUserCapability ModifyStateUserCapability = new WebPartUserCapability("modifyState");
public static readonly WebPartUserCapability EnterSharedScopeUserCapability = new WebPartUserCapability("enterSharedScope");
private WebPartManager _owner;
// Properties
private bool _enabled;
private string _providerName;
private PersonalizationScope _initialScope;
// Computed state
private bool _initialized;
private bool _initializedSet;
private PersonalizationProvider _provider;
private PersonalizationScope _currentScope;
private IDictionary _userCapabilities;
private PersonalizationState _personalizationState;
private bool _scopeToggled;
private bool _shouldResetPersonalizationState;
/// <devdoc>
/// </devdoc>
public WebPartPersonalization(WebPartManager owner) {
if (owner == null) {
throw new ArgumentNullException("owner");
}
_owner = owner;
_enabled = true;
}
/// <devdoc>
/// Indicates whether the current user has the permissions to switch
/// into shared personalization scope.
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public bool CanEnterSharedScope {
get {
// We cannot cache this value, since UserCapabilities is protected virtual,
// and could return a different value at any time
IDictionary userCapabilities = UserCapabilities;
bool canEnterSharedScope = (userCapabilities != null) &&
(userCapabilities.Contains(WebPartPersonalization.EnterSharedScopeUserCapability));
return canEnterSharedScope;
}
}
/// <devdoc>
/// </devdoc>
[
DefaultValue(true),
NotifyParentProperty(true),
WebSysDescription(SR.WebPartPersonalization_Enabled)
]
public virtual bool Enabled {
get {
return _enabled;
}
set {
if (!WebPartManager.DesignMode && _initializedSet && (value != Enabled)) {
throw new InvalidOperationException(
SR.GetString(SR.WebPartPersonalization_MustSetBeforeInit, "Enabled", "WebPartPersonalization"));
}
_enabled = value;
}
}
// Returns true if the current user in the current scope on the current page has personalization data
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public virtual bool HasPersonalizationState {
get {
if (_provider == null) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_CantUsePropertyBeforeInit,
"HasPersonalizationState", "WebPartPersonalization"));
}
Page page = WebPartManager.Page;
if (page == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page"));
}
HttpRequest request = page.RequestInternal;
if (request == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page.Request"));
}
PersonalizationStateQuery query = new PersonalizationStateQuery();
query.PathToMatch = request.AppRelativeCurrentExecutionFilePath;
if (Scope == PersonalizationScope.User && request.IsAuthenticated) {
query.UsernameToMatch = page.User.Identity.Name;
}
return (_provider.GetCountOfState(Scope, query) > 0);
}
}
/// <devdoc>
/// Allows changing the initial personalization scope that is given
/// preference when requesting the page on its first request.
/// This must be set before personalization data is loaded into the
/// WebPartManager.
/// </devdoc>
[
DefaultValue(PersonalizationScope.User),
NotifyParentProperty(true),
WebSysDescription(SR.WebPartPersonalization_InitialScope)
]
public virtual PersonalizationScope InitialScope {
get {
return _initialScope;
}
set {
if ((value < PersonalizationScope.User) || (value > PersonalizationScope.Shared)) {
throw new ArgumentOutOfRangeException("value");
}
if (!WebPartManager.DesignMode && _initializedSet && (value != InitialScope)) {
throw new InvalidOperationException(
SR.GetString(SR.WebPartPersonalization_MustSetBeforeInit, "InitialScope", "WebPartPersonalization"));
}
_initialScope = value;
}
}
/// <devdoc>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public bool IsEnabled {
get {
return IsInitialized;
}
}
/// <devdoc>
/// Indicates whether personalization state has been loaded. Properties of this
/// object cannot be saved once this object is initialized.
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
protected bool IsInitialized {
get {
return _initialized;
}
}
/// <devdoc>
/// Determines if personalization and the ability to modify personalization state is enabled
/// in the current request. This depends on whether a user is authenticated for this request,
/// and if that user has the rights to modify personalization state.
/// To check if just personalization is enabled at all, the IsEnabled property
/// should be used.
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public bool IsModifiable {
get {
// We cannot cache this value, since UserCapabilities is protected virtual,
// and could return a different value at any time
IDictionary userCapabilities = UserCapabilities;
bool isModifiable = (userCapabilities != null) &&
(userCapabilities.Contains(WebPartPersonalization.ModifyStateUserCapability));
return isModifiable;
}
}
/// <devdoc>
/// </devdoc>
[
DefaultValue(""),
NotifyParentProperty(true),
WebSysDescription(SR.WebPartPersonalization_ProviderName)
]
public virtual string ProviderName {
get {
return (_providerName != null) ? _providerName : String.Empty;
}
set {
if (!WebPartManager.DesignMode && _initializedSet &&
!String.Equals(value, ProviderName, StringComparison.Ordinal)) {
throw new InvalidOperationException(
SR.GetString(SR.WebPartPersonalization_MustSetBeforeInit, "ProviderName", "WebPartPersonalization"));
}
_providerName = value;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public PersonalizationScope Scope {
get {
return _currentScope;
}
}
internal bool ScopeToggled {
get {
return _scopeToggled;
}
}
// True if the personalization data was reset this request. If so, we will not save data
// at the end of the request.
protected bool ShouldResetPersonalizationState {
get {
return _shouldResetPersonalizationState;
}
set {
_shouldResetPersonalizationState = value;
}
}
protected virtual IDictionary UserCapabilities {
get {
if (_userCapabilities == null) {
_userCapabilities = new HybridDictionary();
}
return _userCapabilities;
}
}
protected WebPartManager WebPartManager {
get {
return _owner;
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void ApplyPersonalizationState() {
if (IsEnabled) {
EnsurePersonalizationState();
_personalizationState.ApplyWebPartManagerPersonalization();
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void ApplyPersonalizationState(WebPart webPart) {
if (webPart == null) {
throw new ArgumentNullException("webPart");
}
if (IsEnabled) {
EnsurePersonalizationState();
_personalizationState.ApplyWebPartPersonalization(webPart);
}
}
// Helper method used by CopyPersonalizationState()
private void ApplyPersonalizationState(Control control, PersonalizationInfo info) {
ITrackingPersonalizable trackingPersonalizable = control as ITrackingPersonalizable;
IPersonalizable customPersonalizable = control as IPersonalizable;
if (trackingPersonalizable != null) {
trackingPersonalizable.BeginLoad();
}
// If customPersonalizable is null, then info.CustomProperties should also be null
Debug.Assert(!(customPersonalizable == null && info.CustomProperties != null));
if (customPersonalizable != null && info.CustomProperties != null && info.CustomProperties.Count > 0) {
customPersonalizable.Load(info.CustomProperties);
}
if (info.Properties != null && info.Properties.Count > 0) {
BlobPersonalizationState.SetPersonalizedProperties(control, info.Properties);
}
if (trackingPersonalizable != null) {
trackingPersonalizable.EndLoad();
}
}
protected virtual void ChangeScope(PersonalizationScope scope) {
PersonalizationProviderHelper.CheckPersonalizationScope(scope);
if (scope == _currentScope) {
return;
}
if ((scope == PersonalizationScope.Shared) && (!CanEnterSharedScope)) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_CannotEnterSharedScope));
}
_currentScope = scope;
_scopeToggled = true;
}
// Extracts the personalization state from webPartA, and applies it to webPartB.
// Assumes that webPartA and webPartB are the same type. If the WebParts are GenericWebParts,
// then copies the personalization state from the ChildControl of webPartA to the
// ChildControl of webPartB.
protected internal virtual void CopyPersonalizationState(WebPart webPartA, WebPart webPartB) {
if (webPartA == null) {
throw new ArgumentNullException("webPartA");
}
if (webPartB == null) {
throw new ArgumentNullException("webPartB");
}
if (webPartA.GetType() != webPartB.GetType()) {
throw new ArgumentException(SR.GetString(SR.WebPartPersonalization_SameType, "webPartA", "webPartB"));
}
CopyPersonalizationState((Control)webPartA, (Control)webPartB);
GenericWebPart genericWebPartA = webPartA as GenericWebPart;
GenericWebPart genericWebPartB = webPartB as GenericWebPart;
// Assert that the GenericWebParts are either both null or both non-null
Debug.Assert((genericWebPartA == null) == (genericWebPartB == null));
if (genericWebPartA != null && genericWebPartB != null) {
Control childControlA = genericWebPartA.ChildControl;
Control childControlB = genericWebPartB.ChildControl;
if (childControlA == null) {
throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "ChildControl"), "webPartA");
}
if (childControlB == null) {
throw new ArgumentException(SR.GetString(SR.PropertyCannotBeNull, "ChildControl"), "webPartB");
}
if (childControlA.GetType() != childControlB.GetType()) {
throw new ArgumentException(SR.GetString(SR.WebPartPersonalization_SameType, "webPartA.ChildControl", "webPartB.ChildControl"));
}
CopyPersonalizationState(childControlA, childControlB);
}
// IPersonalizable.IsDirty should always be false on the new WebPart, since the only data
// on the new WebPart was loaded via personalization, which should not cause the control
// to be dirty. However, we want to save the IPersonalizable data on this request, so
// we call SetDirty() to force the IPersonalizable data to be saved.
SetDirty(webPartB);
}
private void CopyPersonalizationState(Control controlA, Control controlB) {
PersonalizationInfo info = ExtractPersonalizationState(controlA);
ApplyPersonalizationState(controlB, info);
}
/// <devdoc>
/// </devdoc>
private void DeterminePersonalizationProvider() {
string providerName = ProviderName;
if (String.IsNullOrEmpty(providerName)) {
// Use the default provider
_provider = PersonalizationAdministration.Provider;
// The default provider can never be null
Debug.Assert(_provider != null);
}
else {
// Look for a provider with the specified name
PersonalizationProvider provider = PersonalizationAdministration.Providers[providerName];
if (provider != null) {
_provider = provider;
}
else {
throw new ProviderException(
SR.GetString(SR.WebPartPersonalization_ProviderNotFound, providerName));
}
}
}
/// <devdoc>
/// </devdoc>
public void EnsureEnabled(bool ensureModifiable) {
bool value = (ensureModifiable ? IsModifiable : IsEnabled);
if (!value) {
string message;
if (ensureModifiable) {
message = SR.GetString(SR.WebPartPersonalization_PersonalizationNotModifiable);
}
else {
message = SR.GetString(SR.WebPartPersonalization_PersonalizationNotEnabled);
}
throw new InvalidOperationException(message);
}
}
private void EnsurePersonalizationState() {
if (_personalizationState == null) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_PersonalizationStateNotLoaded));
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void ExtractPersonalizationState() {
// If we reset the personalization data on this request, we should not extract data since
// it will not be saved.
if (IsEnabled && !ShouldResetPersonalizationState) {
EnsurePersonalizationState();
_personalizationState.ExtractWebPartManagerPersonalization();
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void ExtractPersonalizationState(WebPart webPart) {
// If we reset the personalization data on this request, we should not extract data since
// it will not be saved.
if (IsEnabled && !ShouldResetPersonalizationState) {
EnsurePersonalizationState();
_personalizationState.ExtractWebPartPersonalization(webPart);
}
}
// Helper method used by CopyPersonalizationState()
private PersonalizationInfo ExtractPersonalizationState(Control control) {
ITrackingPersonalizable trackingPersonalizable = control as ITrackingPersonalizable;
IPersonalizable customPersonalizable = control as IPersonalizable;
if (trackingPersonalizable != null) {
trackingPersonalizable.BeginSave();
}
PersonalizationInfo info = new PersonalizationInfo();
if (customPersonalizable != null) {
info.CustomProperties = new PersonalizationDictionary();
customPersonalizable.Save(info.CustomProperties);
}
info.Properties = BlobPersonalizationState.GetPersonalizedProperties(control, PersonalizationScope.Shared);
if (trackingPersonalizable != null) {
trackingPersonalizable.EndSave();
}
return info;
}
// Returns the AuthorizationFilter string for a WebPart before it is instantiated
// Returns null if there is no personalized value for AuthorizationFilter
protected internal virtual string GetAuthorizationFilter(string webPartID) {
if (String.IsNullOrEmpty(webPartID)) {
throw ExceptionUtil.ParameterNullOrEmpty("webPartID");
}
EnsureEnabled(false);
EnsurePersonalizationState();
return _personalizationState.GetAuthorizationFilter(webPartID);
}
/// <devdoc>
/// </devdoc>
internal void LoadInternal() {
if (Enabled) {
_currentScope = Load();
_initialized = true;
}
_initializedSet = true;
}
/// <devdoc>
/// Loads personalization information from its storage, and computes the initial personalization
/// scope. This method determines the provider type to be used, and the current user's capabilities.
/// </devdoc>
protected virtual PersonalizationScope Load() {
if (!Enabled) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_PersonalizationNotEnabled));
}
// Determine the provider early, as it is needed to continue execution.
// Provider is used to detect user's capabilities, load personalization state
// and determine initial scope.
DeterminePersonalizationProvider();
Debug.Assert(_provider != null);
Page page = WebPartManager.Page;
if (page == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page"));
}
HttpRequest request = page.RequestInternal;
if (request == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page.Request"));
}
// Ask the provider to load information about what are the capabilities of
// the current user
if (request.IsAuthenticated) {
_userCapabilities = _provider.DetermineUserCapabilities(WebPartManager);
}
// A derived WebPartPersonalization can have ignoreCurrentUser to be true.
_personalizationState = _provider.LoadPersonalizationState(WebPartManager, /* ignoreCurrentUser */ false);
if (_personalizationState == null) {
// We can't assume that _personalizationState will be non-null, because
// it depends on the provider implementation.
throw new ProviderException(SR.GetString(SR.WebPartPersonalization_CannotLoadPersonalization));
}
return _provider.DetermineInitialScope(WebPartManager, _personalizationState);
}
// Resets the personalization data for the current user in the current scope on the current page
public virtual void ResetPersonalizationState() {
EnsureEnabled(/* ensureModifiable */ true);
if (_provider == null) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_CantCallMethodBeforeInit,
"ResetPersonalizationState", "WebPartPersonalization"));
}
_provider.ResetPersonalizationState(WebPartManager);
ShouldResetPersonalizationState = true;
Page page = WebPartManager.Page;
if (page == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page"));
}
// Transfer execution to a new instance of the same page. The new page will execute
// after personalization data has been reset.
TransferToCurrentPage(page);
}
/// <devdoc>
/// </devdoc>
internal void SaveInternal() {
if (IsModifiable) {
Save();
}
}
/// <devdoc>
/// Saves personalization information back to its storage if necessary.
/// </devdoc>
protected virtual void Save() {
EnsureEnabled(/* ensureModifiable */ true);
EnsurePersonalizationState();
if (_provider == null) {
throw new InvalidOperationException(SR.GetString(SR.WebPartPersonalization_CantCallMethodBeforeInit,
"Save", "WebPartPersonalization"));
}
// If we reset the personalization data on this request, we should not save data to the
// DB on this request. It is likely we would not save data anyway, since the data probably
// did not change since the last request, but there are some scenarios where the data could
// have changed (like a WebPart using personalization as a cache).
if (_personalizationState.IsDirty && !ShouldResetPersonalizationState) {
_provider.SavePersonalizationState(_personalizationState);
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void SetDirty() {
if (IsEnabled) {
EnsurePersonalizationState();
_personalizationState.SetWebPartManagerDirty();
}
}
/// <devdoc>
/// </devdoc>
protected internal virtual void SetDirty(WebPart webPart) {
if (IsEnabled) {
EnsurePersonalizationState();
_personalizationState.SetWebPartDirty(webPart);
}
}
/// <devdoc>
/// </devdoc>
public virtual void ToggleScope() {
EnsureEnabled(/* ensureModifiable */ false);
Page page = WebPartManager.Page;
if (page == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page"));
}
if (page.IsExportingWebPart) {
// If the page is exporting, the page determines the desired scope, and it is not meaningful
// to toggle the scope. Note that we no-op the call, rather than throwing because
// that would require exposing a CanToggleScope property, require page developers
// to check for it. Furthermore, we don't guarantee ToggleScope does toggle
// (eg. in the case of absense of user capability to enter shared scope), so
// simply no-op'ing isn't terribly bad...
return;
}
Page previousPage = page.PreviousPage;
if ((previousPage != null) &&
(previousPage.IsCrossPagePostBack == false)) {
WebPartManager previousWebPartManager = WebPartManager.GetCurrentWebPartManager(previousPage);
if ((previousWebPartManager != null) && (previousWebPartManager.Personalization.ScopeToggled)) {
// Looks like some sort of infinite recursion is going on
// and we're toggling again. Like the previous case, we're
// going to no-op and protect ourselves from "----" code...
return;
}
}
if (_currentScope == PersonalizationScope.Shared) {
ChangeScope(PersonalizationScope.User);
}
else {
ChangeScope(PersonalizationScope.Shared);
}
// Transfer execution to a new instance of the same page. The new page will detect the scope
// it should run in, when it begins to load personalization data.
TransferToCurrentPage(page);
}
// Transfers execution to a new instance of the same page. We need to clear the form collection,
// since it should not carry over to the page in the new scope (i.e. ViewState). If the form
// method is GET, then we must not include the query string, since the entire form collection
// is in the query string. If the form method is POST (or there is no form), then we must
// include the query string, since the developer could be using the query string to drive the
// logic of their page (VSWhidbey 444385 and 527117).
private void TransferToCurrentPage(Page page) {
HttpRequest request = page.RequestInternal;
if (request == null) {
throw new InvalidOperationException(SR.GetString(SR.PropertyCannotBeNull, "WebPartManager.Page.Request"));
}
string path = request.CurrentExecutionFilePath;
if (page.Form == null || String.Equals(page.Form.Method, "post", StringComparison.OrdinalIgnoreCase)) {
string queryString = page.ClientQueryString;
if (!String.IsNullOrEmpty(queryString)) {
path += "?" + queryString;
}
}
IScriptManager scriptManager = page.ScriptManager;
if ((scriptManager != null) && scriptManager.IsInAsyncPostBack) {
request.Response.Redirect(path);
}
else {
page.Server.Transfer(path, /* preserveForm */ false);
}
}
private sealed class PersonalizationInfo {
public IDictionary Properties;
public PersonalizationDictionary CustomProperties;
}
}
}
|