Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / BindAttribute.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Linq;\r
16 \r
17     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]\r
18     public sealed class BindAttribute : Attribute {\r
19 \r
20         private string _exclude;\r
21         private string[] _excludeSplit = new string[0];\r
22         private string _include;\r
23         private string[] _includeSplit = new string[0];\r
24 \r
25         public string Exclude {\r
26             get {\r
27                 return _exclude ?? String.Empty;\r
28             }\r
29             set {\r
30                 _exclude = value;\r
31                 _excludeSplit = AuthorizeAttribute.SplitString(value);\r
32             }\r
33         }\r
34 \r
35         public string Include {\r
36             get {\r
37                 return _include ?? String.Empty;\r
38             }\r
39             set {\r
40                 _include = value;\r
41                 _includeSplit = AuthorizeAttribute.SplitString(value);\r
42             }\r
43         }\r
44 \r
45         public string Prefix {\r
46             get;\r
47             set;\r
48         }\r
49 \r
50         internal static bool IsPropertyAllowed(string propertyName, string[] includeProperties, string[] excludeProperties) {\r
51             // We allow a property to be bound if its both in the include list AND not in the exclude list.\r
52             // An empty include list implies all properties are allowed.\r
53             // An empty exclude list implies no properties are disallowed.\r
54             bool includeProperty = (includeProperties == null) || (includeProperties.Length == 0) || includeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase);\r
55             bool excludeProperty = (excludeProperties != null) && excludeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase);\r
56             return includeProperty && !excludeProperty;\r
57         }\r
58 \r
59         public bool IsPropertyAllowed(string propertyName) {\r
60             return IsPropertyAllowed(propertyName, _includeSplit, _excludeSplit);\r
61         }\r
62     }\r
63 }\r