using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.ComponentModel;
namespace Csla.Security
{
  /// 
  /// Maintains a list of allowed and denied
  /// user roles for a specific property.
  /// 
  /// 
  [Serializable()]
  internal class RolesForProperty
  {
    private List _readAllowed = new List();
    private List _readDenied = new List();
    private List _writeAllowed = new List();
    private List _writeDenied = new List();
    /// 
    /// Returns a List(Of string) containing the list
    /// of roles allowed read access.
    /// 
    public List ReadAllowed
    {
      get { return _readAllowed; }
    }
    /// 
    /// Returns a List(Of string) containing the list
    /// of roles denied read access.
    /// 
    public List ReadDenied
    {
      get { return _readDenied; }
    }
    /// 
    /// Returns a List(Of string) containing the list
    /// of roles allowed write access.
    /// 
    public List WriteAllowed
    {
      get { return _writeAllowed; }
    }
    /// 
    /// Returns a List(Of string) containing the list
    /// of roles denied write access.
    /// 
    public List WriteDenied
    {
      get { return _writeDenied; }
    }
    /// 
    /// Returns  if the user is in a role
    /// explicitly allowed read access.
    /// 
    /// A 
    /// representing the user.
    ///  if the user is allowed read access.
    /// 
    public bool IsReadAllowed(IPrincipal principal)
    {
      foreach (string role in ReadAllowed)
        if (principal.IsInRole(role))
          return true;
      return false;
    }
    /// 
    /// Returns  if the user is in a role
    /// explicitly denied read access.
    /// 
    /// A 
    /// representing the user.
    ///  if the user is denied read access.
    /// 
    public bool IsReadDenied(IPrincipal principal)
    {
      foreach (string role in ReadDenied)
        if (principal.IsInRole(role))
          return true;
      return false;
    }
    /// 
    /// Returns  if the user is in a role
    /// explicitly allowed write access.
    /// 
    /// A 
    /// representing the user.
    ///  if the user is allowed write access.
    /// 
    public bool IsWriteAllowed(IPrincipal principal)
    {
      foreach (string role in WriteAllowed)
        if (principal.IsInRole(role))
          return true;
      return false;
    }
    /// 
    /// Returns  if the user is in a role
    /// explicitly denied write access.
    /// 
    /// A 
    /// representing the user.
    ///  if the user is denied write access.
    /// 
    public bool IsWriteDenied(IPrincipal principal)
    {
      foreach (string role in WriteDenied)
        if (principal.IsInRole(role))
          return true;
      return false;
    }
  }
}