/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* 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.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Alphaleonis.Win32.Security
{
/// 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 using statement to ensure that it is properly disposed when the elevated privileges are no longer needed.
public sealed class PrivilegeEnabler : IDisposable
{
#region PrivilegeEnabler
private readonly List _enabledPrivileges = new List();
/// Initializes a new instance of the class.
/// This will enable the privileges specified (unless already enabled), and ensure that they are disabled again when
/// the object is disposed. (Any privileges already enabled will not be disabled).
///
/// The privilege to enable.
/// Additional privileges to enable.
public PrivilegeEnabler(Privilege privilege, params Privilege[] privileges)
{
_enabledPrivileges.Add(new InternalPrivilegeEnabler(privilege));
if (privileges != null)
foreach (Privilege priv in privileges)
_enabledPrivileges.Add(new InternalPrivilegeEnabler(priv));
}
#endregion // PrivilegeEnabler
#region Dispose
/// Makes sure any privileges enabled by this instance are disabled.
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public void Dispose()
{
foreach (InternalPrivilegeEnabler t in _enabledPrivileges)
{
try
{
t.Dispose();
}
catch
{
// We ignore any exceptions here
}
}
}
#endregion // Dispose
#region EnabledPrivileges
/// 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.
/// The enabled privileges.
public IEnumerable EnabledPrivileges
{
get { return from priv in _enabledPrivileges where priv.EnabledPrivilege != null select priv.EnabledPrivilege; }
}
#endregion // EnabledPrivileges
}
}