Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Security.Permissions / StorePermissionAttribute.cs
1 //
2 // System.Security.Permissions.StorePermissionAttribute class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System.Globalization;
31
32 namespace System.Security.Permissions {
33
34         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
35                 AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method, 
36                 AllowMultiple = true, Inherited = false)]
37         [Serializable]
38         public sealed class StorePermissionAttribute : CodeAccessSecurityAttribute {
39
40                 private StorePermissionFlags _flags;
41
42                 public StorePermissionAttribute (SecurityAction action)
43                         : base (action) 
44                 {
45                         _flags = StorePermissionFlags.NoFlags;
46                 }
47
48
49                 public StorePermissionFlags Flags {
50                         get { return _flags; }
51                         set {
52                                 if ((value & StorePermissionFlags.AllFlags) != value) {
53                                         string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
54                                         throw new ArgumentException (msg, "StorePermissionFlags");
55                                 }
56
57                                 _flags = value;
58                         }
59                 }
60
61                 public bool AddToStore {
62                         get { return ((_flags & StorePermissionFlags.AddToStore) != 0); }
63                         set {
64                                 if (value) {
65                                         _flags |= StorePermissionFlags.AddToStore;
66                                 }
67                                 else {
68                                         _flags &= ~StorePermissionFlags.AddToStore;
69                                 }
70                         }
71                 }
72
73                 public bool CreateStore {
74                         get { return ((_flags & StorePermissionFlags.CreateStore) != 0); }
75                         set {
76                                 if (value) {
77                                         _flags |= StorePermissionFlags.CreateStore;
78                                 }
79                                 else {
80                                         _flags &= ~StorePermissionFlags.CreateStore;
81                                 }
82                         }
83                 }
84
85                 public bool DeleteStore {
86                         get { return ((_flags & StorePermissionFlags.DeleteStore) != 0); }
87                         set {
88                                 if (value) {
89                                         _flags |= StorePermissionFlags.DeleteStore;
90                                 }
91                                 else {
92                                         _flags &= ~StorePermissionFlags.DeleteStore;
93                                 }
94                         }
95                 }
96
97                 public bool EnumerateCertificates {
98                         get { return ((_flags & StorePermissionFlags.EnumerateCertificates) != 0); }
99                         set {
100                                 if (value) {
101                                         _flags |= StorePermissionFlags.EnumerateCertificates;
102                                 }
103                                 else {
104                                         _flags &= ~StorePermissionFlags.EnumerateCertificates;
105                                 }
106                         }
107                 }
108
109                 public bool EnumerateStores {
110                         get { return ((_flags & StorePermissionFlags.EnumerateStores) != 0); }
111                         set {
112                                 if (value) {
113                                         _flags |= StorePermissionFlags.EnumerateStores;
114                                 }
115                                 else {
116                                         _flags &= ~StorePermissionFlags.EnumerateStores;
117                                 }
118                         }
119                 }
120
121                 public bool OpenStore {
122                         get { return ((_flags & StorePermissionFlags.OpenStore) != 0); }
123                         set {
124                                 if (value) {
125                                         _flags |= StorePermissionFlags.OpenStore;
126                                 }
127                                 else {
128                                         _flags &= ~StorePermissionFlags.OpenStore;
129                                 }
130                         }
131                 }
132
133                 public bool RemoveFromStore {
134                         get { return ((_flags & StorePermissionFlags.RemoveFromStore) != 0); }
135                         set {
136                                 if (value) {
137                                         _flags |= StorePermissionFlags.RemoveFromStore;
138                                 }
139                                 else {
140                                         _flags &= ~StorePermissionFlags.RemoveFromStore;
141                                 }
142                         }
143                 }
144
145
146                 public override IPermission CreatePermission ()
147                 {
148                         StorePermission perm = null;
149                         if (this.Unrestricted)
150                                 perm = new StorePermission (PermissionState.Unrestricted);
151                         else
152                                 perm = new StorePermission (_flags);
153                         return perm;
154                 }
155         }
156 }
157