25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrivilegeEnabler.cs 3.7 KiB

7 년 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Diagnostics.CodeAnalysis;
  24. using System.Linq;
  25. namespace Alphaleonis.Win32.Security
  26. {
  27. /// <summary>Used to enable one or more privileges. The privileges specified will be enabled during the lifetime of the instance. Users create an instance of this object in a <c>using</c> statement to ensure that it is properly disposed when the elevated privileges are no longer needed.</summary>
  28. public sealed class PrivilegeEnabler : IDisposable
  29. {
  30. #region PrivilegeEnabler
  31. private readonly List<InternalPrivilegeEnabler> _enabledPrivileges = new List<InternalPrivilegeEnabler>();
  32. /// <summary>Initializes a new instance of the <see cref="PrivilegeEnabler"/> class.
  33. /// This will enable the privileges specified (unless already enabled), and ensure that they are disabled again when
  34. /// the object is disposed. (Any privileges already enabled will not be disabled).
  35. /// </summary>
  36. /// <param name="privilege">The privilege to enable.</param>
  37. /// <param name="privileges">Additional privileges to enable.</param>
  38. public PrivilegeEnabler(Privilege privilege, params Privilege[] privileges)
  39. {
  40. _enabledPrivileges.Add(new InternalPrivilegeEnabler(privilege));
  41. if (privileges != null)
  42. foreach (Privilege priv in privileges)
  43. _enabledPrivileges.Add(new InternalPrivilegeEnabler(priv));
  44. }
  45. #endregion // PrivilegeEnabler
  46. #region Dispose
  47. /// <summary>Makes sure any privileges enabled by this instance are disabled.</summary>
  48. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  49. public void Dispose()
  50. {
  51. foreach (InternalPrivilegeEnabler t in _enabledPrivileges)
  52. {
  53. try
  54. {
  55. t.Dispose();
  56. }
  57. catch
  58. {
  59. // We ignore any exceptions here
  60. }
  61. }
  62. }
  63. #endregion // Dispose
  64. #region EnabledPrivileges
  65. /// <summary>Gets the enabled privileges. Note that this might not contain all privileges specified to the constructor. Only the privileges actually enabled by this instance is returned.</summary>
  66. /// <value>The enabled privileges.</value>
  67. public IEnumerable<Privilege> EnabledPrivileges
  68. {
  69. get { return from priv in _enabledPrivileges where priv.EnabledPrivilege != null select priv.EnabledPrivilege; }
  70. }
  71. #endregion // EnabledPrivileges
  72. }
  73. }