e4687ef09269eca3a5d899671123dcde37ab3d12
[mono.git] / mcs / class / System / System.Net / WebPermissionAttribute.cs
1 //
2 // System.Net.WebPermissionAttribute.cs
3 //
4 // Author:
5 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //
7 // (C) 2003 Andreas Nahr
8 //
9
10 using System;
11 using System.Security;
12 using System.Security.Permissions;using System.Text.RegularExpressions;
13
14 namespace System.Net
15 {
16         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class 
17                  | AttributeTargets.Struct | AttributeTargets.Constructor 
18                  | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]   
19         [Serializable]
20         public sealed class WebPermissionAttribute : CodeAccessSecurityAttribute
21         {
22                 // Fields
23                 object m_accept;
24                 object m_connect;
25
26                 // Constructors
27                 public WebPermissionAttribute (SecurityAction action) : base (action)
28                 {
29                 }
30
31                 // Properties
32
33                 public string Accept {
34                         get { return m_accept.ToString (); }
35                         set { 
36                                 if (m_accept != null)
37                                         throw new ArgumentException ("The parameter 'Accept' can be set only once.");
38                                 if (value == null) 
39                                         throw new ArgumentException ("The parameter 'Accept' cannot be null.");
40                                 m_accept = value;
41                         }
42                 }
43
44                 public string AcceptPattern {
45                         get { return m_accept.ToString (); }
46                         set { 
47                                 if (m_accept != null)
48                                         throw new ArgumentException ("The parameter 'Accept' can be set only once.");
49                                 if (value == null) 
50                                         throw new ArgumentException ("The parameter 'Accept' cannot be null.");
51                                 m_accept = new Regex (value, RegexOptions.IgnoreCase);
52                         }
53                 }
54
55                 public string Connect {
56                         get { return m_connect.ToString (); }
57                         set { 
58                                 if (m_connect != null)
59                                         throw new ArgumentException ("The parameter 'Connect' can be set only once.");
60                                 if (value == null) 
61                                         throw new ArgumentException ("The parameter 'Connect' cannot be null.");
62                                 m_connect = value;
63                         }
64                 }
65                 public string ConnectPattern {
66                         get { return m_connect.ToString (); }
67                         set { 
68                                 if (m_connect != null)
69                                         throw new ArgumentException ("The parameter 'Connect' can be set only once.");
70                                 if (value == null) 
71                                         throw new ArgumentException ("The parameter 'Connect' cannot be null.");
72                                 m_connect = new Regex (value, RegexOptions.IgnoreCase);
73                         }
74                 }
75
76                 // Methods
77
78                 public override IPermission CreatePermission () 
79                 {
80                         if (this.Unrestricted)
81                                 return new WebPermission (PermissionState.Unrestricted);
82                         WebPermission newPermission = new WebPermission ();
83                         if (m_accept != null)
84                         {
85                                 if (m_accept is Regex)
86                                         newPermission.AddPermission (NetworkAccess.Accept, (Regex)m_accept);
87                                 else
88                                         newPermission.AddPermission (NetworkAccess.Accept, (string)m_accept);
89                         }
90                         if (m_connect != null)
91                         {
92                                 if (m_connect is Regex)
93                                         newPermission.AddPermission (NetworkAccess.Connect, (Regex)m_connect);
94                                 else
95                                         newPermission.AddPermission (NetworkAccess.Connect, (string)m_connect);
96                         }
97                         return newPermission;
98                 }
99         }
100
101