File: ConfigurationPermission.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (195 lines) | stat: -rw-r--r-- 7,723 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//------------------------------------------------------------------------------
// <copyright file="ConfigurationPermission.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {

    using System.Security;
    using System.Security.Permissions;
    using System.Globalization;
    using System.Diagnostics.CodeAnalysis;
    
    [AttributeUsage(AttributeTargets.All, AllowMultiple=true, Inherited=false )]
    [Serializable] 
    sealed public class ConfigurationPermissionAttribute : CodeAccessSecurityAttribute
    {
        public ConfigurationPermissionAttribute(SecurityAction action) : base(action) {}

        public override IPermission CreatePermission() {
            PermissionState state = (this.Unrestricted) ? 
                    PermissionState.Unrestricted : PermissionState.None;

            return new ConfigurationPermission(state);
        }
    }

    //
    // ConfigurationPermission is used to grant access to configuration sections that
    // would not otherwise be available if the caller attempted to read the configuration
    // files that make up configuration.
    //
    // The permission is a simple boolean one - it is either fully granted or denied.
    // This boolean state is represented by using the PermissionState enumeration.
    //
    [Serializable]
    public sealed class ConfigurationPermission : CodeAccessPermission, IUnrestrictedPermission {

        private PermissionState _permissionState;  // Unrestricted or None

        //
        // Creates a new instance of ConfigurationPermission
        // that passes all demands or that fails all demands.
        //
        public ConfigurationPermission(PermissionState state) {

            // validate state parameter
            switch (state) {
            case PermissionState.Unrestricted:
            case PermissionState.None:
                _permissionState = state;
                break;

            default:
                throw ExceptionUtil.ParameterInvalid("state");
            }
        }

        //
        // IUnrestrictedPermission interface methods
        //

        //
        // Checks the overall permission state of the object.
        //
        public bool IsUnrestricted() {
            return _permissionState == PermissionState.Unrestricted;
        }

        //
        // Creates a copy.
        //
        [SuppressMessage("Microsoft.Security", "CA2103:ReviewImperativeSecurity", Justification = "This is a standard implementation of a copy method.")]
        public override IPermission Copy () {
            return new ConfigurationPermission(_permissionState);
        }

        //
        // Returns the logical union between ConfigurationPermission instances.
        //
        [SuppressMessage("Microsoft.Security", "CA2103:ReviewImperativeSecurity", Justification = "This is a standard implementation of a union method.")]
        public override IPermission Union(IPermission target) {
            if (target == null) {
                return Copy();
            }

            if (target.GetType() !=  typeof(ConfigurationPermission)) {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            // Create an Unrestricted permission if either this or other is unrestricted
            if (_permissionState == PermissionState.Unrestricted) {
                return new ConfigurationPermission(PermissionState.Unrestricted);
            }
            else {
                ConfigurationPermission other = (ConfigurationPermission) target;
                return new ConfigurationPermission(other._permissionState);
            }
        }

        //
        // Returns the logical intersection between two ConfigurationPermission instances.
        //
        [SuppressMessage("Microsoft.Security", "CA2103:ReviewImperativeSecurity", Justification = "This is a standard implementation of an intersection method.")]
        public override IPermission Intersect(IPermission target) {
            if (target == null) {
                return null;
            }

            if (target.GetType() !=  typeof(ConfigurationPermission)) {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            // Create an None permission if either this or other is None
            if (_permissionState == PermissionState.None) {
                return new ConfigurationPermission(PermissionState.None);
            }
            else {
                ConfigurationPermission other = (ConfigurationPermission) target;
                return new ConfigurationPermission(other._permissionState);
            }
        }

        //
        // Compares two ConfigurationPermission instances
        //
        public override bool IsSubsetOf(IPermission target) {
            if (target == null) {
                return _permissionState == PermissionState.None;
            }

            if (target.GetType() != typeof(ConfigurationPermission)) {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            ConfigurationPermission other = (ConfigurationPermission) target;
            return (_permissionState == PermissionState.None || other._permissionState == PermissionState.Unrestricted);
        }

        public override void FromXml(SecurityElement securityElement) {
            if (securityElement == null) {
                throw new ArgumentNullException(SR.GetString(SR.ConfigurationPermissionBadXml,"securityElement"));
            }

            if (!securityElement.Tag.Equals("IPermission")) {
                throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml,"securityElement"));
            }

            string className = securityElement.Attribute("class");
            if (className == null) {
                throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml,"securityElement"));
            }

            if (className.IndexOf(this.GetType().FullName, StringComparison.Ordinal ) < 0) {
                throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml,"securityElement"));
            }

            string version = securityElement.Attribute("version");
            if (version != "1") {
                throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml,"version"));
            }

            string unrestricted = securityElement.Attribute("Unrestricted");
            if (unrestricted == null) {
                _permissionState = PermissionState.None;
            }
            else {
                switch (unrestricted) {
                    case "true":
                        _permissionState = PermissionState.Unrestricted;
                        break;

                    case "false":
                        _permissionState = PermissionState.None;
                        break;

                    default:
                        throw new ArgumentException(SR.GetString(SR.ConfigurationPermissionBadXml,"Unrestricted"));
                }
            }
        }

        public override SecurityElement ToXml() {
            SecurityElement securityElement = new SecurityElement("IPermission");
            securityElement.AddAttribute("class", this.GetType().FullName + ", " + this.GetType().Module.Assembly.FullName.Replace( '\"', '\'' ));
            securityElement.AddAttribute("version", "1");
            if (IsUnrestricted()) {
                securityElement.AddAttribute("Unrestricted", "true");
            }

            return securityElement;
        }
    }
}