More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Attributes / AccessibleThroughAttribute.cs
blob3b23d2983606d2f5f1e75cc7cde788d7c13af913
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework
17 using System;
19 /// <summary>
20 /// Enum to identify a http verb
21 /// </summary>
22 [Flags]
23 public enum Verb
25 /// <summary>
26 /// Not defined
27 /// </summary>
28 Undefined,
29 /// <summary>
30 /// The GET method means retrieve whatever information is identified by the Request-URI.
31 /// <remarks>
32 /// The convention has been established that the GET method SHOULD
33 /// NOT have the significance of taking an action other than retrieval.
34 /// </remarks>
35 /// </summary>
36 Get = 1,
37 /// <summary>
38 /// The POST method is used to request that the origin server accept the entity
39 /// enclosed in the request as a new subordinate of the resource identified by the
40 /// Request-URI in the Request-Line.
41 /// <remarks>
42 /// The convention has been established that the POST method will
43 /// take an action other than just retrieval.
44 /// </remarks>
45 /// </summary>
46 Post = 2,
47 /// <summary>
48 /// Just like a <see cref="Get"/> but without returning the body.
49 /// </summary>
50 Head = 4,
51 /// <summary>
52 /// Query for server options and capabilities.
53 /// </summary>
54 Options = 8,
55 /// <summary>
56 /// Put an entity into the server
57 /// </summary>
58 Put = 16,
59 /// <summary>
60 /// Delete an entity from the server
61 /// </summary>
62 Delete = 32,
63 /// <summary>
64 /// Diagnostic trace on server
65 /// </summary>
66 Trace = 64,
67 /// <summary>
68 /// Proxy Connect
69 /// </summary>
70 Connect = 128
73 /// <summary>
74 /// Decorates an action with a restriction to the HTTP method
75 /// that is allowed to request it.
76 /// </summary>
77 [AttributeUsage(AttributeTargets.Method), Serializable]
78 public class AccessibleThroughAttribute : Attribute
80 private readonly Verb verb;
82 /// <summary>
83 /// Constructs a AccessibleThroughAttribute with
84 /// the specified <paramref name="verb"/>.
85 /// </summary>
86 /// <param name="verb">The <see cref="Verb"/> to allow for this action.</param>
87 public AccessibleThroughAttribute(Verb verb)
89 this.verb = verb;
92 /// <summary>
93 /// The Verb to allow.
94 /// </summary>
95 public Verb Verb
97 get { return verb; }
100 /// <summary>
101 /// Checks if the <paramref name="httpMethod"/> is accessible.
102 /// </summary>
103 /// <param name="httpMethod">The <see cref="IRequest.HttpMethod"/> to check.</param>
104 /// <returns><c>true</c> if <paramref name="httpMethod"/> is allowed, otherwise <c>false.</c></returns>
105 public bool ForHttpMethod(string httpMethod)
107 return ForHttpMethod(Verb, HttpMethodToVerb(httpMethod));
110 /// <summary>
111 /// Checks if the <paramref name="httpMethod"/> is accessible.
112 /// </summary>
113 /// <param name="httpMethod">The Http Method to check.</param>
114 /// <returns><c>true</c> if <paramref name="httpMethod"/> is allowed, otherwise <c>false.</c></returns>
115 public bool ForHttpMethod(Verb httpMethod)
117 return ForHttpMethod(Verb, httpMethod);
120 /// <summary>
121 /// Checks it the <paramref name="testMethod"/> is accessible from the <paramref name="allowedMethods"/>.
122 /// </summary>
123 /// <param name="allowedMethods">THe verbs, http methods, that are allowed.</param>
124 /// <param name="testMethod">The verb, http method, that is being testing for access.</param>
125 /// <returns><c>true</c> if the <paramref name="testMethod"/> is an allowed method in the <paramref name="allowedMethods"/> otherwise <c>false</c></returns>
126 public static bool ForHttpMethod(Verb allowedMethods, Verb testMethod)
128 return (allowedMethods & testMethod) == testMethod;
131 /// <summary>
132 /// Converts a <see cref="IRequest.HttpMethod"/> to a <see cref="Verb"/>
133 /// </summary>
134 /// <param name="httpMethod">The http method to convert.</param>
135 /// <returns>The http method as a <see cref="Verb"/></returns>
136 public static Verb HttpMethodToVerb(string httpMethod)
138 return (Verb)Enum.Parse(typeof(Verb), httpMethod, true);