//****************************************************************************************************** // RestrictAccessAttribute.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 01/06/2011 - Pinal C. Patel // Generated original version of source code. // 12/20/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Threading; namespace GSF.Security { /// /// Represents an that can be used restrict access to a class when using role-based security. /// [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class RestrictAccessAttribute : Attribute { #region [ Members ] // Fields private string[] m_roles; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public RestrictAccessAttribute() : this(null) { } /// /// Initializes a new instance of the class. /// /// List of either roles the current thread principal must have in order to have access. public RestrictAccessAttribute(params string[] roles) { m_roles = roles; } #endregion #region [ Properties ] /// /// Gets or sets the list of either roles the current thread principal must have in order to have access. /// public string[] Roles { get { return m_roles; } set { m_roles = value; } } #endregion #region [ Methods ] /// /// Checks if the current thread principal has at least one of the in order to have access. /// /// true if the current thread principal has access, otherwise false. public bool CheckAccess() { if (m_roles != null) { // One or more roles have been specified. foreach (string role in m_roles) { // Check role against principal's role membership. if (Thread.CurrentPrincipal.IsInRole(role)) // Principal has membership to the role so allow access. return true; } } return false; } #endregion } }